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

test updates

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