diff options
Diffstat (limited to 'spec/frontend/diffs/store/actions_spec.js')
-rw-r--r-- | spec/frontend/diffs/store/actions_spec.js | 439 |
1 files changed, 161 insertions, 278 deletions
diff --git a/spec/frontend/diffs/store/actions_spec.js b/spec/frontend/diffs/store/actions_spec.js index d6a2aa104cd..3b567fbc704 100644 --- a/spec/frontend/diffs/store/actions_spec.js +++ b/spec/frontend/diffs/store/actions_spec.js @@ -9,46 +9,7 @@ import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE, } from '~/diffs/constants'; -import { - setBaseConfig, - fetchDiffFilesBatch, - fetchDiffFilesMeta, - fetchCoverageFiles, - assignDiscussionsToDiff, - removeDiscussionsFromDiff, - startRenderDiffsQueue, - setInlineDiffViewType, - setParallelDiffViewType, - showCommentForm, - cancelCommentForm, - loadMoreLines, - scrollToLineIfNeededInline, - scrollToLineIfNeededParallel, - loadCollapsedDiff, - toggleFileDiscussions, - saveDiffDiscussion, - setHighlightedRow, - toggleTreeOpen, - scrollToFile, - setShowTreeList, - renderFileForDiscussionId, - setRenderTreeList, - setShowWhitespace, - setRenderIt, - receiveFullDiffError, - fetchFullDiff, - toggleFullDiff, - switchToFullDiffFromRenamedFile, - setFileCollapsedByUser, - setExpandedDiffLines, - setSuggestPopoverDismissed, - changeCurrentCommit, - moveToNeighboringCommit, - setCurrentDiffFileIdFromNote, - navigateToDiffFileIndex, - setFileByFile, - reviewFile, -} from '~/diffs/store/actions'; +import * as diffActions from '~/diffs/store/actions'; import * as types from '~/diffs/store/mutation_types'; import * as utils from '~/diffs/store/utils'; import * as treeWorkerUtils from '~/diffs/utils/tree_worker_utils'; @@ -62,6 +23,8 @@ import { diffMetadata } from '../mock_data/diff_metadata'; jest.mock('~/flash'); describe('DiffsStoreActions', () => { + let mock; + useLocalStorageSpy(); const originalMethods = { @@ -83,15 +46,20 @@ describe('DiffsStoreActions', () => { }); }); + beforeEach(() => { + mock = new MockAdapter(axios); + }); + afterEach(() => { ['requestAnimationFrame', 'requestIdleCallback'].forEach((method) => { global[method] = originalMethods[method]; }); createFlash.mockClear(); + mock.restore(); }); describe('setBaseConfig', () => { - it('should set given endpoint and project path', (done) => { + it('should set given endpoint and project path', () => { const endpoint = '/diffs/set/endpoint'; const endpointMetadata = '/diffs/set/endpoint/metadata'; const endpointBatch = '/diffs/set/endpoint/batch'; @@ -104,8 +72,8 @@ describe('DiffsStoreActions', () => { b: ['y', 'hash:a'], }; - testAction( - setBaseConfig, + return testAction( + diffActions.setBaseConfig, { endpoint, endpointBatch, @@ -153,23 +121,12 @@ describe('DiffsStoreActions', () => { }, ], [], - done, ); }); }); describe('fetchDiffFilesBatch', () => { - let mock; - - beforeEach(() => { - mock = new MockAdapter(axios); - }); - - afterEach(() => { - mock.restore(); - }); - - it('should fetch batch diff files', (done) => { + it('should fetch batch diff files', () => { const endpointBatch = '/fetch/diffs_batch'; const res1 = { diff_files: [{ file_hash: 'test' }], pagination: { total_pages: 7 } }; const res2 = { diff_files: [{ file_hash: 'test2' }], pagination: { total_pages: 7 } }; @@ -199,8 +156,8 @@ describe('DiffsStoreActions', () => { ) .reply(200, res2); - testAction( - fetchDiffFilesBatch, + return testAction( + diffActions.fetchDiffFilesBatch, {}, { endpointBatch, diffViewType: 'inline', diffFiles: [] }, [ @@ -216,7 +173,6 @@ describe('DiffsStoreActions', () => { { type: types.SET_BATCH_LOADING_STATE, payload: 'error' }, ], [{ type: 'startRenderDiffsQueue' }, { type: 'startRenderDiffsQueue' }], - done, ); }); @@ -229,13 +185,14 @@ describe('DiffsStoreActions', () => { ({ viewStyle, otherView }) => { const endpointBatch = '/fetch/diffs_batch'; - fetchDiffFilesBatch({ - commit: () => {}, - state: { - endpointBatch: `${endpointBatch}?view=${otherView}`, - diffViewType: viewStyle, - }, - }) + diffActions + .fetchDiffFilesBatch({ + commit: () => {}, + state: { + endpointBatch: `${endpointBatch}?view=${otherView}`, + diffViewType: viewStyle, + }, + }) .then(() => { expect(mock.history.get[0].url).toContain(`view=${viewStyle}`); expect(mock.history.get[0].url).not.toContain(`view=${otherView}`); @@ -248,19 +205,16 @@ describe('DiffsStoreActions', () => { describe('fetchDiffFilesMeta', () => { const endpointMetadata = '/fetch/diffs_metadata.json?view=inline'; const noFilesData = { ...diffMetadata }; - let mock; beforeEach(() => { - mock = new MockAdapter(axios); - delete noFilesData.diff_files; mock.onGet(endpointMetadata).reply(200, diffMetadata); }); - it('should fetch diff meta information', (done) => { - testAction( - fetchDiffFilesMeta, + it('should fetch diff meta information', () => { + return testAction( + diffActions.fetchDiffFilesMeta, {}, { endpointMetadata, diffViewType: 'inline' }, [ @@ -275,55 +229,41 @@ describe('DiffsStoreActions', () => { }, ], [], - () => { - mock.restore(); - done(); - }, ); }); }); describe('fetchCoverageFiles', () => { - let mock; const endpointCoverage = '/fetch'; - beforeEach(() => { - mock = new MockAdapter(axios); - }); - - afterEach(() => mock.restore()); - - it('should commit SET_COVERAGE_DATA with received response', (done) => { + it('should commit SET_COVERAGE_DATA with received response', () => { const data = { files: { 'app.js': { 1: 0, 2: 1 } } }; mock.onGet(endpointCoverage).reply(200, { data }); - testAction( - fetchCoverageFiles, + return testAction( + diffActions.fetchCoverageFiles, {}, { endpointCoverage }, [{ type: types.SET_COVERAGE_DATA, payload: { data } }], [], - done, ); }); - it('should show flash on API error', (done) => { + it('should show flash on API error', async () => { mock.onGet(endpointCoverage).reply(400); - testAction(fetchCoverageFiles, {}, { endpointCoverage }, [], [], () => { - expect(createFlash).toHaveBeenCalledTimes(1); - expect(createFlash).toHaveBeenCalledWith({ - message: expect.stringMatching('Something went wrong'), - }); - done(); + await testAction(diffActions.fetchCoverageFiles, {}, { endpointCoverage }, [], []); + expect(createFlash).toHaveBeenCalledTimes(1); + expect(createFlash).toHaveBeenCalledWith({ + message: expect.stringMatching('Something went wrong'), }); }); }); describe('setHighlightedRow', () => { it('should mark currently selected diff and set lineHash and fileHash of highlightedRow', () => { - testAction(setHighlightedRow, 'ABC_123', {}, [ + return testAction(diffActions.setHighlightedRow, 'ABC_123', {}, [ { type: types.SET_HIGHLIGHTED_ROW, payload: 'ABC_123' }, { type: types.SET_CURRENT_DIFF_FILE, payload: 'ABC' }, ]); @@ -335,7 +275,7 @@ describe('DiffsStoreActions', () => { window.location.hash = ''; }); - it('should merge discussions into diffs', (done) => { + it('should merge discussions into diffs', () => { window.location.hash = 'ABC_123'; const state = { @@ -397,8 +337,8 @@ describe('DiffsStoreActions', () => { const discussions = [singleDiscussion]; - testAction( - assignDiscussionsToDiff, + return testAction( + diffActions.assignDiscussionsToDiff, discussions, state, [ @@ -425,26 +365,24 @@ describe('DiffsStoreActions', () => { }, ], [], - done, ); }); - it('dispatches setCurrentDiffFileIdFromNote with note ID', (done) => { + it('dispatches setCurrentDiffFileIdFromNote with note ID', () => { window.location.hash = 'note_123'; - testAction( - assignDiscussionsToDiff, + return testAction( + diffActions.assignDiscussionsToDiff, [], { diffFiles: [] }, [], [{ type: 'setCurrentDiffFileIdFromNote', payload: '123' }], - done, ); }); }); describe('removeDiscussionsFromDiff', () => { - it('should remove discussions from diffs', (done) => { + it('should remove discussions from diffs', () => { const state = { diffFiles: [ { @@ -480,8 +418,8 @@ describe('DiffsStoreActions', () => { line_code: 'ABC_1_1', }; - testAction( - removeDiscussionsFromDiff, + return testAction( + diffActions.removeDiscussionsFromDiff, singleDiscussion, state, [ @@ -495,7 +433,6 @@ describe('DiffsStoreActions', () => { }, ], [], - done, ); }); }); @@ -528,7 +465,7 @@ describe('DiffsStoreActions', () => { }); }; - startRenderDiffsQueue({ state, commit: pseudoCommit }); + diffActions.startRenderDiffsQueue({ state, commit: pseudoCommit }); expect(state.diffFiles[0].renderIt).toBe(true); expect(state.diffFiles[1].renderIt).toBe(true); @@ -536,69 +473,61 @@ describe('DiffsStoreActions', () => { }); describe('setInlineDiffViewType', () => { - it('should set diff view type to inline and also set the cookie properly', (done) => { - testAction( - setInlineDiffViewType, + it('should set diff view type to inline and also set the cookie properly', async () => { + await testAction( + diffActions.setInlineDiffViewType, null, {}, [{ type: types.SET_DIFF_VIEW_TYPE, payload: INLINE_DIFF_VIEW_TYPE }], [], - () => { - expect(Cookies.get('diff_view')).toEqual(INLINE_DIFF_VIEW_TYPE); - done(); - }, ); + expect(Cookies.get('diff_view')).toEqual(INLINE_DIFF_VIEW_TYPE); }); }); describe('setParallelDiffViewType', () => { - it('should set diff view type to parallel and also set the cookie properly', (done) => { - testAction( - setParallelDiffViewType, + it('should set diff view type to parallel and also set the cookie properly', async () => { + await testAction( + diffActions.setParallelDiffViewType, null, {}, [{ type: types.SET_DIFF_VIEW_TYPE, payload: PARALLEL_DIFF_VIEW_TYPE }], [], - () => { - expect(Cookies.get(DIFF_VIEW_COOKIE_NAME)).toEqual(PARALLEL_DIFF_VIEW_TYPE); - done(); - }, ); + expect(Cookies.get(DIFF_VIEW_COOKIE_NAME)).toEqual(PARALLEL_DIFF_VIEW_TYPE); }); }); describe('showCommentForm', () => { - it('should call mutation to show comment form', (done) => { + it('should call mutation to show comment form', () => { const payload = { lineCode: 'lineCode', fileHash: 'hash' }; - testAction( - showCommentForm, + return testAction( + diffActions.showCommentForm, payload, {}, [{ type: types.TOGGLE_LINE_HAS_FORM, payload: { ...payload, hasForm: true } }], [], - done, ); }); }); describe('cancelCommentForm', () => { - it('should call mutation to cancel comment form', (done) => { + it('should call mutation to cancel comment form', () => { const payload = { lineCode: 'lineCode', fileHash: 'hash' }; - testAction( - cancelCommentForm, + return testAction( + diffActions.cancelCommentForm, payload, {}, [{ type: types.TOGGLE_LINE_HAS_FORM, payload: { ...payload, hasForm: false } }], [], - done, ); }); }); describe('loadMoreLines', () => { - it('should call mutation to show comment form', (done) => { + it('should call mutation to show comment form', () => { const endpoint = '/diffs/load/more/lines'; const params = { since: 6, to: 26 }; const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; @@ -606,12 +535,11 @@ describe('DiffsStoreActions', () => { const isExpandDown = false; const nextLineNumbers = {}; const options = { endpoint, params, lineNumbers, fileHash, isExpandDown, nextLineNumbers }; - const mock = new MockAdapter(axios); const contextLines = { contextLines: [{ lineCode: 6 }] }; mock.onGet(endpoint).reply(200, contextLines); - testAction( - loadMoreLines, + return testAction( + diffActions.loadMoreLines, options, {}, [ @@ -621,31 +549,23 @@ describe('DiffsStoreActions', () => { }, ], [], - () => { - mock.restore(); - done(); - }, ); }); }); describe('loadCollapsedDiff', () => { const state = { showWhitespace: true }; - it('should fetch data and call mutation with response and the give parameter', (done) => { + it('should fetch data and call mutation with response and the give parameter', () => { const file = { hash: 123, load_collapsed_diff_url: '/load/collapsed/diff/url' }; const data = { hash: 123, parallelDiffLines: [{ lineCode: 1 }] }; - const mock = new MockAdapter(axios); const commit = jest.fn(); mock.onGet(file.loadCollapsedDiffUrl).reply(200, data); - loadCollapsedDiff({ commit, getters: { commitId: null }, state }, file) + return diffActions + .loadCollapsedDiff({ commit, getters: { commitId: null }, state }, file) .then(() => { expect(commit).toHaveBeenCalledWith(types.ADD_COLLAPSED_DIFFS, { file, data }); - - mock.restore(); - done(); - }) - .catch(done.fail); + }); }); it('should fetch data without commit ID', () => { @@ -656,7 +576,7 @@ describe('DiffsStoreActions', () => { jest.spyOn(axios, 'get').mockReturnValue(Promise.resolve({ data: {} })); - loadCollapsedDiff({ commit() {}, getters, state }, file); + diffActions.loadCollapsedDiff({ commit() {}, getters, state }, file); expect(axios.get).toHaveBeenCalledWith(file.load_collapsed_diff_url, { params: { commit_id: null, w: '0' }, @@ -671,7 +591,7 @@ describe('DiffsStoreActions', () => { jest.spyOn(axios, 'get').mockReturnValue(Promise.resolve({ data: {} })); - loadCollapsedDiff({ commit() {}, getters, state }, file); + diffActions.loadCollapsedDiff({ commit() {}, getters, state }, file); expect(axios.get).toHaveBeenCalledWith(file.load_collapsed_diff_url, { params: { commit_id: '123', w: '0' }, @@ -689,7 +609,7 @@ describe('DiffsStoreActions', () => { const dispatch = jest.fn(); - toggleFileDiscussions({ getters, dispatch }); + diffActions.toggleFileDiscussions({ getters, dispatch }); expect(dispatch).toHaveBeenCalledWith( 'collapseDiscussion', @@ -707,7 +627,7 @@ describe('DiffsStoreActions', () => { const dispatch = jest.fn(); - toggleFileDiscussions({ getters, dispatch }); + diffActions.toggleFileDiscussions({ getters, dispatch }); expect(dispatch).toHaveBeenCalledWith( 'expandDiscussion', @@ -725,7 +645,7 @@ describe('DiffsStoreActions', () => { const dispatch = jest.fn(); - toggleFileDiscussions({ getters, dispatch }); + diffActions.toggleFileDiscussions({ getters, dispatch }); expect(dispatch).toHaveBeenCalledWith( 'expandDiscussion', @@ -743,7 +663,7 @@ describe('DiffsStoreActions', () => { it('should not call handleLocationHash when there is not hash', () => { window.location.hash = ''; - scrollToLineIfNeededInline({}, lineMock); + diffActions.scrollToLineIfNeededInline({}, lineMock); expect(commonUtils.handleLocationHash).not.toHaveBeenCalled(); }); @@ -751,7 +671,7 @@ describe('DiffsStoreActions', () => { it('should not call handleLocationHash when the hash does not match any line', () => { window.location.hash = 'XYZ_456'; - scrollToLineIfNeededInline({}, lineMock); + diffActions.scrollToLineIfNeededInline({}, lineMock); expect(commonUtils.handleLocationHash).not.toHaveBeenCalled(); }); @@ -759,14 +679,14 @@ describe('DiffsStoreActions', () => { it('should call handleLocationHash only when the hash matches a line', () => { window.location.hash = 'ABC_123'; - scrollToLineIfNeededInline( + diffActions.scrollToLineIfNeededInline( {}, { lineCode: 'ABC_456', }, ); - scrollToLineIfNeededInline({}, lineMock); - scrollToLineIfNeededInline( + diffActions.scrollToLineIfNeededInline({}, lineMock); + diffActions.scrollToLineIfNeededInline( {}, { lineCode: 'XYZ_456', @@ -789,7 +709,7 @@ describe('DiffsStoreActions', () => { it('should not call handleLocationHash when there is not hash', () => { window.location.hash = ''; - scrollToLineIfNeededParallel({}, lineMock); + diffActions.scrollToLineIfNeededParallel({}, lineMock); expect(commonUtils.handleLocationHash).not.toHaveBeenCalled(); }); @@ -797,7 +717,7 @@ describe('DiffsStoreActions', () => { it('should not call handleLocationHash when the hash does not match any line', () => { window.location.hash = 'XYZ_456'; - scrollToLineIfNeededParallel({}, lineMock); + diffActions.scrollToLineIfNeededParallel({}, lineMock); expect(commonUtils.handleLocationHash).not.toHaveBeenCalled(); }); @@ -805,7 +725,7 @@ describe('DiffsStoreActions', () => { it('should call handleLocationHash only when the hash matches a line', () => { window.location.hash = 'ABC_123'; - scrollToLineIfNeededParallel( + diffActions.scrollToLineIfNeededParallel( {}, { left: null, @@ -814,8 +734,8 @@ describe('DiffsStoreActions', () => { }, }, ); - scrollToLineIfNeededParallel({}, lineMock); - scrollToLineIfNeededParallel( + diffActions.scrollToLineIfNeededParallel({}, lineMock); + diffActions.scrollToLineIfNeededParallel( {}, { left: null, @@ -831,7 +751,7 @@ describe('DiffsStoreActions', () => { }); describe('saveDiffDiscussion', () => { - it('dispatches actions', (done) => { + it('dispatches actions', () => { const commitId = 'something'; const formData = { diffFile: { ...mockDiffFile }, @@ -856,33 +776,29 @@ describe('DiffsStoreActions', () => { } }); - saveDiffDiscussion({ state, dispatch }, { note, formData }) - .then(() => { - expect(dispatch).toHaveBeenCalledTimes(5); - expect(dispatch).toHaveBeenNthCalledWith(1, 'saveNote', expect.any(Object), { - root: true, - }); + return diffActions.saveDiffDiscussion({ state, dispatch }, { note, formData }).then(() => { + expect(dispatch).toHaveBeenCalledTimes(5); + expect(dispatch).toHaveBeenNthCalledWith(1, 'saveNote', expect.any(Object), { + root: true, + }); - const postData = dispatch.mock.calls[0][1]; - expect(postData.data.note.commit_id).toBe(commitId); + const postData = dispatch.mock.calls[0][1]; + expect(postData.data.note.commit_id).toBe(commitId); - expect(dispatch).toHaveBeenNthCalledWith(2, 'updateDiscussion', 'test', { root: true }); - expect(dispatch).toHaveBeenNthCalledWith(3, 'assignDiscussionsToDiff', ['discussion']); - }) - .then(done) - .catch(done.fail); + expect(dispatch).toHaveBeenNthCalledWith(2, 'updateDiscussion', 'test', { root: true }); + expect(dispatch).toHaveBeenNthCalledWith(3, 'assignDiscussionsToDiff', ['discussion']); + }); }); }); describe('toggleTreeOpen', () => { - it('commits TOGGLE_FOLDER_OPEN', (done) => { - testAction( - toggleTreeOpen, + it('commits TOGGLE_FOLDER_OPEN', () => { + return testAction( + diffActions.toggleTreeOpen, 'path', {}, [{ type: types.TOGGLE_FOLDER_OPEN, payload: 'path' }], [], - done, ); }); }); @@ -904,7 +820,7 @@ describe('DiffsStoreActions', () => { }, }; - scrollToFile({ state, commit, getters }, { path: 'path' }); + diffActions.scrollToFile({ state, commit, getters }, { path: 'path' }); expect(document.location.hash).toBe('#test'); }); @@ -918,28 +834,27 @@ describe('DiffsStoreActions', () => { }, }; - scrollToFile({ state, commit, getters }, { path: 'path' }); + diffActions.scrollToFile({ state, commit, getters }, { path: 'path' }); expect(commit).toHaveBeenCalledWith(types.SET_CURRENT_DIFF_FILE, 'test'); }); }); describe('setShowTreeList', () => { - it('commits toggle', (done) => { - testAction( - setShowTreeList, + it('commits toggle', () => { + return testAction( + diffActions.setShowTreeList, { showTreeList: true }, {}, [{ type: types.SET_SHOW_TREE_LIST, payload: true }], [], - done, ); }); it('updates localStorage', () => { jest.spyOn(localStorage, 'setItem').mockImplementation(() => {}); - setShowTreeList({ commit() {} }, { showTreeList: true }); + diffActions.setShowTreeList({ commit() {} }, { showTreeList: true }); expect(localStorage.setItem).toHaveBeenCalledWith('mr_tree_show', true); }); @@ -947,7 +862,7 @@ describe('DiffsStoreActions', () => { it('does not update localStorage', () => { jest.spyOn(localStorage, 'setItem').mockImplementation(() => {}); - setShowTreeList({ commit() {} }, { showTreeList: true, saving: false }); + diffActions.setShowTreeList({ commit() {} }, { showTreeList: true, saving: false }); expect(localStorage.setItem).not.toHaveBeenCalled(); }); @@ -994,7 +909,7 @@ describe('DiffsStoreActions', () => { it('renders and expands file for the given discussion id', () => { const localState = state({ collapsed: true, renderIt: false }); - renderFileForDiscussionId({ rootState, state: localState, commit }, '123'); + diffActions.renderFileForDiscussionId({ rootState, state: localState, commit }, '123'); expect(commit).toHaveBeenCalledWith('RENDER_FILE', localState.diffFiles[0]); expect($emit).toHaveBeenCalledTimes(1); @@ -1004,7 +919,7 @@ describe('DiffsStoreActions', () => { it('jumps to discussion on already rendered and expanded file', () => { const localState = state({ collapsed: false, renderIt: true }); - renderFileForDiscussionId({ rootState, state: localState, commit }, '123'); + diffActions.renderFileForDiscussionId({ rootState, state: localState, commit }, '123'); expect(commit).not.toHaveBeenCalled(); expect($emit).toHaveBeenCalledTimes(1); @@ -1013,19 +928,18 @@ describe('DiffsStoreActions', () => { }); describe('setRenderTreeList', () => { - it('commits SET_RENDER_TREE_LIST', (done) => { - testAction( - setRenderTreeList, + it('commits SET_RENDER_TREE_LIST', () => { + return testAction( + diffActions.setRenderTreeList, { renderTreeList: true }, {}, [{ type: types.SET_RENDER_TREE_LIST, payload: true }], [], - done, ); }); it('sets localStorage', () => { - setRenderTreeList({ commit() {} }, { renderTreeList: true }); + diffActions.setRenderTreeList({ commit() {} }, { renderTreeList: true }); expect(localStorage.setItem).toHaveBeenCalledWith('mr_diff_tree_list', true); }); @@ -1034,11 +948,9 @@ describe('DiffsStoreActions', () => { describe('setShowWhitespace', () => { const endpointUpdateUser = 'user/prefs'; let putSpy; - let mock; let gon; beforeEach(() => { - mock = new MockAdapter(axios); putSpy = jest.spyOn(axios, 'put'); gon = window.gon; @@ -1047,25 +959,23 @@ describe('DiffsStoreActions', () => { }); afterEach(() => { - mock.restore(); window.gon = gon; }); - it('commits SET_SHOW_WHITESPACE', (done) => { - testAction( - setShowWhitespace, + it('commits SET_SHOW_WHITESPACE', () => { + return testAction( + diffActions.setShowWhitespace, { showWhitespace: true, updateDatabase: false }, {}, [{ type: types.SET_SHOW_WHITESPACE, payload: true }], [], - done, ); }); it('saves to the database when the user is logged in', async () => { window.gon = { current_user_id: 12345 }; - await setShowWhitespace( + await diffActions.setShowWhitespace( { state: { endpointUpdateUser }, commit() {} }, { showWhitespace: true, updateDatabase: true }, ); @@ -1076,7 +986,7 @@ describe('DiffsStoreActions', () => { it('does not try to save to the API if the user is not logged in', async () => { window.gon = {}; - await setShowWhitespace( + await diffActions.setShowWhitespace( { state: { endpointUpdateUser }, commit() {} }, { showWhitespace: true, updateDatabase: true }, ); @@ -1085,7 +995,7 @@ describe('DiffsStoreActions', () => { }); it('emits eventHub event', async () => { - await setShowWhitespace( + await diffActions.setShowWhitespace( { state: {}, commit() {} }, { showWhitespace: true, updateDatabase: false }, ); @@ -1095,53 +1005,47 @@ describe('DiffsStoreActions', () => { }); describe('setRenderIt', () => { - it('commits RENDER_FILE', (done) => { - testAction(setRenderIt, 'file', {}, [{ type: types.RENDER_FILE, payload: 'file' }], [], done); + it('commits RENDER_FILE', () => { + return testAction( + diffActions.setRenderIt, + 'file', + {}, + [{ type: types.RENDER_FILE, payload: 'file' }], + [], + ); }); }); describe('receiveFullDiffError', () => { - it('updates state with the file that did not load', (done) => { - testAction( - receiveFullDiffError, + it('updates state with the file that did not load', () => { + return testAction( + diffActions.receiveFullDiffError, 'file', {}, [{ type: types.RECEIVE_FULL_DIFF_ERROR, payload: 'file' }], [], - done, ); }); }); describe('fetchFullDiff', () => { - let mock; - - beforeEach(() => { - mock = new MockAdapter(axios); - }); - - afterEach(() => { - mock.restore(); - }); - describe('success', () => { beforeEach(() => { mock.onGet(`${TEST_HOST}/context`).replyOnce(200, ['test']); }); - it('commits the success and dispatches an action to expand the new lines', (done) => { + it('commits the success and dispatches an action to expand the new lines', () => { const file = { context_lines_path: `${TEST_HOST}/context`, file_path: 'test', file_hash: 'test', }; - testAction( - fetchFullDiff, + return testAction( + diffActions.fetchFullDiff, file, null, [{ type: types.RECEIVE_FULL_DIFF_SUCCESS, payload: { filePath: 'test' } }], [{ type: 'setExpandedDiffLines', payload: { file, data: ['test'] } }], - done, ); }); }); @@ -1151,14 +1055,13 @@ describe('DiffsStoreActions', () => { mock.onGet(`${TEST_HOST}/context`).replyOnce(500); }); - it('dispatches receiveFullDiffError', (done) => { - testAction( - fetchFullDiff, + it('dispatches receiveFullDiffError', () => { + return testAction( + diffActions.fetchFullDiff, { context_lines_path: `${TEST_HOST}/context`, file_path: 'test', file_hash: 'test' }, null, [], [{ type: 'receiveFullDiffError', payload: 'test' }], - done, ); }); }); @@ -1173,14 +1076,13 @@ describe('DiffsStoreActions', () => { }; }); - it('dispatches fetchFullDiff when file is not expanded', (done) => { - testAction( - toggleFullDiff, + it('dispatches fetchFullDiff when file is not expanded', () => { + return testAction( + diffActions.toggleFullDiff, 'test', state, [{ type: types.REQUEST_FULL_DIFF, payload: 'test' }], [{ type: 'fetchFullDiff', payload: state.diffFiles[0] }], - done, ); }); }); @@ -1202,16 +1104,13 @@ describe('DiffsStoreActions', () => { }; const testData = [{ rich_text: 'test' }, { rich_text: 'file2' }]; let renamedFile; - let mock; beforeEach(() => { - mock = new MockAdapter(axios); jest.spyOn(utils, 'prepareLineForRenamedFile').mockImplementation(() => preparedLine); }); afterEach(() => { renamedFile = null; - mock.restore(); }); describe('success', () => { @@ -1228,7 +1127,7 @@ describe('DiffsStoreActions', () => { 'performs the correct mutations and starts a render queue for view type $diffViewType', ({ diffViewType }) => { return testAction( - switchToFullDiffFromRenamedFile, + diffActions.switchToFullDiffFromRenamedFile, { diffFile: renamedFile }, { diffViewType }, [ @@ -1249,9 +1148,9 @@ describe('DiffsStoreActions', () => { }); describe('setFileUserCollapsed', () => { - it('commits SET_FILE_COLLAPSED', (done) => { - testAction( - setFileCollapsedByUser, + it('commits SET_FILE_COLLAPSED', () => { + return testAction( + diffActions.setFileCollapsedByUser, { filePath: 'test', collapsed: true }, null, [ @@ -1261,7 +1160,6 @@ describe('DiffsStoreActions', () => { }, ], [], - done, ); }); }); @@ -1273,11 +1171,11 @@ describe('DiffsStoreActions', () => { }); }); - it('commits SET_CURRENT_VIEW_DIFF_FILE_LINES when lines less than MAX_RENDERING_DIFF_LINES', (done) => { + it('commits SET_CURRENT_VIEW_DIFF_FILE_LINES when lines less than MAX_RENDERING_DIFF_LINES', () => { utils.convertExpandLines.mockImplementation(() => ['test']); - testAction( - setExpandedDiffLines, + return testAction( + diffActions.setExpandedDiffLines, { file: { file_path: 'path' }, data: [] }, { diffViewType: 'inline' }, [ @@ -1287,16 +1185,15 @@ describe('DiffsStoreActions', () => { }, ], [], - done, ); }); - it('commits ADD_CURRENT_VIEW_DIFF_FILE_LINES when lines more than MAX_RENDERING_DIFF_LINES', (done) => { + it('commits ADD_CURRENT_VIEW_DIFF_FILE_LINES when lines more than MAX_RENDERING_DIFF_LINES', () => { const lines = new Array(501).fill().map((_, i) => `line-${i}`); utils.convertExpandLines.mockReturnValue(lines); - testAction( - setExpandedDiffLines, + return testAction( + diffActions.setExpandedDiffLines, { file: { file_path: 'path' }, data: [] }, { diffViewType: 'inline' }, [ @@ -1312,41 +1209,34 @@ describe('DiffsStoreActions', () => { { type: 'TOGGLE_DIFF_FILE_RENDERING_MORE', payload: 'path' }, ], [], - done, ); }); }); describe('setSuggestPopoverDismissed', () => { - it('commits SET_SHOW_SUGGEST_POPOVER', (done) => { + it('commits SET_SHOW_SUGGEST_POPOVER', async () => { const state = { dismissEndpoint: `${TEST_HOST}/-/user_callouts` }; - const mock = new MockAdapter(axios); mock.onPost(state.dismissEndpoint).reply(200, {}); jest.spyOn(axios, 'post'); - testAction( - setSuggestPopoverDismissed, + await testAction( + diffActions.setSuggestPopoverDismissed, null, state, [{ type: types.SET_SHOW_SUGGEST_POPOVER }], [], - () => { - expect(axios.post).toHaveBeenCalledWith(state.dismissEndpoint, { - feature_name: 'suggest_popover_dismissed', - }); - - mock.restore(); - done(); - }, ); + expect(axios.post).toHaveBeenCalledWith(state.dismissEndpoint, { + feature_name: 'suggest_popover_dismissed', + }); }); }); describe('changeCurrentCommit', () => { it('commits the new commit information and re-requests the diff metadata for the commit', () => { return testAction( - changeCurrentCommit, + diffActions.changeCurrentCommit, { commitId: 'NEW' }, { commit: { @@ -1384,7 +1274,7 @@ describe('DiffsStoreActions', () => { ({ commitId, commit, msg }) => { const err = new Error(msg); const actionReturn = testAction( - changeCurrentCommit, + diffActions.changeCurrentCommit, { commitId }, { endpoint: 'URL/OLD', @@ -1410,7 +1300,7 @@ describe('DiffsStoreActions', () => { 'for the direction "$direction", dispatches the action to move to the SHA "$expected"', ({ direction, expected, currentCommit }) => { return testAction( - moveToNeighboringCommit, + diffActions.moveToNeighboringCommit, { direction }, { commit: currentCommit }, [], @@ -1431,7 +1321,7 @@ describe('DiffsStoreActions', () => { 'given `{ "isloading": $diffsAreLoading, "commit": $currentCommit }` in state, no actions are dispatched', ({ direction, diffsAreLoading, currentCommit }) => { return testAction( - moveToNeighboringCommit, + diffActions.moveToNeighboringCommit, { direction }, { commit: currentCommit, isLoading: diffsAreLoading }, [], @@ -1450,7 +1340,7 @@ describe('DiffsStoreActions', () => { notesById: { 1: { discussion_id: '2' } }, }; - setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); + diffActions.setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); expect(commit).toHaveBeenCalledWith(types.SET_CURRENT_DIFF_FILE, '123'); }); @@ -1463,7 +1353,7 @@ describe('DiffsStoreActions', () => { notesById: { 1: { discussion_id: '2' } }, }; - setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); + diffActions.setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); expect(commit).not.toHaveBeenCalled(); }); @@ -1476,21 +1366,20 @@ describe('DiffsStoreActions', () => { notesById: { 1: { discussion_id: '2' } }, }; - setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); + diffActions.setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); expect(commit).not.toHaveBeenCalled(); }); }); describe('navigateToDiffFileIndex', () => { - it('commits SET_CURRENT_DIFF_FILE', (done) => { - testAction( - navigateToDiffFileIndex, + it('commits SET_CURRENT_DIFF_FILE', () => { + return testAction( + diffActions.navigateToDiffFileIndex, 0, { diffFiles: [{ file_hash: '123' }] }, [{ type: types.SET_CURRENT_DIFF_FILE, payload: '123' }], [], - done, ); }); }); @@ -1498,19 +1387,13 @@ describe('DiffsStoreActions', () => { describe('setFileByFile', () => { const updateUserEndpoint = 'user/prefs'; let putSpy; - let mock; beforeEach(() => { - mock = new MockAdapter(axios); putSpy = jest.spyOn(axios, 'put'); mock.onPut(updateUserEndpoint).reply(200, {}); }); - afterEach(() => { - mock.restore(); - }); - it.each` value ${true} @@ -1519,7 +1402,7 @@ describe('DiffsStoreActions', () => { 'commits SET_FILE_BY_FILE and persists the File-by-File user preference with the new value $value', async ({ value }) => { await testAction( - setFileByFile, + diffActions.setFileByFile, { fileByFile: value }, { viewDiffsFileByFile: null, @@ -1551,7 +1434,7 @@ describe('DiffsStoreActions', () => { const commitSpy = jest.fn(); const getterSpy = jest.fn().mockReturnValue([]); - reviewFile( + diffActions.reviewFile( { commit: commitSpy, getters: { |