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
*
* @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) => {
let count = 0;
export default (action, payload, state, expectedMutations, expectedActions, done) => {
let mutationsCount = 0;
let actionsCount = 0;
// mock commit
const commit = (type, mutationPayload) => {
const mutation = expectedMutations[count];
const mutation = expectedMutations[mutationsCount];
expect(mutation.type).toEqual(type);
@ -17,18 +32,40 @@ export default (action, payload, state, expectedMutations, done) => {
expect(mutation.payload).toEqual(mutationPayload);
}
count++;
if (count >= expectedMutations.length) {
mutationsCount += 1;
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();
}
};
// 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
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();
}
};

View file

@ -25,6 +25,7 @@ describe('Actions Notes Store', () => {
notesDataMock,
{ notesData: {} },
[{ type: 'SET_NOTES_DATA', payload: notesDataMock }],
[],
done,
);
});
@ -37,6 +38,7 @@ describe('Actions Notes Store', () => {
noteableDataMock,
{ noteableData: {} },
[{ type: 'SET_NOTEABLE_DATA', payload: noteableDataMock }],
[],
done,
);
});
@ -49,6 +51,7 @@ describe('Actions Notes Store', () => {
userDataMock,
{ userData: {} },
[{ type: 'SET_USER_DATA', payload: userDataMock }],
[],
done,
);
});
@ -61,6 +64,7 @@ describe('Actions Notes Store', () => {
'timestamp',
{ lastFetchedAt: {} },
[{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' }],
[],
done,
);
});
@ -73,6 +77,7 @@ describe('Actions Notes Store', () => {
[individualNote],
{ notes: [] },
[{ type: 'SET_INITIAL_NOTES', payload: [individualNote] }],
[],
done,
);
});
@ -85,6 +90,7 @@ describe('Actions Notes Store', () => {
'hash',
{ notes: [] },
[{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' }],
[],
done,
);
});
@ -97,6 +103,7 @@ describe('Actions Notes Store', () => {
{ discussionId: discussionMock.id },
{ notes: [discussionMock] },
[{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } }],
[],
done,
);
});
@ -164,6 +171,7 @@ describe('Actions Notes Store', () => {
true,
{},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: true }],
[],
done,
);
});
@ -174,6 +182,7 @@ describe('Actions Notes Store', () => {
false,
{},
[{ type: 'TOGGLE_STATE_BUTTON_LOADING', payload: false }],
[],
done,
);
});
@ -181,11 +190,11 @@ describe('Actions Notes Store', () => {
describe('toggleIssueLocalState', () => {
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 => {
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.SET_REPOS_LIST, payload: reposServerResponse },
],
[],
done,
);
});
@ -88,6 +89,7 @@ describe('Actions Registry Store', () => {
},
},
],
[],
done,
);
});
@ -101,6 +103,7 @@ describe('Actions Registry Store', () => {
'endpoint',
mockedState,
[{ type: types.SET_MAIN_ENDPOINT, payload: 'endpoint' }],
[],
done,
);
});
@ -113,6 +116,7 @@ describe('Actions Registry Store', () => {
null,
mockedState,
[{ type: types.TOGGLE_MAIN_LOADING }],
[],
done,
);
});