diff --git a/src/discussions/comments/data/redux.test.js b/src/discussions/comments/data/redux.test.js index 9c907269..bec22f11 100644 --- a/src/discussions/comments/data/redux.test.js +++ b/src/discussions/comments/data/redux.test.js @@ -100,7 +100,7 @@ describe('Comments/Responses data layer tests', () => { .toEqual({ 'comment-1': ['comment-4', 'comment-5', 'comment-6'] }); }); - test('successfully handles comment creation for discussion type threads', async () => { + test('successfully handles response creation for discussion type threads', async () => { const threadId = 'test-thread'; const content = 'Test comment'; axiosMock.onGet(commentsApiUrl) @@ -132,6 +132,46 @@ describe('Comments/Responses data layer tests', () => { .toEqual(threadId); }); + test('successfully handles reply creation for discussion type threads', async () => { + const threadId = 'test-thread'; + const parentId = 'comment-1'; + const content = 'Test comment'; + axiosMock.onGet(commentsApiUrl) + .reply(200, Factory.build('commentsResult')); + + expect(store.getState().comments.commentsInComments) + .not.toHaveProperty(parentId); + + await executeThunk(fetchThreadComments(threadId), store.dispatch, store.getState); + + axiosMock.onPost(`${commentsApiUrl}`) + .reply(200, Factory.build('comment', { + thread_id: threadId, + parent_id: parentId, + raw_body: content, + rendered_body: content, + })); + + await executeThunk(addComment(content, threadId, null), store.dispatch, store.getState); + + expect(store.getState().comments.commentsInThreads[threadId]) + .toEqual({ + [EndorsementStatus.DISCUSSION]: [ + 'comment-1', + 'comment-2', + 'comment-3', + ], + }); + expect(Object.keys(store.getState().comments.commentsById)) + .toEqual(['comment-1', 'comment-2', 'comment-3', 'comment-4']); + expect(store.getState().comments.commentsInComments[parentId]) + .toEqual(['comment-4']); + expect(store.getState().comments.commentsById['comment-4'].threadId) + .toEqual(threadId); + expect(store.getState().comments.commentsById['comment-4'].parentId) + .toEqual(parentId); + }); + test('successfully handles comment creation for question type threads', async () => { const threadId = 'test-thread'; const content = 'Test comment'; diff --git a/src/discussions/comments/data/slices.js b/src/discussions/comments/data/slices.js index 18f9c8e2..1ade9598 100644 --- a/src/discussions/comments/data/slices.js +++ b/src/discussions/comments/data/slices.js @@ -115,6 +115,9 @@ const commentsSlice = createSlice({ postCommentSuccess: (state, { payload }) => { state.postStatus = RequestStatus.SUCCESSFUL; if (payload.parentId) { + if (!(payload.parentId in state.commentsInComments)) { + state.commentsInComments[payload.parentId] = []; + } state.commentsInComments[payload.parentId].push(payload.id); } else { // The comment should be added to either the discussion or unendorsed