Commit 705e174b authored by Tim Zallmann's avatar Tim Zallmann

Merge branch 'vuex-action-helper-correctly-fail' into 'master'

Correctly fail Vuex action helper

Closes #44735

See merge request gitlab-org/gitlab-ce!18072
parents 82658bb2 470b8ca1
/* eslint-disable */
/** /**
* helper for testing action with expected mutations * helper for testing action with expected mutations inspired in
* https://vuex.vuejs.org/en/testing.html * https://vuex.vuejs.org/en/testing.html
*
* @example
* testAction(
* actions.actionName, // action
* { }, // mocked response
* state, // state
* [
* { type: types.MUTATION}
* { type: types.MUTATION_1, payload: {}}
* ], // mutations
* [
* { type: 'actionName', payload: {}},
* { type: 'actionName1', payload: {}}
* ] //actions
* done,
* );
*/ */
export default (action, payload, state, expectedMutations, done) => { export default (action, payload, state, expectedMutations, expectedActions, done) => {
let count = 0; let mutationsCount = 0;
let actionsCount = 0;
// mock commit // mock commit
const commit = (type, payload) => { const commit = (type, mutationPayload) => {
const mutation = expectedMutations[count]; const mutation = expectedMutations[mutationsCount];
expect(mutation.type).toEqual(type);
try { if (mutation.payload) {
expect(mutation.type).to.equal(type); expect(mutation.payload).toEqual(mutationPayload);
if (payload) {
expect(mutation.payload).to.deep.equal(payload);
} }
} catch (error) {
done(error); mutationsCount += 1;
if (mutationsCount >= expectedMutations.length) {
done();
} }
};
// mock dispatch
const dispatch = (type, actionPayload) => {
const actionExpected = expectedActions[actionsCount];
expect(actionExpected.type).toEqual(type);
count++; if (actionExpected.payload) {
if (count >= expectedMutations.length) { expect(actionExpected.payload).toEqual(actionPayload);
}
actionsCount += 1;
if (actionsCount >= expectedActions.length) {
done(); done();
} }
}; };
// call the action with mocked store and arguments // call the action with mocked store and arguments
action({ commit, state }, payload); action({ commit, state, dispatch }, 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(mutationsCount).toEqual(0);
done();
}
// check if no mutations should have been dispatched
if (expectedActions.length === 0) {
expect(actionsCount).toEqual(0);
done(); done();
} }
}; };
...@@ -5,7 +5,13 @@ import * as actions from '~/notes/stores/actions'; ...@@ -5,7 +5,13 @@ import * as actions from '~/notes/stores/actions';
import store from '~/notes/stores'; import store from '~/notes/stores';
import testAction from '../../helpers/vuex_action_helper'; import testAction from '../../helpers/vuex_action_helper';
import { resetStore } from '../helpers'; import { resetStore } from '../helpers';
import { discussionMock, notesDataMock, userDataMock, noteableDataMock, individualNote } from '../mock_data'; import {
discussionMock,
notesDataMock,
userDataMock,
noteableDataMock,
individualNote,
} from '../mock_data';
describe('Actions Notes Store', () => { describe('Actions Notes Store', () => {
afterEach(() => { afterEach(() => {
...@@ -13,66 +19,103 @@ describe('Actions Notes Store', () => { ...@@ -13,66 +19,103 @@ describe('Actions Notes Store', () => {
}); });
describe('setNotesData', () => { describe('setNotesData', () => {
it('should set received notes data', (done) => { it('should set received notes data', done => {
testAction(actions.setNotesData, null, { notesData: {} }, [ testAction(
{ type: 'SET_NOTES_DATA', payload: notesDataMock }, actions.setNotesData,
], done); notesDataMock,
{ notesData: {} },
[{ type: 'SET_NOTES_DATA', payload: notesDataMock }],
[],
done,
);
}); });
}); });
describe('setNoteableData', () => { describe('setNoteableData', () => {
it('should set received issue data', (done) => { it('should set received issue data', done => {
testAction(actions.setNoteableData, null, { noteableData: {} }, [ testAction(
{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }, actions.setNoteableData,
], done); noteableDataMock,
{ noteableData: {} },
[{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }],
[],
done,
);
}); });
}); });
describe('setUserData', () => { describe('setUserData', () => {
it('should set received user data', (done) => { it('should set received user data', done => {
testAction(actions.setUserData, null, { userData: {} }, [ testAction(
{ type: 'SET_USER_DATA', payload: userDataMock }, actions.setUserData,
], done); userDataMock,
{ userData: {} },
[{ type: 'SET_USER_DATA', payload: userDataMock }],
[],
done,
);
}); });
}); });
describe('setLastFetchedAt', () => { describe('setLastFetchedAt', () => {
it('should set received timestamp', (done) => { it('should set received timestamp', done => {
testAction(actions.setLastFetchedAt, null, { lastFetchedAt: {} }, [ testAction(
{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }, actions.setLastFetchedAt,
], done); 'timestamp',
{ lastFetchedAt: {} },
[{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }],
[],
done,
);
}); });
}); });
describe('setInitialNotes', () => { describe('setInitialNotes', () => {
it('should set initial notes', (done) => { it('should set initial notes', done => {
testAction(actions.setInitialNotes, null, { notes: [] }, [ testAction(
{ type: 'SET_INITIAL_NOTES', payload: [individualNote] }, actions.setInitialNotes,
], done); [individualNote],
{ notes: [] },
[{ type: 'SET_INITIAL_NOTES', payload: [individualNote] }],
[],
done,
);
}); });
}); });
describe('setTargetNoteHash', () => { describe('setTargetNoteHash', () => {
it('should set target note hash', (done) => { it('should set target note hash', done => {
testAction(actions.setTargetNoteHash, null, { notes: [] }, [ testAction(
{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }, actions.setTargetNoteHash,
], done); 'hash',
{ notes: [] },
[{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }],
[],
done,
);
}); });
}); });
describe('toggleDiscussion', () => { describe('toggleDiscussion', () => {
it('should toggle discussion', (done) => { it('should toggle discussion', done => {
testAction(actions.toggleDiscussion, null, { notes: [discussionMock] }, [ testAction(
{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }, actions.toggleDiscussion,
], done); { discussionId: discussionMock.id },
{ notes: [discussionMock] },
[{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
[],
done,
);
}); });
}); });
describe('async methods', () => { describe('async methods', () => {
const interceptor = (request, next) => { const interceptor = (request, next) => {
next(request.respondWith(JSON.stringify({}), { next(
request.respondWith(JSON.stringify({}), {
status: 200, status: 200,
})); }),
);
}; };
beforeEach(() => { beforeEach(() => {
...@@ -84,8 +127,9 @@ describe('Actions Notes Store', () => { ...@@ -84,8 +127,9 @@ describe('Actions Notes Store', () => {
}); });
describe('closeIssue', () => { describe('closeIssue', () => {
it('sets state as closed', (done) => { it('sets state as closed', done => {
store.dispatch('closeIssue', { notesData: { closeIssuePath: '' } }) store
.dispatch('closeIssue', { notesData: { closeIssuePath: '' } })
.then(() => { .then(() => {
expect(store.state.noteableData.state).toEqual('closed'); expect(store.state.noteableData.state).toEqual('closed');
expect(store.state.isToggleStateButtonLoading).toEqual(false); expect(store.state.isToggleStateButtonLoading).toEqual(false);
...@@ -96,8 +140,9 @@ describe('Actions Notes Store', () => { ...@@ -96,8 +140,9 @@ describe('Actions Notes Store', () => {
}); });
describe('reopenIssue', () => { describe('reopenIssue', () => {
it('sets state as reopened', (done) => { it('sets state as reopened', done => {
store.dispatch('reopenIssue', { notesData: { reopenIssuePath: '' } }) store
.dispatch('reopenIssue', { notesData: { reopenIssuePath: '' } })
.then(() => { .then(() => {
expect(store.state.noteableData.state).toEqual('reopened'); expect(store.state.noteableData.state).toEqual('reopened');
expect(store.state.isToggleStateButtonLoading).toEqual(false); expect(store.state.isToggleStateButtonLoading).toEqual(false);
...@@ -110,7 +155,7 @@ describe('Actions Notes Store', () => { ...@@ -110,7 +155,7 @@ describe('Actions Notes Store', () => {
describe('emitStateChangedEvent', () => { describe('emitStateChangedEvent', () => {
it('emits an event on the document', () => { it('emits an event on the document', () => {
document.addEventListener('issuable_vue_app:change', (event) => { document.addEventListener('issuable_vue_app:change', event => {
expect(event.detail.data).toEqual({ id: '1', state: 'closed' }); expect(event.detail.data).toEqual({ id: '1', state: 'closed' });
expect(event.detail.isClosed).toEqual(false); expect(event.detail.isClosed).toEqual(false);
}); });
...@@ -120,40 +165,47 @@ describe('Actions Notes Store', () => { ...@@ -120,40 +165,47 @@ describe('Actions Notes Store', () => {
}); });
describe('toggleStateButtonLoading', () => { describe('toggleStateButtonLoading', () => {
it('should set loading as true', (done) => { it('should set loading as true', done => {
testAction(actions.toggleStateButtonLoading, true, {}, [ testAction(
{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }, actions.toggleStateButtonLoading,
], done); true,
{},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }],
[],
done,
);
}); });
it('should set loading as false', (done) => { it('should set loading as false', done => {
testAction(actions.toggleStateButtonLoading, false, {}, [ testAction(
{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }, actions.toggleStateButtonLoading,
], done); false,
{},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }],
[],
done,
);
}); });
}); });
describe('toggleIssueLocalState', () => { describe('toggleIssueLocalState', () => {
it('sets issue state as closed', (done) => { it('sets issue state as closed', done => {
testAction(actions.toggleIssueLocalState, 'closed', {}, [ testAction(actions.toggleIssueLocalState, 'closed', {}, [{ type: 'CLOSE_ISSUE' }], [], done);
{ type: 'CLOSE_ISSUE', payload: 'closed' },
], done);
}); });
it('sets issue state as reopened', (done) => { it('sets issue state as reopened', done => {
testAction(actions.toggleIssueLocalState, 'reopened', {}, [ testAction(actions.toggleIssueLocalState, 'reopened', {}, [{ type: 'REOPEN_ISSUE' }], [], done);
{ type: 'REOPEN_ISSUE', payload: 'reopened' },
], done);
}); });
}); });
describe('poll', () => { describe('poll', () => {
beforeEach((done) => { beforeEach(done => {
jasmine.clock().install(); jasmine.clock().install();
spyOn(Vue.http, 'get').and.callThrough(); spyOn(Vue.http, 'get').and.callThrough();
store.dispatch('setNotesData', notesDataMock) store
.dispatch('setNotesData', notesDataMock)
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
}); });
...@@ -162,23 +214,29 @@ describe('Actions Notes Store', () => { ...@@ -162,23 +214,29 @@ describe('Actions Notes Store', () => {
jasmine.clock().uninstall(); jasmine.clock().uninstall();
}); });
it('calls service with last fetched state', (done) => { it('calls service with last fetched state', done => {
const interceptor = (request, next) => { const interceptor = (request, next) => {
next(request.respondWith(JSON.stringify({ next(
request.respondWith(
JSON.stringify({
notes: [], notes: [],
last_fetched_at: '123456', last_fetched_at: '123456',
}), { }),
{
status: 200, status: 200,
headers: { headers: {
'poll-interval': '1000', 'poll-interval': '1000',
}, },
})); },
),
);
}; };
Vue.http.interceptors.push(interceptor); Vue.http.interceptors.push(interceptor);
Vue.http.interceptors.push(headersInterceptor); Vue.http.interceptors.push(headersInterceptor);
store.dispatch('poll') store
.dispatch('poll')
.then(() => new Promise(resolve => requestAnimationFrame(resolve))) .then(() => new Promise(resolve => requestAnimationFrame(resolve)))
.then(() => { .then(() => {
expect(Vue.http.get).toHaveBeenCalledWith(jasmine.anything(), { expect(Vue.http.get).toHaveBeenCalledWith(jasmine.anything(), {
...@@ -192,9 +250,12 @@ describe('Actions Notes Store', () => { ...@@ -192,9 +250,12 @@ describe('Actions Notes Store', () => {
jasmine.clock().tick(1500); jasmine.clock().tick(1500);
}) })
.then(() => new Promise((resolve) => { .then(
() =>
new Promise(resolve => {
requestAnimationFrame(resolve); requestAnimationFrame(resolve);
})) }),
)
.then(() => { .then(() => {
expect(Vue.http.get.calls.count()).toBe(2); expect(Vue.http.get.calls.count()).toBe(2);
expect(Vue.http.get.calls.mostRecent().args[1].headers).toEqual({ expect(Vue.http.get.calls.mostRecent().args[1].headers).toEqual({
......
...@@ -29,57 +29,96 @@ describe('Actions Registry Store', () => { ...@@ -29,57 +29,96 @@ describe('Actions Registry Store', () => {
describe('fetchRepos', () => { describe('fetchRepos', () => {
beforeEach(() => { beforeEach(() => {
interceptor = (request, next) => { interceptor = (request, next) => {
next(request.respondWith(JSON.stringify(reposServerResponse), { next(
request.respondWith(JSON.stringify(reposServerResponse), {
status: 200, status: 200,
})); }),
);
}; };
Vue.http.interceptors.push(interceptor); Vue.http.interceptors.push(interceptor);
}); });
it('should set receveived repos', (done) => { it('should set receveived repos', done => {
testAction(actions.fetchRepos, null, mockedState, [ testAction(
actions.fetchRepos,
null,
mockedState,
[
{ type: types.TOGGLE_MAIN_LOADING },
{ type: types.TOGGLE_MAIN_LOADING }, { type: types.TOGGLE_MAIN_LOADING },
{ type: types.SET_REPOS_LIST, payload: reposServerResponse }, { type: types.SET_REPOS_LIST, payload: reposServerResponse },
], done); ],
[],
done,
);
}); });
}); });
describe('fetchList', () => { describe('fetchList', () => {
beforeEach(() => { beforeEach(() => {
interceptor = (request, next) => { interceptor = (request, next) => {
next(request.respondWith(JSON.stringify(registryServerResponse), { next(
request.respondWith(JSON.stringify(registryServerResponse), {
status: 200, status: 200,
})); }),
);
}; };
Vue.http.interceptors.push(interceptor); Vue.http.interceptors.push(interceptor);
}); });
it('should set received list', (done) => { it('should set received list', done => {
mockedState.repos = parsedReposServerResponse; mockedState.repos = parsedReposServerResponse;
testAction(actions.fetchList, { repo: mockedState.repos[1] }, mockedState, [ const repo = mockedState.repos[1];
{ type: types.TOGGLE_REGISTRY_LIST_LOADING },
{ type: types.SET_REGISTRY_LIST, payload: registryServerResponse }, testAction(
], done); actions.fetchList,
{ repo },
mockedState,
[
{ type: types.TOGGLE_REGISTRY_LIST_LOADING, payload: repo },
{ type: types.TOGGLE_REGISTRY_LIST_LOADING, payload: repo },
{
type: types.SET_REGISTRY_LIST,
payload: {
repo,
resp: registryServerResponse,
headers: jasmine.anything(),
},
},
],
[],
done,
);
}); });
}); });
}); });
describe('setMainEndpoint', () => { describe('setMainEndpoint', () => {
it('should commit set main endpoint', (done) => { it('should commit set main endpoint', done => {
testAction(actions.setMainEndpoint, 'endpoint', mockedState, [ testAction(
{ type: types.SET_MAIN_ENDPOINT, payload: 'endpoint' }, actions.setMainEndpoint,
], done); 'endpoint',
mockedState,
[{ type: types.SET_MAIN_ENDPOINT, payload: 'endpoint' }],
[],
done,
);
}); });
}); });
describe('toggleLoading', () => { describe('toggleLoading', () => {
it('should commit toggle main loading', (done) => { it('should commit toggle main loading', done => {
testAction(actions.toggleLoading, null, mockedState, [ testAction(
{ type: types.TOGGLE_MAIN_LOADING }, actions.toggleLoading,
], done); null,
mockedState,
[{ type: types.TOGGLE_MAIN_LOADING }],
[],
done,
);
}); });
}); });
}); });
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