Skip to content

Commit 67feee5

Browse files
refactor: fix reveiw issues
1 parent 837ac4e commit 67feee5

4 files changed

Lines changed: 59 additions & 61 deletions

File tree

src/notification-preferences/NotificationPreferenceApp.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const NotificationPreferenceApp = ({ appId }) => {
4141
return null;
4242
}
4343
return (
44-
<Collapsible.Advanced open={appToggle} data-testid="notification-app" className="mb-5">
44+
<Collapsible.Advanced open={appToggle} data-testid={`${appId}-app`} className="mb-5">
4545
<Collapsible.Trigger>
4646
<div className="d-flex align-items-center">
4747
<span className="mr-auto">

src/notification-preferences/NotificationPreferenceRow.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const NotificationPreferenceRow = ({ appId, preferenceName }) => {
7878
value={preference[channel]}
7979
onChange={onToggle}
8080
disabled={nonEditable.includes(channel) || updatePreferencesStatus === LOADING_STATUS}
81-
dataTestId={`${preferenceName}-${channel}`}
81+
id={`${preferenceName}-${channel}`}
8282
/>
8383
</div>
8484
))}

src/notification-preferences/NotificationPreferences.test.jsx

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Provider } from 'react-redux';
33
import { BrowserRouter as Router } from 'react-router-dom';
44
import configureStore from 'redux-mock-store';
55
import {
6-
fireEvent, render, screen, waitFor, act,
6+
fireEvent, render, screen, waitFor, act, within,
77
} from '@testing-library/react';
88
import * as auth from '@edx/frontend-platform/auth';
99
import { IntlProvider } from '@edx/frontend-platform/i18n';
@@ -31,11 +31,11 @@ const defaultPreferences = {
3131
],
3232
preferences: [
3333
{
34-
id: 'newPost',
34+
id: 'core',
3535
appId: 'discussion',
36-
web: false,
37-
push: false,
38-
email: false,
36+
web: true,
37+
push: true,
38+
email: true,
3939
},
4040
{
4141
id: 'newComment',
@@ -59,23 +59,20 @@ const defaultPreferences = {
5959
email: false,
6060
},
6161
],
62-
nonEditable: {},
62+
nonEditable: {
63+
discussion: {
64+
core: [
65+
'web',
66+
],
67+
},
68+
},
6369
};
6470

6571
const updateChannelPreferences = (toggleVal = false) => ({
6672
preferences: [
67-
{
68-
id: 'newPost', appId: 'discussion', web: toggleVal, push: toggleVal, email: toggleVal,
69-
},
70-
{
71-
id: 'newComment', appId: 'discussion', web: toggleVal, push: toggleVal, email: toggleVal,
72-
},
73-
{
74-
id: 'newAssignment', appId: 'coursework', web: toggleVal, push: toggleVal, email: toggleVal,
75-
},
76-
{
77-
id: 'newGrade', appId: 'coursework', web: toggleVal, push: toggleVal, email: toggleVal,
78-
},
73+
{ id: 'core', appId: 'discussion', web: true },
74+
{ id: 'newComment', appId: 'discussion', web: toggleVal },
75+
{ id: 'newAssignment', appId: 'coursework', web: toggleVal },
7976
],
8077
});
8178

@@ -97,7 +94,7 @@ const setupStore = (override = {}) => {
9794
return store;
9895
};
9996

100-
const renderComponent = (store = {}) => (
97+
const notificationPreferences = (store = {}) => (
10198
<Router>
10299
<IntlProvider locale="en">
103100
<Provider store={store}>
@@ -129,75 +126,76 @@ describe('Notification Preferences', () => {
129126
afterEach(() => jest.clearAllMocks());
130127

131128
it('tests if all notification apps are listed', async () => {
132-
await render(renderComponent(store));
133-
expect(screen.queryAllByTestId('notification-app')).toHaveLength(2);
129+
await render(notificationPreferences(store));
130+
expect(screen.queryByTestId('discussion-app')).toBeInTheDocument();
131+
expect(screen.queryByTestId('coursework-app')).toBeInTheDocument();
134132
});
135133

136134
it('show spinner if api call is in progress', async () => {
137135
store = setupStore({ status: LOADING_STATUS });
138-
await render(renderComponent(store));
136+
await render(notificationPreferences(store));
139137
expect(screen.queryByTestId('loading-spinner')).toBeInTheDocument();
140138
});
141139

142140
it('tests if all notification preferences are listed', async () => {
143-
await render(renderComponent(store));
141+
await render(notificationPreferences(store));
144142
expect(screen.queryAllByTestId('notification-preference')).toHaveLength(4);
145143
});
146144

147145
it('update group on click', async () => {
148-
const wrapper = await render(renderComponent(store));
146+
const wrapper = await render(notificationPreferences(store));
149147
const element = wrapper.container.querySelector('#discussion-app-toggle');
150148
await fireEvent.click(element);
151149
expect(mockDispatch).toHaveBeenCalled();
152150
});
153151

154152
it('update preference on click', async () => {
155-
const wrapper = await render(renderComponent(store));
156-
const element = wrapper.container.querySelector('#newPost-web');
153+
const wrapper = await render(notificationPreferences(store));
154+
const element = wrapper.container.querySelector('#core-web');
157155
expect(element).not.toBeChecked();
158156
await fireEvent.click(element);
159157
expect(mockDispatch).toHaveBeenCalled();
160158
});
161159

162160
it('show not found page if invalid course id is entered in url', async () => {
163161
store = setupStore({ status: FAILURE_STATUS, selectedCourse: 'invalid-course-id' });
164-
await render(renderComponent(store));
162+
await render(notificationPreferences(store));
165163
expect(screen.queryByTestId('not-found-page')).toBeInTheDocument();
166164
});
167165

168-
it.each([false, true])(
169-
'updates all preferences in the column on web channel click when toggle state is - %s',
170-
async (toggleState) => {
171-
store = setupStore(updateChannelPreferences(toggleState));
172-
const wrapper = render(renderComponent(store));
166+
it('updates all preferences in the column on web channel click', async () => {
167+
store = setupStore(updateChannelPreferences(true));
168+
const wrapper = render(notificationPreferences(store));
173169

174-
const getCheckbox = (id) => screen.queryByTestId(`${id}-web`);
175-
const checkboxes = ['newPost', 'newComment', 'newAssignment', 'newGrade'];
170+
const getChannelSwitch = (id) => screen.queryByTestId(`${id}-web`);
171+
const notificationTypes = ['newComment', 'newAssignment'];
176172

177-
const verifyState = (expectedState) => {
178-
checkboxes.forEach((checkbox) => {
179-
if (expectedState) {
180-
expect(getCheckbox(checkbox)).toBeChecked();
181-
} else {
182-
expect(getCheckbox(checkbox)).not.toBeChecked();
183-
}
184-
});
185-
};
173+
const verifyState = (toggleState) => {
174+
notificationTypes.forEach((notificationType) => {
175+
if (toggleState) {
176+
expect(getChannelSwitch(notificationType)).toBeChecked();
177+
} else {
178+
expect(getChannelSwitch(notificationType)).not.toBeChecked();
179+
}
180+
});
181+
};
186182

187-
verifyState(toggleState);
183+
verifyState(true);
184+
expect(getChannelSwitch('core')).toBeChecked();
188185

189-
const element = screen.queryAllByText('Web')[0];
186+
const discussionApp = screen.queryByTestId('discussion-app');
187+
const webChannel = within(discussionApp).queryByText('Web');
190188

191-
await act(async () => {
192-
await fireEvent.click(element);
193-
});
189+
await act(async () => {
190+
await fireEvent.click(webChannel);
191+
});
194192

195-
store = setupStore(updateChannelPreferences(!toggleState));
196-
wrapper.rerender(renderComponent(store));
193+
store = setupStore(updateChannelPreferences(false));
194+
wrapper.rerender(notificationPreferences(store));
197195

198-
await waitFor(() => {
199-
verifyState(!toggleState);
200-
});
201-
},
202-
);
196+
await waitFor(() => {
197+
verifyState(false);
198+
expect(getChannelSwitch('core')).toBeChecked();
199+
});
200+
});
203201
});

src/notification-preferences/ToggleSwitch.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const ToggleSwitch = ({
77
value,
88
disabled,
99
onChange,
10-
dataTestId,
10+
id,
1111
}) => (
1212
<Form.Switch
1313
name={name}
1414
checked={value}
1515
disabled={disabled}
1616
onChange={onChange}
17-
data-testid={dataTestId}
17+
data-testid={id}
1818
/>
1919
);
2020

@@ -23,13 +23,13 @@ ToggleSwitch.propTypes = {
2323
value: PropTypes.bool.isRequired,
2424
disabled: PropTypes.bool,
2525
onChange: PropTypes.func,
26-
dataTestId: PropTypes.string,
26+
id: PropTypes.string,
2727
};
2828

2929
ToggleSwitch.defaultProps = {
3030
onChange: () => null,
3131
disabled: false,
32-
dataTestId: '',
32+
id: '',
3333
};
3434

3535
export default React.memo(ToggleSwitch);

0 commit comments

Comments
 (0)