diff --git a/app/assets/javascripts/diffs/store/actions.js b/app/assets/javascripts/diffs/store/actions.js index e5bb9904664..27001142257 100644 --- a/app/assets/javascripts/diffs/store/actions.js +++ b/app/assets/javascripts/diffs/store/actions.js @@ -103,9 +103,7 @@ export const toggleFileDiscussions = ({ getters, dispatch }, diff) => { if (shouldCloseAll) { dispatch('collapseDiscussion', data, { root: true }); - } else if (shouldExpandAll) { - dispatch('expandDiscussion', data, { root: true }); - } else if (!shouldCloseAll && !shouldExpandAll && !discussion.expanded) { + } else if (shouldExpandAll || (!shouldCloseAll && !shouldExpandAll && !discussion.expanded)) { dispatch('expandDiscussion', data, { root: true }); } }); diff --git a/spec/javascripts/diffs/store/actions_spec.js b/spec/javascripts/diffs/store/actions_spec.js index 6829c1e956a..c1560dac1a0 100644 --- a/spec/javascripts/diffs/store/actions_spec.js +++ b/spec/javascripts/diffs/store/actions_spec.js @@ -191,4 +191,48 @@ describe('DiffsStoreActions', () => { ); }); }); + + describe('toggleFileDiscussions', () => { + it('should dispatch collapseDiscussion when all discussions are expanded', () => { + const getters = { + getDiffFileDiscussions: jasmine.createSpy().and.returnValue([{ id: 1 }]), + diffHasAllExpandedDiscussions: jasmine.createSpy().and.returnValue(true), + diffHasAllCollpasedDiscussions: jasmine.createSpy().and.returnValue(false), + }; + + const dispatch = jasmine.createSpy('dispatch'); + + actions.toggleFileDiscussions({ getters, dispatch }); + + expect(dispatch).toHaveBeenCalledWith('collapseDiscussion', { discussionId: 1 }, { root: true }); + }); + + it('should dispatch expandDiscussion when all discussions are collapsed', () => { + const getters = { + getDiffFileDiscussions: jasmine.createSpy().and.returnValue([{ id: 1 }]), + diffHasAllExpandedDiscussions: jasmine.createSpy().and.returnValue(false), + diffHasAllCollpasedDiscussions: jasmine.createSpy().and.returnValue(true), + }; + + const dispatch = jasmine.createSpy(); + + actions.toggleFileDiscussions({ getters, dispatch }); + + expect(dispatch).toHaveBeenCalledWith('expandDiscussion', { discussionId: 1 }, { root: true }); + }); + + it('should dispatch expandDiscussion when some discussions are collapsed and others are expanded for the collapsed discussion', () => { + const getters = { + getDiffFileDiscussions: jasmine.createSpy().and.returnValue([{ expanded: false, id: 1 }]), + diffHasAllExpandedDiscussions: jasmine.createSpy().and.returnValue(false), + diffHasAllCollpasedDiscussions: jasmine.createSpy().and.returnValue(false), + }; + + const dispatch = jasmine.createSpy(); + + actions.toggleFileDiscussions({ getters, dispatch }); + + expect(dispatch).toHaveBeenCalledWith('expandDiscussion', { discussionId: 1 }, { root: true }); + }); + }); });