BigW Consortium Gitlab

Added tests to multi-file Vuex store

parent 8c65b76b
......@@ -3,16 +3,16 @@ import * as types from '../mutation_types';
import { pushState } from '../utils';
// eslint-disable-next-line import/prefer-default-export
export const createNewBranch = ({ rootState, commit }, branch) => service.createBranch(
rootState.project.id,
export const createNewBranch = ({ state, commit }, branch) => service.createBranch(
state.project.id,
{
branch,
ref: rootState.currentBranch,
ref: state.currentBranch,
},
).then(res => res.json())
.then((data) => {
const branchName = data.name;
const url = location.href.replace(rootState.currentBranch, branchName);
const url = location.href.replace(state.currentBranch, branchName);
pushState(url);
......
/* eslint-disable */
export const testWithDispatch = (action, payload, state, expectedDispatch, done) => {
let count = 0;
// mock commit
const dispatch = (type, payload) => {
const dispatch = expectedDispatch[count];
try {
expect(dispatch.type).toEqual(type);
if (payload !== null) {
expect(dispatch.payload).toEqual(payload);
}
} catch (error) {
done.fail(error);
}
count++;
if (count >= expectedDispatch.length) {
done();
}
};
// call the action with mocked store and arguments
action({ dispatch, state }, payload);
// check if no mutations should have been dispatched
if (expectedDispatch.length === 0) {
expect(count).to.equal(0);
done();
}
};
/**
* helper for testing action with expected mutations
* https://vuex.vuejs.org/en/testing.html
......@@ -12,26 +44,31 @@ export default (action, payload, state, expectedMutations, done) => {
const mutation = expectedMutations[count];
try {
expect(mutation.type).to.equal(type);
if (payload) {
expect(mutation.payload).to.deep.equal(payload);
expect(mutation.type).toEqual(type);
if (payload !== null) {
expect(mutation.payload).toEqual(payload);
}
} catch (error) {
done(error);
if (done) {
done.fail(error);
}
}
count++;
if (count >= expectedMutations.length) {
if (count >= expectedMutations.length && done) {
done();
}
};
// call the action with mocked store and arguments
action({ commit, state }, payload);
return action({ commit, state }, payload);
// check if no mutations should have been dispatched
if (expectedMutations.length === 0) {
expect(count).to.equal(0);
done();
if (done) {
done();
}
}
};
import * as actions from '~/repo/stores/actions/branch';
import state from '~/repo/stores/state';
import service from '~/repo/services';
import testAction from '../../../helpers/vuex_action_helper';
describe('Multi-file store branch actions', () => {
let localState;
beforeEach(() => {
localState = state();
});
describe('createNewBranch', () => {
beforeEach(() => {
spyOn(service, 'createBranch').and.returnValue(Promise.resolve({
json: () => ({
name: 'testing',
}),
}));
spyOn(history, 'pushState');
localState.project.id = 2;
localState.currentBranch = 'testing';
});
it('creates new branch', (done) => {
testAction(
actions.createNewBranch,
'master',
localState,
[
{ type: 'SET_CURRENT_BRANCH', payload: 'testing' },
],
).then(() => {
expect(service.createBranch).toHaveBeenCalledWith(2, {
branch: 'master',
ref: 'testing',
});
expect(history.pushState).toHaveBeenCalled();
done();
}).catch(done.fail);
});
});
});
describe('Multi-file store file actions', () => {
describe('closeFile', () => {
});
describe('setFileActive', () => {
});
describe('getFileData', () => {
});
describe('getRawFileData', () => {
});
describe('changeFileContent', () => {
});
describe('createTempFile', () => {
});
});
describe('Multi-file store tree actions', () => {
describe('getTreeData', () => {
});
describe('toggleTreeOpen', () => {
});
describe('clickedTreeRow', () => {
});
describe('createTempTree', () => {
});
});
import * as actions from '~/repo/stores/actions';
import state from '~/repo/stores/state';
import service from '~/repo/services';
import testAction, { testWithDispatch } from '../../helpers/vuex_action_helper';
import { file } from '../helpers';
describe('Multi-file store actions', () => {
let localState;
beforeEach(() => {
localState = state();
});
describe('redirectToUrl', () => {
it('calls visitUrl', () => {
spyOn(gl.utils, 'visitUrl');
actions.redirectToUrl('test');
expect(gl.utils.visitUrl).toHaveBeenCalledWith('test');
});
});
describe('setInitialData', () => {
it('commits initial data', (done) => {
testAction(
actions.setInitialData,
{ canCommit: true },
localState,
[
{ type: 'SET_INITIAL_DATA', payload: { canCommit: true } },
],
done,
);
});
});
describe('closeDiscardPopup', () => {
it('closes the discard popup', (done) => {
testAction(
actions.closeDiscardPopup,
false,
localState,
[
{ type: 'TOGGLE_DISCARD_POPUP', payload: false },
],
done,
);
});
});
describe('discardAllChanges', () => {
beforeEach(() => {
localState.openFiles.push(file());
localState.openFiles[0].changed = true;
});
});
describe('closeAllFiles', () => {
beforeEach(() => {
localState.openFiles.push(file());
localState.openFiles[0].changed = true;
});
it('closes all open files', (done) => {
testWithDispatch(
actions.closeAllFiles,
localState.openFiles[0],
localState,
[
{ type: 'closeFile', payload: { file: localState.openFiles[0] } },
],
done,
);
});
});
describe('toggleEditMode', () => {
});
describe('toggleBlobView', () => {
it('sets edit mode view if in edit mode', (done) => {
localState.editMode = true;
testAction(
actions.toggleBlobView,
null,
localState,
[
{ type: 'SET_EDIT_MODE' },
],
done,
);
});
it('sets preview mode view if not in edit mode', (done) => {
testAction(
actions.toggleBlobView,
null,
localState,
[
{ type: 'SET_PREVIEW_MODE' },
],
done,
);
});
});
describe('checkCommitStatus', () => {
beforeEach(() => {
localState.project.id = 2;
localState.currentBranch = 'master';
localState.currentRef = '1';
});
it('calls service', () => {
spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
commit: { id: '123' },
}));
actions.checkCommitStatus({ state: localState });
expect(service.getBranchData).toHaveBeenCalledWith(2, 'master');
});
it('returns true if current ref does not equal returned ID', (done) => {
spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
commit: { id: '123' },
}));
actions.checkCommitStatus({ state: localState })
.then((val) => {
expect(val).toBeTruthy();
done();
})
.catch(done.fail);
});
it('returns false if current ref equals returned ID', (done) => {
spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
commit: { id: '1' },
}));
actions.checkCommitStatus({ state: localState })
.then((val) => {
expect(val).toBeFalsy();
done();
})
.catch(done.fail);
});
});
describe('commitChanges', () => {
});
describe('createTempEntry', () => {
it('creates a temp tree', (done) => {
testWithDispatch(
actions.createTempEntry,
{ name: 'test', type: 'tree' },
localState,
[
{ type: 'createTempTree', payload: 'test' },
],
done,
);
});
it('creates temp file', (done) => {
testWithDispatch(
actions.createTempEntry,
{ name: 'test', type: 'blob' },
localState,
[
{
type: 'createTempFile',
payload: {
tree: localState,
name: 'test',
base64: false,
content: '',
},
},
],
done,
);
});
});
describe('popHistoryState', () => {
});
describe('scrollToTab', () => {
it('focuses the current active element', (done) => {
document.body.innerHTML += '<div id="tabs"><div class="active"><div class="repo-tab"></div></div></div>';
const el = document.querySelector('.repo-tab');
spyOn(el, 'focus');
actions.scrollToTab();
setTimeout(() => {
expect(el.focus).toHaveBeenCalled();
document.getElementById('tabs').remove();
done();
});
});
});
});
import * as getters from '~/repo/stores/getters';
import state from '~/repo/stores/state';
import { file } from '../helpers';
describe('Multi-file store getters', () => {
let localState;
beforeEach(() => {
localState = state();
});
describe('treeList', () => {
it('returns flat tree list', () => {
localState.tree.push(file('1'));
localState.tree[0].tree.push(file('2'));
localState.tree[0].tree[0].tree.push(file('3'));
const treeList = getters.treeList(localState);
expect(treeList.length).toBe(3);
expect(treeList[1].name).toBe(localState.tree[0].tree[0].name);
expect(treeList[2].name).toBe(localState.tree[0].tree[0].tree[0].name);
});
});
describe('changedFiles', () => {
it('returns a list of changed opened files', () => {
localState.openFiles.push(file());
localState.openFiles.push(file('changed'));
localState.openFiles[1].changed = true;
const changedFiles = getters.changedFiles(localState);
expect(changedFiles.length).toBe(1);
expect(changedFiles[0].name).toBe('changed');
});
});
describe('activeFile', () => {
it('returns the current active file', () => {
localState.openFiles.push(file());
localState.openFiles.push(file('active'));
localState.openFiles[1].active = true;
expect(getters.activeFile(localState).name).toBe('active');
});
it('returns undefined if no active files are found', () => {
localState.openFiles.push(file());
localState.openFiles.push(file('active'));
expect(getters.activeFile(localState)).toBeUndefined();
});
});
describe('activeFileExtension', () => {
it('returns the file extension for the current active file', () => {
localState.openFiles.push(file('active'));
localState.openFiles[0].active = true;
localState.openFiles[0].path = 'test.js';
expect(getters.activeFileExtension(localState)).toBe('.js');
localState.openFiles[0].path = 'test.es6.js';
expect(getters.activeFileExtension(localState)).toBe('.js');
});
});
describe('isCollapsed', () => {
it('returns true if state has open files', () => {
localState.openFiles.push(file());
expect(getters.isCollapsed(localState)).toBeTruthy();
});
it('returns false if state has no open files', () => {
expect(getters.isCollapsed(localState)).toBeFalsy();
});
});
describe('canEditFile', () => {
beforeEach(() => {
localState.onTopOfBranch = true;
localState.canCommit = true;
localState.openFiles.push(file());
localState.openFiles[0].active = true;
});
it('returns true if user can commit and has open files', () => {
expect(getters.canEditFile(localState)).toBeTruthy();
});
it('returns false if user can commit and has no open files', () => {
localState.openFiles = [];
expect(getters.canEditFile(localState)).toBeFalsy();
});
it('returns false if user can commit and active file is binary', () => {
localState.openFiles[0].binary = true;
expect(getters.canEditFile(localState)).toBeFalsy();
});
it('returns false if user cant commit', () => {
localState.canCommit = false;
expect(getters.canEditFile(localState)).toBeFalsy();
});
it('returns false if user can commit but on a branch', () => {
localState.onTopOfBranch = false;
expect(getters.canEditFile(localState)).toBeFalsy();
});
});
});
import mutations from '~/repo/stores/mutations/branch';
import state from '~/repo/stores/state';
describe('Multi-file store branch mutations', () => {
let localState;
beforeEach(() => {
localState = state();
});
describe('SET_CURRENT_BRANCH', () => {
it('sets currentBranch', () => {
mutations.SET_CURRENT_BRANCH(localState, 'master');
expect(localState.currentBranch).toBe('master');
});
});
});
import mutations from '~/repo/stores/mutations/file';
import state from '~/repo/stores/state';
import { file } from '../../helpers';
describe('Multi-file store file mutations', () => {
let localState;
let localFile;
beforeEach(() => {
localState = state();
localFile = file();
});
describe('SET_FILE_ACTIVE', () => {
it('sets the file active', () => {
mutations.SET_FILE_ACTIVE(localState, {
file: localFile,
active: true,
});
expect(localFile.active).toBeTruthy();
});
});
describe('TOGGLE_FILE_OPEN', () => {
beforeEach(() => {
mutations.TOGGLE_FILE_OPEN(localState, localFile);
});
it('adds into opened files', () => {
expect(localFile.opened).toBeTruthy();
expect(localState.openFiles.length).toBe(1);
});
it('removes from opened files', () => {
mutations.TOGGLE_FILE_OPEN(localState, localFile);
expect(localFile.opened).toBeFalsy();
expect(localState.openFiles.length).toBe(0);
});
});
describe('SET_FILE_DATA', () => {
it('sets extra file data', () => {
mutations.SET_FILE_DATA(localState, {
data: {
blame_path: 'blame',
commits_path: 'commits',
permalink: 'permalink',
raw_path: 'raw',
binary: true,
html: 'html',
render_error: 'render_error',
},
file: localFile,
});
expect(localFile.blamePath).toBe('blame');
expect(localFile.commitsPath).toBe('commits');
expect(localFile.permalink).toBe('permalink');
expect(localFile.rawPath).toBe('raw');
expect(localFile.binary).toBeTruthy();
expect(localFile.html).toBe('html');
expect(localFile.renderError).toBe('render_error');
});
});
describe('SET_FILE_RAW_DATA', () => {
it('sets raw data', () => {
mutations.SET_FILE_RAW_DATA(localState, {
file: localFile,
raw: 'testing',
});
expect(localFile.raw).toBe('testing');
});
});
describe('UPDATE_FILE_CONTENT', () => {
beforeEach(() => {
localFile.raw = 'test';
});
it('sets content', () => {
mutations.UPDATE_FILE_CONTENT(localState, {
file: localFile,
content: 'test',
});
expect(localFile.content).toBe('test');
});
it('sets changed if content does not match raw', () => {
mutations.UPDATE_FILE_CONTENT(localState, {
file: localFile,
content: 'testing',
});
expect(localFile.content).toBe('testing');
expect(localFile.changed).toBeTruthy();
});
});
describe('DISCARD_FILE_CHANGES', () => {
beforeEach(() => {
localFile.content = 'test';
localFile.changed = true;
});
it('resets content and changed', () => {
mutations.DISCARD_FILE_CHANGES(localState, localFile);
expect(localFile.content).toBe('');
expect(localFile.changed).toBeFalsy();
});
});
describe('CREATE_TMP_FILE', () => {
it('adds file into parent tree', () => {
const f = file();
mutations.CREATE_TMP_FILE(localState, {
file: f,
parent: localFile,
});
expect(localFile.tree.length).toBe(1);
expect(localFile.tree[0].name).toBe(f.name);
});
});
});
import mutations from '~/repo/stores/mutations/tree';
import state from '~/repo/stores/state';
import { file } from '../../helpers';
describe('Multi-file store tree mutations', () => {
let localState;
let localTree;
beforeEach(() => {
localState = state();
localTree = file();
});
describe('TOGGLE_TREE_OPEN', () => {
it('toggles tree open', () => {
mutations.TOGGLE_TREE_OPEN(localState, localTree);
expect(localTree.opened).toBeTruthy();
mutations.TOGGLE_TREE_OPEN(localState, localTree);
expect(localTree.opened).toBeFalsy();
});
});
describe('SET_DIRECTORY_DATA', () => {
const data = {
trees: [{
name: 'tree',
}],
submodules: [{
name: 'submodule',
}],
blobs: [{
name: 'blob',
}],
};
it('adds directory data', () => {
mutations.SET_DIRECTORY_DATA(localState, {
data,
tree: localState,
});
expect(localState.tree.length).toBe(3);
expect(localState.tree[0].type).toBe('tree');
expect(localState.tree[1].type).toBe('submodule');
expect(localState.tree[2].type).toBe('blob');
});
it('defaults to rootUrl when no parent_tree_url is in data', () => {
localState.endpoints.rootUrl = 'test';
mutations.SET_DIRECTORY_DATA(localState, {
data,
tree: localState,
});
expect(localState.tree[0].parentTreeUrl).toBe('test');
});
it('uses parent_tree_url from data', () => {
mutations.SET_DIRECTORY_DATA(localState, {
data: {
...data,
parent_tree_url: 'parent/',
path: 'test',
},
tree: localState,
});
expect(localState.tree[0].parentTreeUrl).toBe('parent/test');
});
});
describe('SET_PARENT_TREE_URL', () => {
it('sets the parent tree url', () => {
mutations.SET_PARENT_TREE_URL(localState, 'test');
expect(localState.parentTreeUrl).toBe('test');
});
});
describe('CREATE_TMP_TREE', () => {
it('adds tree into parent tree', () => {
const tmpEntry = file();
mutations.CREATE_TMP_TREE(localState, {
tmpEntry,
parent: localTree,
});
expect(localTree.tree.length).toBe(1);
expect(localTree.tree[0].name).toBe(tmpEntry.name);
});
});
});
import mutations from '~/repo/stores/mutations';
import state from '~/repo/stores/state';
import { file } from '../helpers';
describe('Multi-file store mutations', () => {
let localState;
let entry;
beforeEach(() => {
localState = state();
entry = file();
});
describe('SET_INITIAL_DATA', () => {
it('sets all initial data', () => {
mutations.SET_INITIAL_DATA(localState, {
test: 'test',
});
expect(localState.test).toBe('test');
});
});
describe('SET_PREVIEW_MODE', () => {
it('sets currentBlobView to repo-preview', () => {
mutations.SET_PREVIEW_MODE(localState);
expect(localState.currentBlobView).toBe('repo-preview');
localState.currentBlobView = 'testing';
mutations.SET_PREVIEW_MODE(localState);
expect(localState.currentBlobView).toBe('repo-preview');
});
});
describe('SET_EDIT_MODE', () => {
it('sets currentBlobView to repo-editor', () => {
mutations.SET_EDIT_MODE(localState);
expect(localState.currentBlobView).toBe('repo-editor');
localState.currentBlobView = 'testing';
mutations.SET_EDIT_MODE(localState);
expect(localState.currentBlobView).toBe('repo-editor');
});
});
describe('TOGGLE_LOADING', () => {
it('toggles loading of entry', () => {
mutations.TOGGLE_LOADING(localState, entry);
expect(entry.loading).toBeTruthy();
mutations.TOGGLE_LOADING(localState, entry);
expect(entry.loading).toBeFalsy();
});
});
describe('TOGGLE_EDIT_MODE', () => {
it('toggles editMode', () => {
mutations.TOGGLE_EDIT_MODE(localState);
expect(localState.editMode).toBeTruthy();
mutations.TOGGLE_EDIT_MODE(localState);
expect(localState.editMode).toBeFalsy();
});
});
describe('TOGGLE_DISCARD_POPUP', () => {
it('sets discardPopupOpen', () => {
mutations.TOGGLE_DISCARD_POPUP(localState, true);
expect(localState.discardPopupOpen).toBeTruthy();
mutations.TOGGLE_DISCARD_POPUP(localState, false);
expect(localState.discardPopupOpen).toBeFalsy();
});
});
describe('SET_COMMIT_REF', () => {
it('sets currentRef', () => {
mutations.SET_COMMIT_REF(localState, '123');
expect(localState.currentRef).toBe('123');
});
});
describe('SET_ROOT', () => {
it('sets isRoot & initialRoot', () => {
mutations.SET_ROOT(localState, true);
expect(localState.isRoot).toBeTruthy();
expect(localState.isInitialRoot).toBeTruthy();
mutations.SET_ROOT(localState, false);
expect(localState.isRoot).toBeFalsy();
expect(localState.isInitialRoot).toBeFalsy();
});
});
describe('SET_PREVIOUS_URL', () => {
it('sets previousUrl', () => {
mutations.SET_PREVIOUS_URL(localState, 'testing');
expect(localState.previousUrl).toBe('testing');
});
});
});
import * as utils from '~/repo/stores/utils';
describe('Multi-file store utils', () => {
describe('setPageTitle', () => {
it('sets the document page title', () => {
utils.setPageTitle('test');
expect(document.title).toBe('test');
});
});
describe('pushState', () => {
it('calls history.pushState', () => {
spyOn(history, 'pushState');
utils.pushState('test');
expect(history.pushState).toHaveBeenCalledWith({ url: 'test' }, '', 'test');
});
});
describe('createTemp', () => {
it('creates temp tree', () => {
const tmp = utils.createTemp({
name: 'test',
path: 'test',
type: 'tree',
level: 0,
changed: false,
content: '',
base64: '',
});
expect(tmp.tempFile).toBeTruthy();
expect(tmp.icon).toBe('fa-folder');
});
it('creates temp file', () => {
const tmp = utils.createTemp({
name: 'test',
path: 'test',
type: 'blob',
level: 0,
changed: false,
content: '',
base64: '',
});
expect(tmp.tempFile).toBeTruthy();
expect(tmp.icon).toBe('fa-file-text-o');
});
});
describe('findIndexOfFile', () => {
let state;
beforeEach(() => {
state = [{
path: '1',
}, {
path: '2',
}];
});
it('finds in the index of an entry by path', () => {
const index = utils.findIndexOfFile(state, {
path: '2',
});
expect(index).toBe(1);
});
});
describe('findEntry', () => {
let state;
beforeEach(() => {
state = {
tree: [{
type: 'tree',
name: 'test',
}, {
type: 'blob',
name: 'file',
}],
};
});
it('returns an entry found by name', () => {
const foundEntry = utils.findEntry(state, 'tree', 'test');
expect(foundEntry.type).toBe('tree');
expect(foundEntry.name).toBe('test');
});
it('returns undefined when no entry found', () => {
const foundEntry = utils.findEntry(state, 'blob', 'test');
expect(foundEntry).toBeUndefined();
});
});
});
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