From b8ecb2c7eb5eeb488e4532b9011d681b398f165d Mon Sep 17 00:00:00 2001 From: Tim Zallmann Date: Fri, 7 Sep 2018 01:27:21 +0200 Subject: [PATCH] Added new tests for newly added mutations, actions, utils --- .../javascripts/diffs/components/app.vue | 9 +- app/assets/javascripts/diffs/store/utils.js | 2 +- app/assets/javascripts/notes/stores/utils.js | 1 - spec/javascripts/diffs/store/actions_spec.js | 165 +++++++++++++++++- .../javascripts/diffs/store/mutations_spec.js | 37 ++++ spec/javascripts/diffs/store/utils_spec.js | 19 ++ 6 files changed, 221 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/diffs/components/app.vue b/app/assets/javascripts/diffs/components/app.vue index 13ff79029d0..4261a99c52b 100644 --- a/app/assets/javascripts/diffs/components/app.vue +++ b/app/assets/javascripts/diffs/components/app.vue @@ -127,13 +127,8 @@ export default { .then(() => { requestIdleCallback( () => { - this.startRenderDiffsQueue() - .then(() => { - this.setDiscussions(); - }) - .catch(() => { - createFlash(__('Something went wrong on our end. Please try again!')); - }); + this.setDiscussions(); + this.startRenderDiffsQueue(); }, { timeout: 1000 }, ); diff --git a/app/assets/javascripts/diffs/store/utils.js b/app/assets/javascripts/diffs/store/utils.js index eb9bab09d38..e15588a58a8 100644 --- a/app/assets/javascripts/diffs/store/utils.js +++ b/app/assets/javascripts/diffs/store/utils.js @@ -182,8 +182,8 @@ export function trimFirstCharOfLineContent(line = {}) { } export function prepareDiffData(diffData) { - let showingLines = 0; const filesLength = diffData.diffFiles.length; + let showingLines = 0; let i; for (i = 0; i < filesLength; i += 1) { const file = diffData.diffFiles[i]; diff --git a/app/assets/javascripts/notes/stores/utils.js b/app/assets/javascripts/notes/stores/utils.js index 71533cd9c88..a6cfd1590f5 100644 --- a/app/assets/javascripts/notes/stores/utils.js +++ b/app/assets/javascripts/notes/stores/utils.js @@ -32,7 +32,6 @@ export const reduceDiscussionsToLineCodes = selectedDiscussions => const items = acc[note.line_code] || []; if (note.diff_file) { Object.assign(note, { fileHash: note.diff_file.file_hash }); - // delete note.diff_file; } items.push(note); diff --git a/spec/javascripts/diffs/store/actions_spec.js b/spec/javascripts/diffs/store/actions_spec.js index c1560dac1a0..8b52c9bffc7 100644 --- a/spec/javascripts/diffs/store/actions_spec.js +++ b/spec/javascripts/diffs/store/actions_spec.js @@ -7,8 +7,11 @@ import { } from '~/diffs/constants'; import * as actions from '~/diffs/store/actions'; import * as types from '~/diffs/store/mutation_types'; +import { reduceDiscussionsToLineCodes } from '~/notes/stores/utils'; import axios from '~/lib/utils/axios_utils'; import testAction from '../../helpers/vuex_action_helper'; +import mockFile from '../mock_data/diff_file'; +import mockDiscussion from '../mock_data/diff_discussions'; describe('DiffsStoreActions', () => { describe('setBaseConfig', () => { @@ -53,6 +56,150 @@ describe('DiffsStoreActions', () => { }); }); + describe('assignDiscussionsToDiff', () => { + it('should merge discussions into diffs', done => { + const state = { diffFiles: [Object.assign({}, mockFile)] }; + const singleDiscussion = Object.assign({}, mockDiscussion); + const discussions = reduceDiscussionsToLineCodes([singleDiscussion]); + + testAction( + actions.assignDiscussionsToDiff, + discussions, + state, + [ + { + type: types.SET_LINE_DISCUSSIONS, + payload: { + line: { + lineCode: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2', + type: 'new', + oldLine: null, + newLine: 2, + discussions: [], + text: '+\n', + richText: '\n', + metaData: null, + }, + discussions: discussions['1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2'], + }, + }, + { + type: types.SET_LINE_DISCUSSIONS, + payload: { + line: { + lineCode: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2', + type: 'new', + oldLine: null, + newLine: 2, + discussions: [], + text: '+\n', + richText: '+\n', + metaData: null, + }, + discussions: discussions['1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2'], + }, + }, + ], + [], + () => { + done(); + }, + ); + }); + }); + + describe('removeDiscussionsFromDiff', () => { + it('should remove discussions from diffs', done => { + const state = { diffFiles: [Object.assign({}, mockFile)] }; + const singleDiscussion = Object.assign({}, mockDiscussion); + + reduceDiscussionsToLineCodes([singleDiscussion]); + + testAction( + actions.removeDiscussionsFromDiff, + singleDiscussion, + state, + [ + { + type: types.REMOVE_LINE_DISCUSSIONS, + payload: { + lineCode: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2', + type: 'new', + oldLine: null, + newLine: 2, + discussions: [], + text: '+\n', + richText: '\n', + metaData: null, + }, + }, + { + type: types.REMOVE_LINE_DISCUSSIONS, + payload: { + lineCode: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2', + type: 'new', + oldLine: null, + newLine: 2, + discussions: [], + text: '+\n', + richText: '+\n', + metaData: null, + }, + }, + ], + [], + () => { + done(); + }, + ); + }); + }); + + describe('startRenderDiffsQueue', () => { + it('should set all files to RENDER_FILE', done => { + const actualRAF = global.requestAnimationFrame; + global.requestAnimationFrame = cb => { + cb(); + }; + + const state = { + diffFiles: [ + { + id: 1, + renderIt: false, + collapsed: false, + }, + { + id: 2, + renderIt: false, + collapsed: false, + }, + ], + }; + + const pseudoCommit = (commitType, file) => { + expect(commitType).toBe(types.RENDER_FILE); + Object.assign(file, { + renderIt: true, + }); + }; + + actions + .startRenderDiffsQueue({ state, commit: pseudoCommit }) + .then(() => { + global.requestAnimationFrame = actualRAF; + + expect(state.diffFiles[0].renderIt).toBeTruthy(); + expect(state.diffFiles[1].renderIt).toBeTruthy(); + + done(); + }) + .catch(() => { + done.fail(); + }); + }); + }); + describe('setInlineDiffViewType', () => { it('should set diff view type to inline and also set the cookie properly', done => { testAction( @@ -204,7 +351,11 @@ describe('DiffsStoreActions', () => { actions.toggleFileDiscussions({ getters, dispatch }); - expect(dispatch).toHaveBeenCalledWith('collapseDiscussion', { discussionId: 1 }, { root: true }); + expect(dispatch).toHaveBeenCalledWith( + 'collapseDiscussion', + { discussionId: 1 }, + { root: true }, + ); }); it('should dispatch expandDiscussion when all discussions are collapsed', () => { @@ -218,7 +369,11 @@ describe('DiffsStoreActions', () => { actions.toggleFileDiscussions({ getters, dispatch }); - expect(dispatch).toHaveBeenCalledWith('expandDiscussion', { discussionId: 1 }, { root: true }); + 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', () => { @@ -232,7 +387,11 @@ describe('DiffsStoreActions', () => { actions.toggleFileDiscussions({ getters, dispatch }); - expect(dispatch).toHaveBeenCalledWith('expandDiscussion', { discussionId: 1 }, { root: true }); + expect(dispatch).toHaveBeenCalledWith( + 'expandDiscussion', + { discussionId: 1 }, + { root: true }, + ); }); }); }); diff --git a/spec/javascripts/diffs/store/mutations_spec.js b/spec/javascripts/diffs/store/mutations_spec.js index 7dfab46ba4f..4a042b7675f 100644 --- a/spec/javascripts/diffs/store/mutations_spec.js +++ b/spec/javascripts/diffs/store/mutations_spec.js @@ -148,4 +148,41 @@ describe('DiffsStoreMutations', () => { expect(state.diffFiles[1].extraField).toEqual(1); }); }); + + describe('SET_LINE_DISCUSSIONS', () => { + it('should add discussions to the given line', () => { + const line = { fileHash: 'ABC', discussions: [] }; + const discussions = [ + { + id: 1, + }, + { + id: 2, + }, + ]; + + mutations[types.SET_LINE_DISCUSSIONS]({}, { line, discussions }); + expect(line.discussions.length).toEqual(2); + expect(line.discussions[1].id).toEqual(2); + }); + }); + + describe('REMOVE_LINE_DISCUSSIONS', () => { + it('should remove the existing discussions on the given line', () => { + const line = { + fileHash: 'ABC', + discussions: [ + { + id: 1, + }, + { + id: 2, + }, + ], + }; + + mutations[types.REMOVE_LINE_DISCUSSIONS]({}, line); + expect(line.discussions.length).toEqual(0); + }); + }); }); diff --git a/spec/javascripts/diffs/store/utils_spec.js b/spec/javascripts/diffs/store/utils_spec.js index cd16d597c54..bd9d63769a1 100644 --- a/spec/javascripts/diffs/store/utils_spec.js +++ b/spec/javascripts/diffs/store/utils_spec.js @@ -220,4 +220,23 @@ describe('DiffsStoreUtils', () => { expect(utils.trimFirstCharOfLineContent()).toEqual({ discussions: [] }); }); }); + + describe('prepareDiffData', () => { + it('sets the renderIt and collapsed attribute on files', () => { + const preparedDiff = { diffFiles: [getDiffFileMock()] }; + utils.prepareDiffData(preparedDiff); + + const firstParallelDiffLine = preparedDiff.diffFiles[0].parallelDiffLines[2]; + expect(firstParallelDiffLine.left.discussions.length).toBe(0); + expect(firstParallelDiffLine.left).not.toHaveAttr('text'); + expect(firstParallelDiffLine.right.discussions.length).toBe(0); + expect(firstParallelDiffLine.right).not.toHaveAttr('text'); + + expect(preparedDiff.diffFiles[0].highlightedDiffLines[0].discussions.length).toBe(0); + expect(preparedDiff.diffFiles[0].highlightedDiffLines[0]).not.toHaveAttr('text'); + + expect(preparedDiff.diffFiles[0].renderIt).toBeTruthy(); + expect(preparedDiff.diffFiles[0].collapsed).toBeFalsy(); + }); + }); });