fix: when making a comment on a response that doesn't have responses (#63)

Fixes a bug that wouldn't update the UI when making a comment on a response that doesn't have responses.
This commit is contained in:
Kshitij Sobti
2022-02-23 12:36:32 +00:00
committed by GitHub
parent 789d468440
commit df7457a5b5
2 changed files with 44 additions and 1 deletions

View File

@@ -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';

View File

@@ -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