Commit b8c9b1c6 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Fix race condition causing duplicate replies

We check if the reply exists before adding it because
it may have been added already from polling requests
parent 67cd4763
......@@ -33,10 +33,11 @@ export default {
},
[types.ADD_NEW_REPLY_TO_DISCUSSION](state, note) {
const noteObj = utils.findNoteObjectById(state.discussions, note.discussion_id);
const discussion = utils.findNoteObjectById(state.discussions, note.discussion_id);
const existingNote = discussion && utils.findNoteObjectById(discussion.notes, note.id);
if (noteObj) {
noteObj.notes.push(note);
if (discussion && !existingNote) {
discussion.notes.push(note);
}
},
......
---
title: Fix new discussion replies sometimes showing up twice
merge_request: 17255
author:
type: fixed
......@@ -48,9 +48,22 @@ describe('Notes Store mutations', () => {
});
describe('ADD_NEW_REPLY_TO_DISCUSSION', () => {
const newReply = Object.assign({}, note, { discussion_id: discussionMock.id });
let state;
beforeEach(() => {
state = { discussions: [{ ...discussionMock }] };
});
it('should add a reply to a specific discussion', () => {
const state = { discussions: [discussionMock] };
const newReply = Object.assign({}, note, { discussion_id: discussionMock.id });
mutations.ADD_NEW_REPLY_TO_DISCUSSION(state, newReply);
expect(state.discussions[0].notes.length).toEqual(4);
});
it('should not add the note if it already exists in the discussion', () => {
mutations.ADD_NEW_REPLY_TO_DISCUSSION(state, newReply);
mutations.ADD_NEW_REPLY_TO_DISCUSSION(state, newReply);
expect(state.discussions[0].notes.length).toEqual(4);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment