Skip to content

Commit 820caa6

Browse files
committed
fix: comments fetching fix on section change
- added also test for the case Refs: KER-594
1 parent 6719516 commit 820caa6

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

src/components/SortableCommentList.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff 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) {

src/components/__tests__/SortableCommentList.test.jsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

0 commit comments

Comments
 (0)