Commit b8ecb2c7 authored by Tim Zallmann's avatar Tim Zallmann

Added new tests for newly added mutations, actions, utils

parent 89c796db
......@@ -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.startRenderDiffsQueue();
},
{ timeout: 1000 },
);
......
......@@ -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];
......
......@@ -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);
......
......@@ -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: '+<span id="LC2" class="line" lang="plaintext"></span>\n',
richText: '<span id="LC2" class="line" lang="plaintext"></span>\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: '+<span id="LC2" class="line" lang="plaintext"></span>\n',
richText: '+<span id="LC2" class="line" lang="plaintext"></span>\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: '+<span id="LC2" class="line" lang="plaintext"></span>\n',
richText: '<span id="LC2" class="line" lang="plaintext"></span>\n',
metaData: null,
},
},
{
type: types.REMOVE_LINE_DISCUSSIONS,
payload: {
lineCode: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2',
type: 'new',
oldLine: null,
newLine: 2,
discussions: [],
text: '+<span id="LC2" class="line" lang="plaintext"></span>\n',
richText: '+<span id="LC2" class="line" lang="plaintext"></span>\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 },
);
});
});
});
......@@ -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);
});
});
});
......@@ -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();
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment