Commit 8b01ef82 authored by Filipa Lacerda's avatar Filipa Lacerda

Adds helper to test Vuex actions

parent cbddad5a
import * as actions from '~/notes/stores/actions';
describe('Actions Notes Store', () => {
import testAction from './helpers';
import { note, discussionMock, notesDataMock, userDataMock, issueDataMock, individualNote } from '../mock_data';
import service from '~/notes/services/issue_notes_service';
// 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);
});
});
......
/* 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();
}
};
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