diff options
Diffstat (limited to 'spec/frontend/boards/stores/mutations_spec.js')
-rw-r--r-- | spec/frontend/boards/stores/mutations_spec.js | 330 |
1 files changed, 309 insertions, 21 deletions
diff --git a/spec/frontend/boards/stores/mutations_spec.js b/spec/frontend/boards/stores/mutations_spec.js index c1f7f3dda6e..a13a99a507e 100644 --- a/spec/frontend/boards/stores/mutations_spec.js +++ b/spec/frontend/boards/stores/mutations_spec.js @@ -1,6 +1,17 @@ import mutations from '~/boards/stores/mutations'; +import * as types from '~/boards/stores/mutation_types'; import defaultState from '~/boards/stores/state'; -import { mockIssue } from '../mock_data'; +import { + listObj, + listObjDuplicate, + mockListsWithModel, + mockLists, + rawIssue, + mockIssue, + mockIssue2, + mockIssueWithModel, + mockIssue2WithModel, +} from '../mock_data'; const expectNotImplemented = action => { it('is not implemented', () => { @@ -26,21 +37,56 @@ describe('Board Store Mutations', () => { fullPath: 'gitlab-org', }; const boardType = 'group'; + const disabled = false; + const showPromotion = false; - mutations.SET_INITIAL_BOARD_DATA(state, { ...endpoints, boardType }); + mutations[types.SET_INITIAL_BOARD_DATA](state, { + ...endpoints, + boardType, + disabled, + showPromotion, + }); expect(state.endpoints).toEqual(endpoints); expect(state.boardType).toEqual(boardType); + expect(state.disabled).toEqual(disabled); + expect(state.showPromotion).toEqual(showPromotion); + }); + }); + + describe('RECEIVE_BOARD_LISTS_SUCCESS', () => { + it('Should set boardLists to state', () => { + const lists = [listObj, listObjDuplicate]; + + mutations[types.RECEIVE_BOARD_LISTS_SUCCESS](state, lists); + + expect(state.boardLists).toEqual(lists); }); }); describe('SET_ACTIVE_ID', () => { - it('updates activeListId to be the value that is passed', () => { - const expectedId = 1; + const expected = { id: 1, sidebarType: '' }; - mutations.SET_ACTIVE_ID(state, expectedId); + beforeEach(() => { + mutations.SET_ACTIVE_ID(state, expected); + }); + + it('updates aciveListId to be the value that is passed', () => { + expect(state.activeId).toBe(expected.id); + }); - expect(state.activeId).toBe(expectedId); + it('updates sidebarType to be the value that is passed', () => { + expect(state.sidebarType).toBe(expected.sidebarType); + }); + }); + + describe('SET_FILTERS', () => { + it('updates filterParams to be the value that is passed', () => { + const filterParams = { labelName: 'label' }; + + mutations.SET_FILTERS(state, filterParams); + + expect(state.filterParams).toBe(filterParams); }); }); @@ -56,16 +102,35 @@ describe('Board Store Mutations', () => { expectNotImplemented(mutations.RECEIVE_ADD_LIST_ERROR); }); - describe('REQUEST_UPDATE_LIST', () => { - expectNotImplemented(mutations.REQUEST_UPDATE_LIST); - }); + describe('MOVE_LIST', () => { + it('updates boardLists state with reordered lists', () => { + state = { + ...state, + boardLists: mockListsWithModel, + }; - describe('RECEIVE_UPDATE_LIST_SUCCESS', () => { - expectNotImplemented(mutations.RECEIVE_UPDATE_LIST_SUCCESS); + mutations.MOVE_LIST(state, { + movedList: mockListsWithModel[0], + listAtNewIndex: mockListsWithModel[1], + }); + + expect(state.boardLists).toEqual([mockListsWithModel[1], mockListsWithModel[0]]); + }); }); - describe('RECEIVE_UPDATE_LIST_ERROR', () => { - expectNotImplemented(mutations.RECEIVE_UPDATE_LIST_ERROR); + describe('UPDATE_LIST_FAILURE', () => { + it('updates boardLists state with previous order and sets error message', () => { + state = { + ...state, + boardLists: [mockListsWithModel[1], mockListsWithModel[0]], + error: undefined, + }; + + mutations.UPDATE_LIST_FAILURE(state, mockListsWithModel); + + expect(state.boardLists).toEqual(mockListsWithModel); + expect(state.error).toEqual('An error occurred while updating the list. Please try again.'); + }); }); describe('REQUEST_REMOVE_LIST', () => { @@ -80,6 +145,33 @@ describe('Board Store Mutations', () => { expectNotImplemented(mutations.RECEIVE_REMOVE_LIST_ERROR); }); + describe('RECEIVE_ISSUES_FOR_LIST_SUCCESS', () => { + it('updates issuesByListId and issues on state', () => { + const listIssues = { + 'gid://gitlab/List/1': [mockIssue.id], + }; + const issues = { + '1': mockIssue, + }; + + state = { + ...state, + isLoadingIssues: true, + issuesByListId: {}, + issues: {}, + boardLists: mockListsWithModel, + }; + + mutations.RECEIVE_ISSUES_FOR_LIST_SUCCESS(state, { + listIssues: { listData: listIssues, issues }, + listId: 'gid://gitlab/List/1', + }); + + expect(state.issuesByListId).toEqual(listIssues); + expect(state.issues).toEqual(issues); + }); + }); + describe('REQUEST_ISSUES_FOR_ALL_LISTS', () => { it('sets isLoadingIssues to true', () => { expect(state.isLoadingIssues).toBe(false); @@ -90,22 +182,45 @@ describe('Board Store Mutations', () => { }); }); + describe('RECEIVE_ISSUES_FOR_LIST_FAILURE', () => { + it('sets error message', () => { + state = { + ...state, + boardLists: mockListsWithModel, + error: undefined, + }; + + const listId = 'gid://gitlab/List/1'; + + mutations.RECEIVE_ISSUES_FOR_LIST_FAILURE(state, listId); + + expect(state.error).toEqual( + 'An error occurred while fetching the board issues. Please reload the page.', + ); + }); + }); + describe('RECEIVE_ISSUES_FOR_ALL_LISTS_SUCCESS', () => { it('sets isLoadingIssues to false and updates issuesByListId object', () => { const listIssues = { - '1': [mockIssue], + 'gid://gitlab/List/1': [mockIssue.id], + }; + const issues = { + '1': mockIssue, }; state = { ...state, isLoadingIssues: true, issuesByListId: {}, + issues: {}, }; - mutations.RECEIVE_ISSUES_FOR_ALL_LISTS_SUCCESS(state, listIssues); + mutations.RECEIVE_ISSUES_FOR_ALL_LISTS_SUCCESS(state, { listData: listIssues, issues }); expect(state.isLoadingIssues).toBe(false); expect(state.issuesByListId).toEqual(listIssues); + expect(state.issues).toEqual(issues); }); }); @@ -113,6 +228,65 @@ describe('Board Store Mutations', () => { expectNotImplemented(mutations.REQUEST_ADD_ISSUE); }); + describe('RECEIVE_ISSUES_FOR_ALL_LISTS_FAILURE', () => { + it('sets isLoadingIssues to false and sets error message', () => { + state = { + ...state, + isLoadingIssues: true, + error: undefined, + }; + + mutations.RECEIVE_ISSUES_FOR_ALL_LISTS_FAILURE(state); + + expect(state.isLoadingIssues).toBe(false); + expect(state.error).toEqual( + 'An error occurred while fetching the board issues. Please reload the page.', + ); + }); + }); + + describe('UPDATE_ISSUE_BY_ID', () => { + const issueId = '1'; + const prop = 'id'; + const value = '2'; + const issue = { [issueId]: { id: 1, title: 'Issue' } }; + + beforeEach(() => { + state = { + ...state, + isLoadingIssues: true, + error: undefined, + issues: { + ...issue, + }, + }; + }); + + describe('when the issue is in state', () => { + it('updates the property of the correct issue', () => { + mutations.UPDATE_ISSUE_BY_ID(state, { + issueId, + prop, + value, + }); + + expect(state.issues[issueId]).toEqual({ ...issue[issueId], id: '2' }); + }); + }); + + describe('when the issue is not in state', () => { + it('throws an error', () => { + expect(() => { + mutations.UPDATE_ISSUE_BY_ID(state, { + issueId: '3', + prop, + value, + }); + }).toThrow(new Error('No issue found.')); + }); + }); + }); + describe('RECEIVE_ADD_ISSUE_SUCCESS', () => { expectNotImplemented(mutations.RECEIVE_ADD_ISSUE_SUCCESS); }); @@ -121,16 +295,86 @@ describe('Board Store Mutations', () => { expectNotImplemented(mutations.RECEIVE_ADD_ISSUE_ERROR); }); - describe('REQUEST_MOVE_ISSUE', () => { - expectNotImplemented(mutations.REQUEST_MOVE_ISSUE); + describe('MOVE_ISSUE', () => { + it('updates issuesByListId, moving issue between lists', () => { + const listIssues = { + 'gid://gitlab/List/1': [mockIssue.id, mockIssue2.id], + 'gid://gitlab/List/2': [], + }; + + const issues = { + '1': mockIssueWithModel, + '2': mockIssue2WithModel, + }; + + state = { + ...state, + issuesByListId: listIssues, + boardLists: mockListsWithModel, + issues, + }; + + mutations.MOVE_ISSUE(state, { + originalIssue: mockIssue2WithModel, + fromListId: 'gid://gitlab/List/1', + toListId: 'gid://gitlab/List/2', + }); + + const updatedListIssues = { + 'gid://gitlab/List/1': [mockIssue.id], + 'gid://gitlab/List/2': [mockIssue2.id], + }; + + expect(state.issuesByListId).toEqual(updatedListIssues); + }); }); - describe('RECEIVE_MOVE_ISSUE_SUCCESS', () => { - expectNotImplemented(mutations.RECEIVE_MOVE_ISSUE_SUCCESS); + describe('MOVE_ISSUE_SUCCESS', () => { + it('updates issue in issues state', () => { + const issues = { + '436': { id: rawIssue.id }, + }; + + state = { + ...state, + issues, + }; + + mutations.MOVE_ISSUE_SUCCESS(state, { + issue: rawIssue, + }); + + expect(state.issues).toEqual({ '436': { ...mockIssueWithModel, id: 436 } }); + }); }); - describe('RECEIVE_MOVE_ISSUE_ERROR', () => { - expectNotImplemented(mutations.RECEIVE_MOVE_ISSUE_ERROR); + describe('MOVE_ISSUE_FAILURE', () => { + it('updates issuesByListId, reverting moving issue between lists, and sets error message', () => { + const listIssues = { + 'gid://gitlab/List/1': [mockIssue.id], + 'gid://gitlab/List/2': [mockIssue2.id], + }; + + state = { + ...state, + issuesByListId: listIssues, + }; + + mutations.MOVE_ISSUE_FAILURE(state, { + originalIssue: mockIssue2, + fromListId: 'gid://gitlab/List/1', + toListId: 'gid://gitlab/List/2', + originalIndex: 1, + }); + + const updatedListIssues = { + 'gid://gitlab/List/1': [mockIssue.id, mockIssue2.id], + 'gid://gitlab/List/2': [], + }; + + expect(state.issuesByListId).toEqual(updatedListIssues); + expect(state.error).toEqual('An error occurred while moving the issue. Please try again.'); + }); }); describe('REQUEST_UPDATE_ISSUE', () => { @@ -145,6 +389,50 @@ describe('Board Store Mutations', () => { expectNotImplemented(mutations.RECEIVE_UPDATE_ISSUE_ERROR); }); + describe('ADD_ISSUE_TO_LIST', () => { + it('adds issue to issues state and issue id in list in issuesByListId', () => { + const listIssues = { + 'gid://gitlab/List/1': [mockIssue.id], + }; + const issues = { + '1': mockIssue, + }; + + state = { + ...state, + issuesByListId: listIssues, + issues, + }; + + mutations.ADD_ISSUE_TO_LIST(state, { list: mockLists[0], issue: mockIssue2 }); + + expect(state.issuesByListId['gid://gitlab/List/1']).toContain(mockIssue2.id); + expect(state.issues[mockIssue2.id]).toEqual(mockIssue2); + }); + }); + + describe('ADD_ISSUE_TO_LIST_FAILURE', () => { + it('removes issue id from list in issuesByListId', () => { + const listIssues = { + 'gid://gitlab/List/1': [mockIssue.id, mockIssue2.id], + }; + const issues = { + '1': mockIssue, + '2': mockIssue2, + }; + + state = { + ...state, + issuesByListId: listIssues, + issues, + }; + + mutations.ADD_ISSUE_TO_LIST_FAILURE(state, { list: mockLists[0], issue: mockIssue2 }); + + expect(state.issuesByListId['gid://gitlab/List/1']).not.toContain(mockIssue2.id); + }); + }); + describe('SET_CURRENT_PAGE', () => { expectNotImplemented(mutations.SET_CURRENT_PAGE); }); |