From 20689d8f72f4b1924efc243b3f0c847ed944e5db Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sun, 21 Jan 2018 16:08:49 +1000 Subject: [PATCH 1/2] Extract common code in router tests --- test/unit/specs/routing.spec.js | 68 +++++++++++++++------------------ 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/test/unit/specs/routing.spec.js b/test/unit/specs/routing.spec.js index 3d3e6ff7b..d268b5b7c 100644 --- a/test/unit/specs/routing.spec.js +++ b/test/unit/specs/routing.spec.js @@ -13,55 +13,47 @@ describe('router/index.js', function () { } describe('routing when route change is requested', function () { + const getBeforeEnterHandler = (state, fromName) => { + let exposedOptions + const RouterClass = function (options) { + exposedOptions = options + } + const storeMock = createStoreMock() + storeMock.state = state + createRouter(storeMock, RouterClass) + expect(exposedOptions).to.be.a('object') + const {routes} = exposedOptions + expect(routes).to.be.instanceOf(Array) + const routeOption = routes.filter(r => r.name === fromName)[0] + return routeOption.beforeEnter + } + + const getNextArg = beforeEnter => { + let exposedNextArg + const next = sinon.spy(arg => { + exposedNextArg = arg + }) + beforeEnter(undefined, undefined, next) + expect(next).to.have.been.calledOnce + return exposedNextArg + } + const createRedirectTest = function (state, fromName, expectedRedirectName) { return function () { - let exposedOptions - const RouterClass = function (options) { - exposedOptions = options - } - const storeMock = createStoreMock() - storeMock.state = state - createRouter(storeMock, RouterClass) - expect(exposedOptions).to.be.a('object') - const {routes} = exposedOptions - expect(routes).to.be.instanceOf(Array) - const routeOption = routes.filter(r => r.name === fromName)[0] - const {beforeEnter} = routeOption + const beforeEnter = getBeforeEnterHandler(state, fromName) expect(beforeEnter).to.be.a('function') - let exposedNextArg - const next = arg => { - exposedNextArg = arg - } - beforeEnter(undefined, undefined, next) + const exposedNextArg = getNextArg(beforeEnter) expect(exposedNextArg.name).to.be.equal(expectedRedirectName) } } const createNoRedirectTest = function (state, fromName) { return function () { - let exposedOptions - const RouterClass = function (options) { - exposedOptions = options - } - const storeMock = createStoreMock() - storeMock.state = state - createRouter(storeMock, RouterClass) - expect(exposedOptions).to.be.a('object') - const {routes} = exposedOptions - expect(routes).to.be.instanceOf(Array) - const routeOption = routes.filter(r => r.name === fromName)[0] - const {beforeEnter} = routeOption + const beforeEnter = getBeforeEnterHandler(state, fromName) const handlerType = typeof beforeEnter - expect( - handlerType === 'function' || handlerType === 'undefined' - ).to.be.equal(true) + expect(['function', 'undefined']).to.include(handlerType) if (handlerType === 'function') { - let exposedNextArg - const next = sinon.spy(arg => { - exposedNextArg = arg - }) - beforeEnter(undefined, undefined, next) - expect(next).to.have.been.calledOnce + const exposedNextArg = getNextArg(beforeEnter) expect(exposedNextArg).to.be.a('undefined') } } From f61157d58983f70f6aa2a37e2d41b4c12c5f8feb Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sun, 21 Jan 2018 16:35:52 +1000 Subject: [PATCH 2/2] Simplify retrieving of mock args --- test/unit/specs/routing.spec.js | 52 ++++++++++----------------------- 1 file changed, 15 insertions(+), 37 deletions(-) diff --git a/test/unit/specs/routing.spec.js b/test/unit/specs/routing.spec.js index d268b5b7c..cd2b53ed0 100644 --- a/test/unit/specs/routing.spec.js +++ b/test/unit/specs/routing.spec.js @@ -5,22 +5,16 @@ describe('router/index.js', function () { } describe('guarding routes', function () { - const createStoreMock = () => { - return { - subscribe: NO_OP, - subscribeAction: NO_OP - } - } + const createStoreMock = (store) => ({ + subscribe: NO_OP, + ...store + }) describe('routing when route change is requested', function () { const getBeforeEnterHandler = (state, fromName) => { - let exposedOptions - const RouterClass = function (options) { - exposedOptions = options - } - const storeMock = createStoreMock() - storeMock.state = state - createRouter(storeMock, RouterClass) + const RouterClass = sinon.spy() + createRouter(createStoreMock({ state }), RouterClass) + const exposedOptions = RouterClass.firstCall.args[0] expect(exposedOptions).to.be.a('object') const {routes} = exposedOptions expect(routes).to.be.instanceOf(Array) @@ -29,13 +23,10 @@ describe('router/index.js', function () { } const getNextArg = beforeEnter => { - let exposedNextArg - const next = sinon.spy(arg => { - exposedNextArg = arg - }) + const next = sinon.spy() beforeEnter(undefined, undefined, next) expect(next).to.have.been.calledOnce - return exposedNextArg + return next.firstCall.args[0] } const createRedirectTest = function (state, fromName, expectedRedirectName) { @@ -131,16 +122,10 @@ describe('router/index.js', function () { const createRedirectTest = function (state, mutationType, expectedRedirect, currentPath) { return function () { - let exposedHandler - const subscribe = handler => { - exposedHandler = handler - } - const storeMock = { - subscribe, - subscribeAction: NO_OP - } + const subscribe = sinon.spy() const mutation = {type: mutationType} - const routerMock = createRouter(storeMock, RouterClass) + const routerMock = createRouter(createStoreMock({ subscribe }), RouterClass) + const exposedHandler = subscribe.firstCall.args[0] exposedHandler(mutation, state) expect(routerMock.push).to.have.been.calledOnce expect(routerMock.push).to.have.been.calledWith({name: expectedRedirect}) @@ -148,17 +133,10 @@ describe('router/index.js', function () { } it('registers a listener for vuex mutations', () => { - let exposedHandler - const subscribe = sinon.spy(handler => { - exposedHandler = handler - }) - - const storeMock = { - subscribe, - subscribeAction: NO_OP - } + const subscribe = sinon.spy() + createRouter(createStoreMock({ subscribe })) + const exposedHandler = subscribe.firstCall.args[0] - createRouter(storeMock) expect(subscribe).to.have.been.calledOnce expect(exposedHandler).to.be.a('function') })