File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -118,9 +118,7 @@ const SortableCommentListComponent = ({
118118
119119 useEffect ( ( ) => {
120120 fetchComments ( section . id , DEFAULT_ORDERING ) ;
121-
122- // eslint-disable-next-line react-hooks/exhaustive-deps
123- } , [ ] ) ;
121+ } , [ section . id ] ) ;
124122
125123 useEffect ( ( ) => {
126124 if ( sectionComments ) {
Original file line number Diff line number Diff line change @@ -181,4 +181,62 @@ describe('<SortableCommentList />', () => {
181181
182182 waitFor ( ( ) => expect ( fetchCommentsMock ) . toHaveBeenCalledTimes ( 2 ) ) ;
183183 } ) ;
184+
185+ it ( 'should fetch comments again when section changes' , async ( ) => {
186+ const fetchCommentsMock = vi . fn ( ) ;
187+ const firstSection = {
188+ ...mockHearingWithSections . data . sections [ 0 ] ,
189+ questions : [ ] ,
190+ } ;
191+ const secondSection = {
192+ ...mockHearingWithSections . data . sections [ 1 ] ,
193+ questions : [ ] ,
194+ } ;
195+
196+ const store = mockStore ( storeDefaultState ) ;
197+
198+ const defaultProps = {
199+ hearingSlug : mockHearingWithSections . data . slug ,
200+ canVote : true ,
201+ canComment : true ,
202+ published : true ,
203+ fetchComments : fetchCommentsMock ,
204+ } ;
205+
206+ const { rerender } = renderWithProviders (
207+ < MemoryRouter >
208+ < SortableCommentListComponent
209+ intl = { getIntlAsProp ( ) }
210+ { ...defaultProps }
211+ section = { firstSection }
212+ />
213+ </ MemoryRouter > ,
214+ { store }
215+ ) ;
216+
217+ await waitFor ( ( ) =>
218+ expect ( fetchCommentsMock ) . toHaveBeenCalledWith (
219+ firstSection . id ,
220+ '-created_at'
221+ )
222+ ) ;
223+
224+ rerender (
225+ < MemoryRouter >
226+ < SortableCommentListComponent
227+ intl = { getIntlAsProp ( ) }
228+ { ...defaultProps }
229+ section = { secondSection }
230+ />
231+ </ MemoryRouter >
232+ ) ;
233+
234+ // Verify that fetch was called again when section changed
235+ await waitFor ( ( ) =>
236+ expect ( fetchCommentsMock ) . toHaveBeenCalledWith (
237+ secondSection . id ,
238+ '-created_at'
239+ )
240+ ) ;
241+ } ) ;
184242} ) ;
You can’t perform that action at this time.
0 commit comments