diff options
Diffstat (limited to 'spec/frontend/diffs/store/actions_spec.js')
-rw-r--r-- | spec/frontend/diffs/store/actions_spec.js | 123 |
1 files changed, 83 insertions, 40 deletions
diff --git a/spec/frontend/diffs/store/actions_spec.js b/spec/frontend/diffs/store/actions_spec.js index fef7676e795..056ac23fcf7 100644 --- a/spec/frontend/diffs/store/actions_spec.js +++ b/spec/frontend/diffs/store/actions_spec.js @@ -2,7 +2,8 @@ import MockAdapter from 'axios-mock-adapter'; import Cookies from 'js-cookie'; import mockDiffFile from 'jest/diffs/mock_data/diff_file'; import { useLocalStorageSpy } from 'helpers/local_storage_helper'; -import { TEST_HOST } from 'jest/helpers/test_constants'; +import { TEST_HOST } from 'helpers/test_constants'; +import testAction from 'helpers/vuex_action_helper'; import { DIFF_VIEW_COOKIE_NAME, INLINE_DIFF_VIEW_TYPE, @@ -49,11 +50,11 @@ import { setCurrentDiffFileIdFromNote, navigateToDiffFileIndex, setFileByFile, + reviewFile, } from '~/diffs/store/actions'; import eventHub from '~/notes/event_hub'; import * as types from '~/diffs/store/mutation_types'; import axios from '~/lib/utils/axios_utils'; -import testAction from '../../helpers/vuex_action_helper'; import * as utils from '~/diffs/store/utils'; import * as commonUtils from '~/lib/utils/common_utils'; import { mergeUrlParams } from '~/lib/utils/url_utility'; @@ -77,22 +78,22 @@ describe('DiffsStoreActions', () => { jest.spyOn(commonUtils, 'scrollToElement').mockImplementation(() => null); jest.spyOn(utils, 'convertExpandLines').mockImplementation(() => null); jest.spyOn(utils, 'idleCallback').mockImplementation(() => null); - ['requestAnimationFrame', 'requestIdleCallback'].forEach(method => { - global[method] = cb => { + ['requestAnimationFrame', 'requestIdleCallback'].forEach((method) => { + global[method] = (cb) => { cb(); }; }); }); afterEach(() => { - ['requestAnimationFrame', 'requestIdleCallback'].forEach(method => { + ['requestAnimationFrame', 'requestIdleCallback'].forEach((method) => { global[method] = originalMethods[method]; }); createFlash.mockClear(); }); describe('setBaseConfig', () => { - it('should set given endpoint and project path', done => { + it('should set given endpoint and project path', (done) => { const endpoint = '/diffs/set/endpoint'; const endpointMetadata = '/diffs/set/endpoint/metadata'; const endpointBatch = '/diffs/set/endpoint/batch'; @@ -152,7 +153,7 @@ describe('DiffsStoreActions', () => { mock.restore(); }); - it('should fetch batch diff files', done => { + it('should fetch batch diff files', (done) => { const endpointBatch = '/fetch/diffs_batch'; const res1 = { diff_files: [{ file_hash: 'test' }], pagination: { next_page: 2 } }; const res2 = { diff_files: [{ file_hash: 'test2' }], pagination: {} }; @@ -240,7 +241,7 @@ describe('DiffsStoreActions', () => { mock.onGet(endpointMetadata).reply(200, diffMetadata); }); - it('should fetch diff meta information', done => { + it('should fetch diff meta information', (done) => { testAction( fetchDiffFilesMeta, {}, @@ -270,8 +271,8 @@ describe('DiffsStoreActions', () => { afterEach(() => mock.restore()); - it('should commit SET_COVERAGE_DATA with received response', done => { - const data = { files: { 'app.js': { '1': 0, '2': 1 } } }; + it('should commit SET_COVERAGE_DATA with received response', (done) => { + const data = { files: { 'app.js': { 1: 0, 2: 1 } } }; mock.onGet(endpointCoverage).reply(200, { data }); @@ -285,7 +286,7 @@ describe('DiffsStoreActions', () => { ); }); - it('should show flash on API error', done => { + it('should show flash on API error', (done) => { mock.onGet(endpointCoverage).reply(400); testAction(fetchCoverageFiles, {}, { endpointCoverage }, [], [], () => { @@ -310,7 +311,7 @@ describe('DiffsStoreActions', () => { window.location.hash = ''; }); - it('should merge discussions into diffs', done => { + it('should merge discussions into diffs', (done) => { window.location.hash = 'ABC_123'; const state = { @@ -404,7 +405,7 @@ describe('DiffsStoreActions', () => { ); }); - it('dispatches setCurrentDiffFileIdFromNote with note ID', done => { + it('dispatches setCurrentDiffFileIdFromNote with note ID', (done) => { window.location.hash = 'note_123'; testAction( @@ -419,7 +420,7 @@ describe('DiffsStoreActions', () => { }); describe('removeDiscussionsFromDiff', () => { - it('should remove discussions from diffs', done => { + it('should remove discussions from diffs', (done) => { const state = { diffFiles: [ { @@ -511,7 +512,7 @@ describe('DiffsStoreActions', () => { }); describe('setInlineDiffViewType', () => { - it('should set diff view type to inline and also set the cookie properly', done => { + it('should set diff view type to inline and also set the cookie properly', (done) => { testAction( setInlineDiffViewType, null, @@ -529,7 +530,7 @@ describe('DiffsStoreActions', () => { }); describe('setParallelDiffViewType', () => { - it('should set diff view type to parallel and also set the cookie properly', done => { + it('should set diff view type to parallel and also set the cookie properly', (done) => { testAction( setParallelDiffViewType, null, @@ -547,7 +548,7 @@ describe('DiffsStoreActions', () => { }); describe('showCommentForm', () => { - it('should call mutation to show comment form', done => { + it('should call mutation to show comment form', (done) => { const payload = { lineCode: 'lineCode', fileHash: 'hash' }; testAction( @@ -562,7 +563,7 @@ describe('DiffsStoreActions', () => { }); describe('cancelCommentForm', () => { - it('should call mutation to cancel comment form', done => { + it('should call mutation to cancel comment form', (done) => { const payload = { lineCode: 'lineCode', fileHash: 'hash' }; testAction( @@ -577,7 +578,7 @@ describe('DiffsStoreActions', () => { }); describe('loadMoreLines', () => { - it('should call mutation to show comment form', done => { + it('should call mutation to show comment form', (done) => { const endpoint = '/diffs/load/more/lines'; const params = { since: 6, to: 26 }; const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; @@ -610,7 +611,7 @@ describe('DiffsStoreActions', () => { 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', (done) => { const file = { hash: 123, load_collapsed_diff_url: '/load/collapsed/diff/url' }; const data = { hash: 123, parallelDiffLines: [{ lineCode: 1 }] }; const mock = new MockAdapter(axios); @@ -810,7 +811,7 @@ describe('DiffsStoreActions', () => { }); describe('saveDiffDiscussion', () => { - it('dispatches actions', done => { + it('dispatches actions', (done) => { const commitId = 'something'; const formData = { diffFile: { ...mockDiffFile }, @@ -822,7 +823,7 @@ describe('DiffsStoreActions', () => { id: commitId, }, }; - const dispatch = jest.fn(name => { + const dispatch = jest.fn((name) => { switch (name) { case 'saveNote': return Promise.resolve({ @@ -854,7 +855,7 @@ describe('DiffsStoreActions', () => { }); describe('toggleTreeOpen', () => { - it('commits TOGGLE_FOLDER_OPEN', done => { + it('commits TOGGLE_FOLDER_OPEN', (done) => { testAction( toggleTreeOpen, 'path', @@ -903,7 +904,7 @@ describe('DiffsStoreActions', () => { }); describe('setShowTreeList', () => { - it('commits toggle', done => { + it('commits toggle', (done) => { testAction( setShowTreeList, { showTreeList: true }, @@ -991,7 +992,7 @@ describe('DiffsStoreActions', () => { }); describe('setRenderTreeList', () => { - it('commits SET_RENDER_TREE_LIST', done => { + it('commits SET_RENDER_TREE_LIST', (done) => { testAction( setRenderTreeList, true, @@ -1014,7 +1015,7 @@ describe('DiffsStoreActions', () => { jest.spyOn(eventHub, '$emit').mockImplementation(); }); - it('commits SET_SHOW_WHITESPACE', done => { + it('commits SET_SHOW_WHITESPACE', (done) => { testAction( setShowWhitespace, { showWhitespace: true }, @@ -1057,13 +1058,13 @@ describe('DiffsStoreActions', () => { }); describe('setRenderIt', () => { - it('commits RENDER_FILE', done => { + it('commits RENDER_FILE', (done) => { testAction(setRenderIt, 'file', {}, [{ type: types.RENDER_FILE, payload: 'file' }], [], done); }); }); describe('receiveFullDiffError', () => { - it('updates state with the file that did not load', done => { + it('updates state with the file that did not load', (done) => { testAction( receiveFullDiffError, 'file', @@ -1091,7 +1092,7 @@ describe('DiffsStoreActions', () => { 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', (done) => { const file = { context_lines_path: `${TEST_HOST}/context`, file_path: 'test', @@ -1113,7 +1114,7 @@ describe('DiffsStoreActions', () => { mock.onGet(`${TEST_HOST}/context`).replyOnce(500); }); - it('dispatches receiveFullDiffError', done => { + it('dispatches receiveFullDiffError', (done) => { testAction( fetchFullDiff, { context_lines_path: `${TEST_HOST}/context`, file_path: 'test', file_hash: 'test' }, @@ -1135,7 +1136,7 @@ describe('DiffsStoreActions', () => { }; }); - it('dispatches fetchFullDiff when file is not expanded', done => { + it('dispatches fetchFullDiff when file is not expanded', (done) => { testAction( toggleFullDiff, 'test', @@ -1211,7 +1212,7 @@ describe('DiffsStoreActions', () => { }); describe('setFileUserCollapsed', () => { - it('commits SET_FILE_COLLAPSED', done => { + it('commits SET_FILE_COLLAPSED', (done) => { testAction( setFileCollapsedByUser, { filePath: 'test', collapsed: true }, @@ -1230,12 +1231,12 @@ describe('DiffsStoreActions', () => { describe('setExpandedDiffLines', () => { beforeEach(() => { - utils.idleCallback.mockImplementation(cb => { + utils.idleCallback.mockImplementation((cb) => { cb({ timeRemaining: () => 50 }); }); }); - 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', (done) => { utils.convertExpandLines.mockImplementation(() => ['test']); testAction( @@ -1253,7 +1254,7 @@ describe('DiffsStoreActions', () => { ); }); - 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', (done) => { const lines = new Array(501).fill().map((_, i) => `line-${i}`); utils.convertExpandLines.mockReturnValue(lines); @@ -1280,7 +1281,7 @@ describe('DiffsStoreActions', () => { }); describe('setSuggestPopoverDismissed', () => { - it('commits SET_SHOW_SUGGEST_POPOVER', done => { + it('commits SET_SHOW_SUGGEST_POPOVER', (done) => { const state = { dismissEndpoint: `${TEST_HOST}/-/user_callouts` }; const mock = new MockAdapter(axios); mock.onPost(state.dismissEndpoint).reply(200, {}); @@ -1409,7 +1410,7 @@ describe('DiffsStoreActions', () => { const state = { diffFiles: [{ file_hash: '123' }] }; const rootGetters = { getDiscussion: () => ({ diff_file: { file_hash: '123' } }), - notesById: { '1': { discussion_id: '2' } }, + notesById: { 1: { discussion_id: '2' } }, }; setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); @@ -1422,7 +1423,7 @@ describe('DiffsStoreActions', () => { const state = { diffFiles: [{ file_hash: '123' }] }; const rootGetters = { getDiscussion: () => ({ id: '1' }), - notesById: { '1': { discussion_id: '2' } }, + notesById: { 1: { discussion_id: '2' } }, }; setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); @@ -1435,7 +1436,7 @@ describe('DiffsStoreActions', () => { const state = { diffFiles: [{ file_hash: '123' }] }; const rootGetters = { getDiscussion: () => ({ diff_file: { file_hash: '124' } }), - notesById: { '1': { discussion_id: '2' } }, + notesById: { 1: { discussion_id: '2' } }, }; setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1'); @@ -1445,7 +1446,7 @@ describe('DiffsStoreActions', () => { }); describe('navigateToDiffFileIndex', () => { - it('commits VIEW_DIFF_FILE', done => { + it('commits VIEW_DIFF_FILE', (done) => { testAction( navigateToDiffFileIndex, 0, @@ -1472,4 +1473,46 @@ describe('DiffsStoreActions', () => { ); }); }); + + describe('reviewFile', () => { + const file = { + id: '123', + file_identifier_hash: 'abc', + load_collapsed_diff_url: 'gitlab-org/gitlab-test/-/merge_requests/1/diffs', + }; + it.each` + reviews | diffFile | reviewed + ${{ abc: ['123'] }} | ${file} | ${true} + ${{}} | ${file} | ${false} + `( + 'sets reviews ($reviews) to localStorage and state for file $file if it is marked reviewed=$reviewed', + ({ reviews, diffFile, reviewed }) => { + const commitSpy = jest.fn(); + const getterSpy = jest.fn().mockReturnValue([]); + + reviewFile( + { + commit: commitSpy, + getters: { + fileReviews: getterSpy, + }, + state: { + mrReviews: { abc: ['123'] }, + }, + }, + { + file: diffFile, + reviewed, + }, + ); + + expect(localStorage.setItem).toHaveBeenCalledTimes(1); + expect(localStorage.setItem).toHaveBeenCalledWith( + 'gitlab-org/gitlab-test/-/merge_requests/1-file-reviews', + JSON.stringify(reviews), + ); + expect(commitSpy).toHaveBeenCalledWith(types.SET_MR_FILE_REVIEWS, reviews); + }, + ); + }); }); |