Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to router unit tests #34

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 33 additions & 63 deletions test/unit/specs/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,46 @@ 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) => {
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)
const routeOption = routes.filter(r => r.name === fromName)[0]
return routeOption.beforeEnter
}

const getNextArg = beforeEnter => {
const next = sinon.spy()
beforeEnter(undefined, undefined, next)
expect(next).to.have.been.calledOnce
return next.firstCall.args[0]
}

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')
}
}
Expand Down Expand Up @@ -139,34 +122,21 @@ 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})
}
}

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')
})
Expand Down