Skip to content

Instantly share code, notes, and snippets.

@rchl
Created December 5, 2018 10:25
Show Gist options
  • Save rchl/2418f5c40e66f9f27a34f89f83960b9a to your computer and use it in GitHub Desktop.
Save rchl/2418f5c40e66f9f27a34f89f83960b9a to your computer and use it in GitHub Desktop.
/*eslint-disable no-undef*/
import { createMatcher } from '../../../src/create-matcher'
const routes = [
{ path: '/', name: 'home', component: { name: 'home' }},
{ path: '/foo', name: 'foo', component: { name: 'foo' }},
{ path: '/baz/:testparam', name: 'baz', component: { name: 'baz' }},
{ path: '*', name: 'catch-all', props: true, component: { name: 'notFound' }}
]
describe('Creating Matcher', function () {
let match
beforeAll(function () {
spyOn(console, 'warn')
match = createMatcher(routes).match
})
beforeEach(function () {
console.warn.calls.reset()
process.env.NODE_ENV = 'production'
})
afterAll(function () {
process.env.NODE_ENV = 'production'
})
it('in development, has logged a warning if a named route does not exist', function () {
process.env.NODE_ENV = 'development'
const { name, matched } = match({ name: 'bar' }, routes[0])
expect(matched.length).toBe(0)
expect(name).toBe('bar')
expect(console.warn).toHaveBeenCalled()
expect(console.warn.calls.argsFor(0)[0]).toMatch('Route with name \'bar\' does not exist')
})
it('in production, it has not logged this warning', function () {
match({ name: 'foo' }, routes[0])
expect(console.warn).not.toHaveBeenCalled()
})
it('matches named route with params without warning', function () {
process.env.NODE_ENV = 'development'
const { name, path } = match({ name: 'baz', params: { testparam: 'testvalue' }})
expect(console.warn).not.toHaveBeenCalled()
expect(name).toEqual('baz')
expect(path).toEqual('/baz/testvalue')
})
it('matches asterisk routes with a default param name', function () {
process.env.NODE_ENV = 'development'
const { params } = match({ path: '/not-found' }, routes[0])
expect(console.warn).not.toHaveBeenCalled()
expect(params).toEqual({ pathMatch: '/not-found' })
})
it('matches asterisk routes using name and params', function () {
process.env.NODE_ENV = 'development'
const { path, params } = match({ name: 'catch-all', params: { pathMatch: '/other' }})
expect(console.warn).not.toHaveBeenCalled()
expect(path).toEqual('/other')
expect(params).toEqual({ pathMatch: '/other' })
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment