diff options
Diffstat (limited to 'spec/frontend/merge_conflicts/store/actions_spec.js')
-rw-r--r-- | spec/frontend/merge_conflicts/store/actions_spec.js | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/spec/frontend/merge_conflicts/store/actions_spec.js b/spec/frontend/merge_conflicts/store/actions_spec.js new file mode 100644 index 00000000000..352f1783b87 --- /dev/null +++ b/spec/frontend/merge_conflicts/store/actions_spec.js @@ -0,0 +1,257 @@ +import axios from 'axios'; +import MockAdapter from 'axios-mock-adapter'; +import { useMockLocationHelper } from 'helpers/mock_window_location_helper'; +import testAction from 'helpers/vuex_action_helper'; +import createFlash from '~/flash'; +import { INTERACTIVE_RESOLVE_MODE, EDIT_RESOLVE_MODE } from '~/merge_conflicts/constants'; +import * as actions from '~/merge_conflicts/store/actions'; +import * as types from '~/merge_conflicts/store/mutation_types'; +import { restoreFileLinesState, markLine, decorateFiles } from '~/merge_conflicts/utils'; + +jest.mock('~/flash.js'); +jest.mock('~/merge_conflicts/utils'); + +describe('merge conflicts actions', () => { + let mock; + + const files = [ + { + blobPath: 'a', + }, + { blobPath: 'b' }, + ]; + + beforeEach(() => { + mock = new MockAdapter(axios); + }); + + afterEach(() => { + mock.restore(); + }); + + describe('fetchConflictsData', () => { + const conflictsPath = 'conflicts/path/mock'; + + it('on success dispatches setConflictsData', (done) => { + mock.onGet(conflictsPath).reply(200, {}); + testAction( + actions.fetchConflictsData, + conflictsPath, + {}, + [ + { type: types.SET_LOADING_STATE, payload: true }, + { type: types.SET_LOADING_STATE, payload: false }, + ], + [{ type: 'setConflictsData', payload: {} }], + done, + ); + }); + + it('when data has type equal to error ', (done) => { + mock.onGet(conflictsPath).reply(200, { type: 'error', message: 'error message' }); + testAction( + actions.fetchConflictsData, + conflictsPath, + {}, + [ + { type: types.SET_LOADING_STATE, payload: true }, + { type: types.SET_FAILED_REQUEST, payload: 'error message' }, + { type: types.SET_LOADING_STATE, payload: false }, + ], + [], + done, + ); + }); + + it('when request fails ', (done) => { + mock.onGet(conflictsPath).reply(400); + testAction( + actions.fetchConflictsData, + conflictsPath, + {}, + [ + { type: types.SET_LOADING_STATE, payload: true }, + { type: types.SET_FAILED_REQUEST }, + { type: types.SET_LOADING_STATE, payload: false }, + ], + [], + done, + ); + }); + }); + + describe('submitResolvedConflicts', () => { + useMockLocationHelper(); + const resolveConflictsPath = 'resolve/conflicts/path/mock'; + + it('on success reloads the page', (done) => { + mock.onPost(resolveConflictsPath).reply(200, { redirect_to: 'hrefPath' }); + testAction( + actions.submitResolvedConflicts, + resolveConflictsPath, + {}, + [{ type: types.SET_SUBMIT_STATE, payload: true }], + [], + () => { + expect(window.location.assign).toHaveBeenCalledWith('hrefPath'); + done(); + }, + ); + }); + + it('on errors shows flash', (done) => { + mock.onPost(resolveConflictsPath).reply(400); + testAction( + actions.submitResolvedConflicts, + resolveConflictsPath, + {}, + [ + { type: types.SET_SUBMIT_STATE, payload: true }, + { type: types.SET_SUBMIT_STATE, payload: false }, + ], + [], + () => { + expect(createFlash).toHaveBeenCalledWith({ + message: 'Failed to save merge conflicts resolutions. Please try again!', + }); + done(); + }, + ); + }); + }); + + describe('setConflictsData', () => { + it('INTERACTIVE_RESOLVE_MODE updates the correct file ', (done) => { + decorateFiles.mockReturnValue([{ bar: 'baz' }]); + testAction( + actions.setConflictsData, + { files, foo: 'bar' }, + {}, + [ + { + type: types.SET_CONFLICTS_DATA, + payload: { foo: 'bar', files: [{ bar: 'baz' }] }, + }, + ], + [], + done, + ); + }); + }); + + describe('setFileResolveMode', () => { + it('INTERACTIVE_RESOLVE_MODE updates the correct file ', (done) => { + testAction( + actions.setFileResolveMode, + { file: files[0], mode: INTERACTIVE_RESOLVE_MODE }, + { conflictsData: { files }, getFileIndex: () => 0 }, + [ + { + type: types.UPDATE_FILE, + payload: { + file: { ...files[0], showEditor: false, resolveMode: INTERACTIVE_RESOLVE_MODE }, + index: 0, + }, + }, + ], + [], + done, + ); + }); + + it('EDIT_RESOLVE_MODE updates the correct file ', (done) => { + restoreFileLinesState.mockReturnValue([]); + const file = { + ...files[0], + showEditor: true, + loadEditor: true, + resolutionData: {}, + resolveMode: EDIT_RESOLVE_MODE, + }; + testAction( + actions.setFileResolveMode, + { file: files[0], mode: EDIT_RESOLVE_MODE }, + { conflictsData: { files }, getFileIndex: () => 0 }, + [ + { + type: types.UPDATE_FILE, + payload: { + file, + index: 0, + }, + }, + ], + [], + () => { + expect(restoreFileLinesState).toHaveBeenCalledWith(file); + done(); + }, + ); + }); + }); + + describe('setPromptConfirmationState', () => { + it('updates the correct file ', (done) => { + testAction( + actions.setPromptConfirmationState, + { file: files[0], promptDiscardConfirmation: true }, + { conflictsData: { files }, getFileIndex: () => 0 }, + [ + { + type: types.UPDATE_FILE, + payload: { + file: { ...files[0], promptDiscardConfirmation: true }, + index: 0, + }, + }, + ], + [], + done, + ); + }); + }); + + describe('handleSelected', () => { + const file = { + ...files[0], + inlineLines: [{ id: 1, hasConflict: true }, { id: 2 }], + parallelLines: [ + [{ id: 1, hasConflict: true }, { id: 1 }], + [{ id: 2 }, { id: 3 }], + ], + }; + + it('updates the correct file ', (done) => { + const marLikeMockReturn = { foo: 'bar' }; + markLine.mockReturnValue(marLikeMockReturn); + + testAction( + actions.handleSelected, + { file, line: { id: 1, section: 'baz' } }, + { conflictsData: { files }, getFileIndex: () => 0 }, + [ + { + type: types.UPDATE_FILE, + payload: { + file: { + ...file, + resolutionData: { 1: 'baz' }, + inlineLines: [marLikeMockReturn, { id: 2 }], + parallelLines: [ + [marLikeMockReturn, marLikeMockReturn], + [{ id: 2 }, { id: 3 }], + ], + }, + index: 0, + }, + }, + ], + [], + () => { + expect(markLine).toHaveBeenCalledTimes(3); + done(); + }, + ); + }); + }); +}); |