@@ -3,7 +3,7 @@ import { Provider } from 'react-redux';
33import { BrowserRouter as Router } from 'react-router-dom' ;
44import configureStore from 'redux-mock-store' ;
55import {
6- fireEvent , render , screen , waitFor , act ,
6+ fireEvent , render , screen , waitFor , act , within ,
77} from '@testing-library/react' ;
88import * as auth from '@edx/frontend-platform/auth' ;
99import { 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
6571const 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} ) ;
0 commit comments