Add dispatch mock to the test helper

This commit is contained in:
Filipa Lacerda 2018-03-29 11:14:18 +01:00
parent e26d20311c
commit 470b8ca10a
No known key found for this signature in database
GPG key ID: 9CA3FDE4D1E2F1C8
3 changed files with 62 additions and 12 deletions

View file

@ -1,15 +1,30 @@
/* 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, mutationPayload) => { const commit = (type, mutationPayload) => {
const mutation = expectedMutations[count]; const mutation = expectedMutations[mutationsCount];
expect(mutation.type).toEqual(type); expect(mutation.type).toEqual(type);
@ -17,18 +32,40 @@ export default (action, payload, state, expectedMutations, done) => {
expect(mutation.payload).toEqual(mutationPayload); expect(mutation.payload).toEqual(mutationPayload);
} }
count++; mutationsCount += 1;
if (count >= expectedMutations.length) { if (mutationsCount >= expectedMutations.length) {
done();
}
};
// mock dispatch
const dispatch = (type, actionPayload) => {
const actionExpected = expectedActions[actionsCount];
expect(actionExpected.type).toEqual(type);
if (actionExpected.payload) {
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).toEqual(0); expect(mutationsCount).toEqual(0);
done();
}
// check if no mutations should have been dispatched
if (expectedActions.length === 0) {
expect(actionsCount).toEqual(0);
done(); done();
} }
}; };

View file

@ -25,6 +25,7 @@ describe('Actions Notes Store', () => {
notesDataMock, notesDataMock,
{ notesData: {} }, { notesData: {} },
[{ type: 'SET_NOTES_DATA', payload: notesDataMock }], [{ type: 'SET_NOTES_DATA', payload: notesDataMock }],
[],
done, done,
); );
}); });
@ -37,6 +38,7 @@ describe('Actions Notes Store', () => {
noteableDataMock, noteableDataMock,
{ noteableData: {} }, { noteableData: {} },
[{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }], [{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }],
[],
done, done,
); );
}); });
@ -49,6 +51,7 @@ describe('Actions Notes Store', () => {
userDataMock, userDataMock,
{ userData: {} }, { userData: {} },
[{ type: 'SET_USER_DATA', payload: userDataMock }], [{ type: 'SET_USER_DATA', payload: userDataMock }],
[],
done, done,
); );
}); });
@ -61,6 +64,7 @@ describe('Actions Notes Store', () => {
'timestamp', 'timestamp',
{ lastFetchedAt: {} }, { lastFetchedAt: {} },
[{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }], [{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }],
[],
done, done,
); );
}); });
@ -73,6 +77,7 @@ describe('Actions Notes Store', () => {
[individualNote], [individualNote],
{ notes: [] }, { notes: [] },
[{ type: 'SET_INITIAL_NOTES', payload: [individualNote] }], [{ type: 'SET_INITIAL_NOTES', payload: [individualNote] }],
[],
done, done,
); );
}); });
@ -85,6 +90,7 @@ describe('Actions Notes Store', () => {
'hash', 'hash',
{ notes: [] }, { notes: [] },
[{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }], [{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }],
[],
done, done,
); );
}); });
@ -97,6 +103,7 @@ describe('Actions Notes Store', () => {
{ discussionId: discussionMock.id }, { discussionId: discussionMock.id },
{ notes: [discussionMock] }, { notes: [discussionMock] },
[{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }], [{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
[],
done, done,
); );
}); });
@ -164,6 +171,7 @@ describe('Actions Notes Store', () => {
true, true,
{}, {},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }], [{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }],
[],
done, done,
); );
}); });
@ -174,6 +182,7 @@ describe('Actions Notes Store', () => {
false, false,
{}, {},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }], [{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }],
[],
done, done,
); );
}); });
@ -181,11 +190,11 @@ describe('Actions Notes Store', () => {
describe('toggleIssueLocalState', () => { describe('toggleIssueLocalState', () => {
it('sets issue state as closed', done => { it('sets issue state as closed', done => {
testAction(actions.toggleIssueLocalState, 'closed', {}, [{ type: 'CLOSE_ISSUE' }], done); testAction(actions.toggleIssueLocalState, 'closed', {}, [{ type: 'CLOSE_ISSUE' }], [], done);
}); });
it('sets issue state as reopened', done => { it('sets issue state as reopened', done => {
testAction(actions.toggleIssueLocalState, 'reopened', {}, [{ type: 'REOPEN_ISSUE' }], done); testAction(actions.toggleIssueLocalState, 'reopened', {}, [{ type: 'REOPEN_ISSUE' }], [], done);
}); });
}); });

View file

@ -49,6 +49,7 @@ describe('Actions Registry Store', () => {
{ 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,
); );
}); });
@ -88,6 +89,7 @@ describe('Actions Registry Store', () => {
}, },
}, },
], ],
[],
done, done,
); );
}); });
@ -101,6 +103,7 @@ describe('Actions Registry Store', () => {
'endpoint', 'endpoint',
mockedState, mockedState,
[{ type: types.SET_MAIN_ENDPOINT, payload: 'endpoint' }], [{ type: types.SET_MAIN_ENDPOINT, payload: 'endpoint' }],
[],
done, done,
); );
}); });
@ -113,6 +116,7 @@ describe('Actions Registry Store', () => {
null, null,
mockedState, mockedState,
[{ type: types.TOGGLE_MAIN_LOADING }], [{ type: types.TOGGLE_MAIN_LOADING }],
[],
done, done,
); );
}); });