Adds helper to test Vuex actions

This commit is contained in:
Filipa Lacerda 2017-08-11 20:58:20 +01:00
parent cbddad5a2d
commit 8b01ef826d
2 changed files with 97 additions and 13 deletions

View file

@ -1,44 +1,91 @@
import * as actions from '~/notes/stores/actions';
import testAction from './helpers';
import { note, discussionMock, notesDataMock, userDataMock, issueDataMock, individualNote } from '../mock_data';
import service from '~/notes/services/issue_notes_service';
describe('Actions Notes Store', () => {
// use require syntax for inline loaders.
// with inject-loader, this returns a module factory
// that allows us to inject mocked dependencies.
// const actionsInjector = require('inject-loader!./actions');
// const actions = actionsInjector({
// '../api/shop': {
// getProducts (cb) {
// setTimeout(() => {
// cb([ /* mocked response */ ])
// }, 100)
// }
// }
// });
fdescribe('Actions Notes Store', () => {
describe('setNotesData', () => {
it('should set received notes data', () => {
it('should set received notes data', (done) => {
testAction(actions.setNotesData, null, { notesData: {} }, [
{ type: 'SET_NOTES_DATA', payload: notesDataMock },
], done);
});
});
describe('setIssueData', () => {
it('should set received issue data', () => {});
it('should set received issue data', (done) => {
testAction(actions.setIssueData, null, { issueData: {} }, [
{ type: 'SET_ISSUE_DATA', payload: issueDataMock },
], done);
});
});
describe('setUserData', () => {
it('should set received user data', () => {});
it('should set received user data', (done) => {
testAction(actions.setUserData, null, { userData: {} }, [
{ type: 'SET_USER_DATA', payload: userDataMock },
], done);
});
});
describe('setLastFetchedAt', () => {
it('should set received timestamp', () => {});
it('should set received timestamp', (done) => {
testAction(actions.setLastFetchedAt, null, { lastFetchedAt: {} }, [
{ type: 'SET_LAST_FETCHED_AT', payload: 'timestamp' },
], done);
});
});
describe('setInitialNotes', () => {
it('should set initial notes', () => {
it('should set initial notes', (done) => {
testAction(actions.setInitialNotes, null, { notes: [] }, [
{ type: 'SET_INITAL_NOTES', payload: [individualNote] },
], done);
});
});
describe('setTargetNoteHash', () => {
it('should set target note hash', () => {});
it('should set target note hash', (done) => {
testAction(actions.setTargetNoteHash, null, { notes: [] }, [
{ type: 'SET_TARGET_NOTE_HASH', payload: 'hash' },
], done);
});
});
describe('toggleDiscussion', () => {
it('should toggle discussion', () => {
it('should toggle discussion', (done) => {
testAction(actions.toggleDiscussion, null, { notes: [discussionMock] }, [
{ type: 'TOGGLE_DISCUSSION', payload: { discussionId: discussionMock.id } },
], done);
});
});
describe('fetchNotes', () => {
it('should request notes', () => {
it('should request notes', (done) => {
spyOn(service, 'fetchNotes').and.returnValue(Promise.resolve({
json() {
return [individualNote];
},
}));
testAction(actions.fetchNotes, null, { notes: [] }, [
{ type: 'TOGGLE_DISCUSSION', payload: [individualNote] },
], done);
});
});

View file

@ -0,0 +1,37 @@
/* eslint-disable */
/**
* helper for testing action with expected mutations
* https://vuex.vuejs.org/en/testing.html
*/
export default (action, payload, state, expectedMutations, done) => {
let count = 0;
// mock commit
const commit = (type, payload) => {
const mutation = expectedMutations[count];
try {
expect(mutation.type).to.equal(type);
if (payload) {
expect(mutation.payload).to.deep.equal(payload);
}
} catch (error) {
done(error);
}
count++;
if (count >= expectedMutations.length) {
done();
}
};
// call the action with mocked store and arguments
action({ commit, state }, payload);
// check if no mutations should have been dispatched
if (expectedMutations.length === 0) {
expect(count).to.equal(0);
done();
}
};