diff options
author | Phil Hughes <me@iamphill.com> | 2017-11-06 13:07:59 +0000 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2017-11-10 16:55:58 +0000 |
commit | 8ccbfa2f26431ff2c001660da5e7629d35d98169 (patch) | |
tree | d9ff5fa4ada7abd3b73d22f503fabe13eaa80922 /spec/javascripts | |
parent | f82af4bdcf19328d763aeb39d9d77a45c9cb9547 (diff) | |
download | gitlab-ce-8ccbfa2f26431ff2c001660da5e7629d35d98169.tar.gz |
test updates
Diffstat (limited to 'spec/javascripts')
-rw-r--r-- | spec/javascripts/helpers/vuex_action_helper.js | 51 | ||||
-rw-r--r-- | spec/javascripts/repo/stores/actions/branch_spec.js | 41 | ||||
-rw-r--r-- | spec/javascripts/repo/stores/actions_spec.js | 199 |
3 files changed, 120 insertions, 171 deletions
diff --git a/spec/javascripts/helpers/vuex_action_helper.js b/spec/javascripts/helpers/vuex_action_helper.js index 5e63aa01c12..2d386fe1da5 100644 --- a/spec/javascripts/helpers/vuex_action_helper.js +++ b/spec/javascripts/helpers/vuex_action_helper.js @@ -1,37 +1,5 @@ /* eslint-disable */ -export const testWithDispatch = (action, payload, state, expectedDispatch, done) => { - let count = 0; - - // mock commit - const dispatch = (type, payload) => { - const dispatch = expectedDispatch[count]; - - try { - expect(dispatch.type).toEqual(type); - if (payload !== null) { - expect(dispatch.payload).toEqual(payload); - } - } catch (error) { - done.fail(error); - } - - count++; - if (count >= expectedDispatch.length) { - done(); - } - }; - - // call the action with mocked store and arguments - action({ dispatch, state }, payload); - - // check if no mutations should have been dispatched - if (expectedDispatch.length === 0) { - expect(count).to.equal(0); - done(); - } -}; - /** * helper for testing action with expected mutations * https://vuex.vuejs.org/en/testing.html @@ -44,31 +12,26 @@ export default (action, payload, state, expectedMutations, done) => { const mutation = expectedMutations[count]; try { - expect(mutation.type).toEqual(type); - if (payload !== null) { - expect(mutation.payload).toEqual(payload); + expect(mutation.type).to.equal(type); + if (payload) { + expect(mutation.payload).to.deep.equal(payload); } } catch (error) { - if (done) { - done.fail(error); - } + done(error); } count++; - if (count >= expectedMutations.length && done) { + if (count >= expectedMutations.length) { done(); } }; // call the action with mocked store and arguments - return action({ commit, state }, payload); + action({ commit, state }, payload); // check if no mutations should have been dispatched if (expectedMutations.length === 0) { expect(count).to.equal(0); - - if (done) { - done(); - } + done(); } }; diff --git a/spec/javascripts/repo/stores/actions/branch_spec.js b/spec/javascripts/repo/stores/actions/branch_spec.js index 2c6ac89a254..af9d6835a67 100644 --- a/spec/javascripts/repo/stores/actions/branch_spec.js +++ b/spec/javascripts/repo/stores/actions/branch_spec.js @@ -1,13 +1,10 @@ -import * as actions from '~/repo/stores/actions/branch'; -import state from '~/repo/stores/state'; +import store from '~/repo/stores'; import service from '~/repo/services'; -import testAction from '../../../helpers/vuex_action_helper'; +import { resetStore } from '../../helpers'; describe('Multi-file store branch actions', () => { - let localState; - - beforeEach(() => { - localState = state(); + afterEach(() => { + resetStore(store); }); describe('createNewBranch', () => { @@ -19,27 +16,23 @@ describe('Multi-file store branch actions', () => { })); spyOn(history, 'pushState'); - localState.project.id = 2; - localState.currentBranch = 'testing'; + store.state.project.id = 2; + store.state.currentBranch = 'testing'; }); it('creates new branch', (done) => { - testAction( - actions.createNewBranch, - 'master', - localState, - [ - { type: 'SET_CURRENT_BRANCH', payload: 'testing' }, - ], - ).then(() => { - expect(service.createBranch).toHaveBeenCalledWith(2, { - branch: 'master', - ref: 'testing', - }); - expect(history.pushState).toHaveBeenCalled(); + store.dispatch('createNewBranch', 'master') + .then(() => { + expect(store.state.currentBranch).toBe('testing'); + expect(service.createBranch).toHaveBeenCalledWith(2, { + branch: 'master', + ref: 'testing', + }); + expect(history.pushState).toHaveBeenCalled(); - done(); - }).catch(done.fail); + done(); + }) + .catch(done.fail); }); }); }); diff --git a/spec/javascripts/repo/stores/actions_spec.js b/spec/javascripts/repo/stores/actions_spec.js index 12ac23d113d..af2f28be5f6 100644 --- a/spec/javascripts/repo/stores/actions_spec.js +++ b/spec/javascripts/repo/stores/actions_spec.js @@ -1,77 +1,70 @@ -import * as actions from '~/repo/stores/actions'; -import state from '~/repo/stores/state'; +import store from '~/repo/stores'; import service from '~/repo/services'; -import testAction, { testWithDispatch } from '../../helpers/vuex_action_helper'; -import { file } from '../helpers'; +import { resetStore, file } from '../helpers'; describe('Multi-file store actions', () => { - let localState; - - beforeEach(() => { - localState = state(); + afterEach(() => { + resetStore(store); }); describe('redirectToUrl', () => { - it('calls visitUrl', () => { + it('calls visitUrl', (done) => { spyOn(gl.utils, 'visitUrl'); - actions.redirectToUrl('test'); + store.dispatch('redirectToUrl', 'test') + .then(() => { + expect(gl.utils.visitUrl).toHaveBeenCalledWith('test'); - expect(gl.utils.visitUrl).toHaveBeenCalledWith('test'); + done(); + }) + .catch(done.fail); }); }); describe('setInitialData', () => { it('commits initial data', (done) => { - testAction( - actions.setInitialData, - { canCommit: true }, - localState, - [ - { type: 'SET_INITIAL_DATA', payload: { canCommit: true } }, - ], - done, - ); + store.dispatch('setInitialData', { canCommit: true }) + .then(() => { + expect(store.state.canCommit).toBeTruthy(); + done(); + }) + .catch(done.fail); }); }); describe('closeDiscardPopup', () => { it('closes the discard popup', (done) => { - testAction( - actions.closeDiscardPopup, - false, - localState, - [ - { type: 'TOGGLE_DISCARD_POPUP', payload: false }, - ], - done, - ); + store.dispatch('closeDiscardPopup', false) + .then(() => { + expect(store.state.discardPopupOpen).toBeFalsy(); + + done(); + }) + .catch(done.fail); }); }); describe('discardAllChanges', () => { beforeEach(() => { - localState.openFiles.push(file()); - localState.openFiles[0].changed = true; + store.state.openFiles.push(file()); + store.state.openFiles[0].changed = true; }); }); describe('closeAllFiles', () => { beforeEach(() => { - localState.openFiles.push(file()); - localState.openFiles[0].changed = true; + store.state.openFiles.push(file()); + store.state.openFiles[0].opened = true; }); it('closes all open files', (done) => { - testWithDispatch( - actions.closeAllFiles, - localState.openFiles[0], - localState, - [ - { type: 'closeFile', payload: { file: localState.openFiles[0] } }, - ], - done, - ); + store.dispatch('closeAllFiles') + .then(() => { + expect(store.state.openFiles.length).toBe(0); + + done(); + }) + .catch(done.fail); }); }); @@ -81,47 +74,47 @@ describe('Multi-file store actions', () => { describe('toggleBlobView', () => { it('sets edit mode view if in edit mode', (done) => { - localState.editMode = true; - - testAction( - actions.toggleBlobView, - null, - localState, - [ - { type: 'SET_EDIT_MODE' }, - ], - done, - ); + store.state.editMode = true; + + store.dispatch('toggleBlobView') + .then(() => { + expect(store.state.currentBlobView).toBe('repo-editor'); + + done(); + }) + .catch(done.fail); }); it('sets preview mode view if not in edit mode', (done) => { - testAction( - actions.toggleBlobView, - null, - localState, - [ - { type: 'SET_PREVIEW_MODE' }, - ], - done, - ); + store.dispatch('toggleBlobView') + .then(() => { + expect(store.state.currentBlobView).toBe('repo-preview'); + + done(); + }) + .catch(done.fail); }); }); describe('checkCommitStatus', () => { beforeEach(() => { - localState.project.id = 2; - localState.currentBranch = 'master'; - localState.currentRef = '1'; + store.state.project.id = 2; + store.state.currentBranch = 'master'; + store.state.currentRef = '1'; }); - it('calls service', () => { + it('calls service', (done) => { spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({ commit: { id: '123' }, })); - actions.checkCommitStatus({ state: localState }); + store.dispatch('checkCommitStatus') + .then(() => { + expect(service.getBranchData).toHaveBeenCalledWith(2, 'master'); - expect(service.getBranchData).toHaveBeenCalledWith(2, 'master'); + done(); + }) + .catch(done.fail); }); it('returns true if current ref does not equal returned ID', (done) => { @@ -129,7 +122,7 @@ describe('Multi-file store actions', () => { commit: { id: '123' }, })); - actions.checkCommitStatus({ state: localState }) + store.dispatch('checkCommitStatus') .then((val) => { expect(val).toBeTruthy(); @@ -143,7 +136,7 @@ describe('Multi-file store actions', () => { commit: { id: '1' }, })); - actions.checkCommitStatus({ state: localState }) + store.dispatch('checkCommitStatus') .then((val) => { expect(val).toBeFalsy(); @@ -159,35 +152,33 @@ describe('Multi-file store actions', () => { describe('createTempEntry', () => { it('creates a temp tree', (done) => { - testWithDispatch( - actions.createTempEntry, - { name: 'test', type: 'tree' }, - localState, - [ - { type: 'createTempTree', payload: 'test' }, - ], - done, - ); + store.dispatch('createTempEntry', { + name: 'test', + type: 'tree', + }) + .then(() => { + expect(store.state.tree.length).toBe(1); + expect(store.state.tree[0].tempFile).toBeTruthy(); + expect(store.state.tree[0].type).toBe('tree'); + + done(); + }) + .catch(done.fail); }); it('creates temp file', (done) => { - testWithDispatch( - actions.createTempEntry, - { name: 'test', type: 'blob' }, - localState, - [ - { - type: 'createTempFile', - payload: { - tree: localState, - name: 'test', - base64: false, - content: '', - }, - }, - ], - done, - ); + store.dispatch('createTempEntry', { + name: 'test', + type: 'blob', + }) + .then(() => { + expect(store.state.tree.length).toBe(1); + expect(store.state.tree[0].tempFile).toBeTruthy(); + expect(store.state.tree[0].type).toBe('blob'); + + done(); + }) + .catch(done.fail); }); }); @@ -201,15 +192,17 @@ describe('Multi-file store actions', () => { const el = document.querySelector('.repo-tab'); spyOn(el, 'focus'); - actions.scrollToTab(); + store.dispatch('scrollToTab') + .then(() => { + setTimeout(() => { + expect(el.focus).toHaveBeenCalled(); - setTimeout(() => { - expect(el.focus).toHaveBeenCalled(); + document.getElementById('tabs').remove(); - document.getElementById('tabs').remove(); - - done(); - }); + done(); + }); + }) + .catch(done.fail); }); }); }); |