diff options
author | Filipa Lacerda <filipa@gitlab.com> | 2017-08-11 20:58:20 +0100 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2017-08-11 20:58:20 +0100 |
commit | 8b01ef826df97fa23db657ba5826cb0dd0d0b38f (patch) | |
tree | a770776eadf9413f21c24877280899f00d508ba8 /spec/javascripts/notes | |
parent | cbddad5a2d22767d2dc7d7214f09e7b3ab8bf322 (diff) | |
download | gitlab-ce-8b01ef826df97fa23db657ba5826cb0dd0d0b38f.tar.gz |
Adds helper to test Vuex actions
Diffstat (limited to 'spec/javascripts/notes')
-rw-r--r-- | spec/javascripts/notes/stores/actions_spec.js | 75 | ||||
-rw-r--r-- | spec/javascripts/notes/stores/helpers.js | 37 |
2 files changed, 98 insertions, 14 deletions
diff --git a/spec/javascripts/notes/stores/actions_spec.js b/spec/javascripts/notes/stores/actions_spec.js index 18b1c2c8f6f..be304893a74 100644 --- a/spec/javascripts/notes/stores/actions_spec.js +++ b/spec/javascripts/notes/stores/actions_spec.js @@ -1,44 +1,91 @@ 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); }); }); diff --git a/spec/javascripts/notes/stores/helpers.js b/spec/javascripts/notes/stores/helpers.js new file mode 100644 index 00000000000..2d386fe1da5 --- /dev/null +++ b/spec/javascripts/notes/stores/helpers.js @@ -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(); + } +}; |