Commit 8ccbfa2f authored by Phil Hughes's avatar Phil Hughes

test updates

parent f82af4bd
...@@ -3,7 +3,7 @@ import flash from '../../flash'; ...@@ -3,7 +3,7 @@ import flash from '../../flash';
import service from '../services'; import service from '../services';
import * as types from './mutation_types'; import * as types from './mutation_types';
export const redirectToUrl = url => gl.utils.visitUrl(url); export const redirectToUrl = (_, url) => gl.utils.visitUrl(url);
export const setInitialData = ({ commit }, data) => commit(types.SET_INITIAL_DATA, data); export const setInitialData = ({ commit }, data) => commit(types.SET_INITIAL_DATA, data);
......
/* eslint-disable */ /* 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 * helper for testing action with expected mutations
* https://vuex.vuejs.org/en/testing.html * https://vuex.vuejs.org/en/testing.html
...@@ -44,31 +12,26 @@ export default (action, payload, state, expectedMutations, done) => { ...@@ -44,31 +12,26 @@ export default (action, payload, state, expectedMutations, done) => {
const mutation = expectedMutations[count]; const mutation = expectedMutations[count];
try { try {
expect(mutation.type).toEqual(type); expect(mutation.type).to.equal(type);
if (payload !== null) { if (payload) {
expect(mutation.payload).toEqual(payload); expect(mutation.payload).to.deep.equal(payload);
} }
} catch (error) { } catch (error) {
if (done) { done(error);
done.fail(error);
}
} }
count++; count++;
if (count >= expectedMutations.length && done) { if (count >= expectedMutations.length) {
done(); done();
} }
}; };
// call the action with mocked store and arguments // call the action with mocked store and arguments
return action({ commit, state }, payload); action({ commit, state }, payload);
// check if no mutations should have been dispatched // check if no mutations should have been dispatched
if (expectedMutations.length === 0) { if (expectedMutations.length === 0) {
expect(count).to.equal(0); expect(count).to.equal(0);
done();
if (done) {
done();
}
} }
}; };
import * as actions from '~/repo/stores/actions/branch'; import store from '~/repo/stores';
import state from '~/repo/stores/state';
import service from '~/repo/services'; import service from '~/repo/services';
import testAction from '../../../helpers/vuex_action_helper'; import { resetStore } from '../../helpers';
describe('Multi-file store branch actions', () => { describe('Multi-file store branch actions', () => {
let localState; afterEach(() => {
resetStore(store);
beforeEach(() => {
localState = state();
}); });
describe('createNewBranch', () => { describe('createNewBranch', () => {
...@@ -19,27 +16,23 @@ describe('Multi-file store branch actions', () => { ...@@ -19,27 +16,23 @@ describe('Multi-file store branch actions', () => {
})); }));
spyOn(history, 'pushState'); spyOn(history, 'pushState');
localState.project.id = 2; store.state.project.id = 2;
localState.currentBranch = 'testing'; store.state.currentBranch = 'testing';
}); });
it('creates new branch', (done) => { it('creates new branch', (done) => {
testAction( store.dispatch('createNewBranch', 'master')
actions.createNewBranch, .then(() => {
'master', expect(store.state.currentBranch).toBe('testing');
localState, expect(service.createBranch).toHaveBeenCalledWith(2, {
[ branch: 'master',
{ type: 'SET_CURRENT_BRANCH', payload: 'testing' }, ref: 'testing',
], });
).then(() => { expect(history.pushState).toHaveBeenCalled();
expect(service.createBranch).toHaveBeenCalledWith(2, {
branch: 'master',
ref: 'testing',
});
expect(history.pushState).toHaveBeenCalled();
done(); done();
}).catch(done.fail); })
.catch(done.fail);
}); });
}); });
}); });
import * as actions from '~/repo/stores/actions'; import store from '~/repo/stores';
import state from '~/repo/stores/state';
import service from '~/repo/services'; import service from '~/repo/services';
import testAction, { testWithDispatch } from '../../helpers/vuex_action_helper'; import { resetStore, file } from '../helpers';
import { file } from '../helpers';
describe('Multi-file store actions', () => { describe('Multi-file store actions', () => {
let localState; afterEach(() => {
resetStore(store);
beforeEach(() => {
localState = state();
}); });
describe('redirectToUrl', () => { describe('redirectToUrl', () => {
it('calls visitUrl', () => { it('calls visitUrl', (done) => {
spyOn(gl.utils, 'visitUrl'); spyOn(gl.utils, 'visitUrl');
actions.redirectToUrl('test'); store.dispatch('redirectToUrl', 'test')
.then(() => {
expect(gl.utils.visitUrl).toHaveBeenCalledWith('test');
expect(gl.utils.visitUrl).toHaveBeenCalledWith('test'); done();
})
.catch(done.fail);
}); });
}); });
describe('setInitialData', () => { describe('setInitialData', () => {
it('commits initial data', (done) => { it('commits initial data', (done) => {
testAction( store.dispatch('setInitialData', { canCommit: true })
actions.setInitialData, .then(() => {
{ canCommit: true }, expect(store.state.canCommit).toBeTruthy();
localState, done();
[ })
{ type: 'SET_INITIAL_DATA', payload: { canCommit: true } }, .catch(done.fail);
],
done,
);
}); });
}); });
describe('closeDiscardPopup', () => { describe('closeDiscardPopup', () => {
it('closes the discard popup', (done) => { it('closes the discard popup', (done) => {
testAction( store.dispatch('closeDiscardPopup', false)
actions.closeDiscardPopup, .then(() => {
false, expect(store.state.discardPopupOpen).toBeFalsy();
localState,
[ done();
{ type: 'TOGGLE_DISCARD_POPUP', payload: false }, })
], .catch(done.fail);
done,
);
}); });
}); });
describe('discardAllChanges', () => { describe('discardAllChanges', () => {
beforeEach(() => { beforeEach(() => {
localState.openFiles.push(file()); store.state.openFiles.push(file());
localState.openFiles[0].changed = true; store.state.openFiles[0].changed = true;
}); });
}); });
describe('closeAllFiles', () => { describe('closeAllFiles', () => {
beforeEach(() => { beforeEach(() => {
localState.openFiles.push(file()); store.state.openFiles.push(file());
localState.openFiles[0].changed = true; store.state.openFiles[0].opened = true;
}); });
it('closes all open files', (done) => { it('closes all open files', (done) => {
testWithDispatch( store.dispatch('closeAllFiles')
actions.closeAllFiles, .then(() => {
localState.openFiles[0], expect(store.state.openFiles.length).toBe(0);
localState,
[ done();
{ type: 'closeFile', payload: { file: localState.openFiles[0] } }, })
], .catch(done.fail);
done,
);
}); });
}); });
...@@ -81,47 +74,47 @@ describe('Multi-file store actions', () => { ...@@ -81,47 +74,47 @@ describe('Multi-file store actions', () => {
describe('toggleBlobView', () => { describe('toggleBlobView', () => {
it('sets edit mode view if in edit mode', (done) => { it('sets edit mode view if in edit mode', (done) => {
localState.editMode = true; store.state.editMode = true;
testAction( store.dispatch('toggleBlobView')
actions.toggleBlobView, .then(() => {
null, expect(store.state.currentBlobView).toBe('repo-editor');
localState,
[ done();
{ type: 'SET_EDIT_MODE' }, })
], .catch(done.fail);
done,
);
}); });
it('sets preview mode view if not in edit mode', (done) => { it('sets preview mode view if not in edit mode', (done) => {
testAction( store.dispatch('toggleBlobView')
actions.toggleBlobView, .then(() => {
null, expect(store.state.currentBlobView).toBe('repo-preview');
localState,
[ done();
{ type: 'SET_PREVIEW_MODE' }, })
], .catch(done.fail);
done,
);
}); });
}); });
describe('checkCommitStatus', () => { describe('checkCommitStatus', () => {
beforeEach(() => { beforeEach(() => {
localState.project.id = 2; store.state.project.id = 2;
localState.currentBranch = 'master'; store.state.currentBranch = 'master';
localState.currentRef = '1'; store.state.currentRef = '1';
}); });
it('calls service', () => { it('calls service', (done) => {
spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({ spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
commit: { id: '123' }, commit: { id: '123' },
})); }));
actions.checkCommitStatus({ state: localState }); store.dispatch('checkCommitStatus')
.then(() => {
expect(service.getBranchData).toHaveBeenCalledWith(2, 'master');
expect(service.getBranchData).toHaveBeenCalledWith(2, 'master'); done();
})
.catch(done.fail);
}); });
it('returns true if current ref does not equal returned ID', (done) => { it('returns true if current ref does not equal returned ID', (done) => {
...@@ -129,7 +122,7 @@ describe('Multi-file store actions', () => { ...@@ -129,7 +122,7 @@ describe('Multi-file store actions', () => {
commit: { id: '123' }, commit: { id: '123' },
})); }));
actions.checkCommitStatus({ state: localState }) store.dispatch('checkCommitStatus')
.then((val) => { .then((val) => {
expect(val).toBeTruthy(); expect(val).toBeTruthy();
...@@ -143,7 +136,7 @@ describe('Multi-file store actions', () => { ...@@ -143,7 +136,7 @@ describe('Multi-file store actions', () => {
commit: { id: '1' }, commit: { id: '1' },
})); }));
actions.checkCommitStatus({ state: localState }) store.dispatch('checkCommitStatus')
.then((val) => { .then((val) => {
expect(val).toBeFalsy(); expect(val).toBeFalsy();
...@@ -159,35 +152,33 @@ describe('Multi-file store actions', () => { ...@@ -159,35 +152,33 @@ describe('Multi-file store actions', () => {
describe('createTempEntry', () => { describe('createTempEntry', () => {
it('creates a temp tree', (done) => { it('creates a temp tree', (done) => {
testWithDispatch( store.dispatch('createTempEntry', {
actions.createTempEntry, name: 'test',
{ name: 'test', type: 'tree' }, type: 'tree',
localState, })
[ .then(() => {
{ type: 'createTempTree', payload: 'test' }, expect(store.state.tree.length).toBe(1);
], expect(store.state.tree[0].tempFile).toBeTruthy();
done, expect(store.state.tree[0].type).toBe('tree');
);
done();
})
.catch(done.fail);
}); });
it('creates temp file', (done) => { it('creates temp file', (done) => {
testWithDispatch( store.dispatch('createTempEntry', {
actions.createTempEntry, name: 'test',
{ name: 'test', type: 'blob' }, type: 'blob',
localState, })
[ .then(() => {
{ expect(store.state.tree.length).toBe(1);
type: 'createTempFile', expect(store.state.tree[0].tempFile).toBeTruthy();
payload: { expect(store.state.tree[0].type).toBe('blob');
tree: localState,
name: 'test', done();
base64: false, })
content: '', .catch(done.fail);
},
},
],
done,
);
}); });
}); });
...@@ -201,15 +192,17 @@ describe('Multi-file store actions', () => { ...@@ -201,15 +192,17 @@ describe('Multi-file store actions', () => {
const el = document.querySelector('.repo-tab'); const el = document.querySelector('.repo-tab');
spyOn(el, 'focus'); spyOn(el, 'focus');
actions.scrollToTab(); store.dispatch('scrollToTab')
.then(() => {
setTimeout(() => {
expect(el.focus).toHaveBeenCalled();
setTimeout(() => { document.getElementById('tabs').remove();
expect(el.focus).toHaveBeenCalled();
document.getElementById('tabs').remove(); done();
});
done(); })
}); .catch(done.fail);
}); });
}); });
}); });
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