},
]
+function createRouter(...options: ConstructorParameters<typeof Router>) {
+ return new Router(...options)
+}
+
describe('Router', () => {
beforeAll(() => {
createDom()
it('can be instantiated', () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
expect(router.currentRoute).toEqual({
name: undefined,
fullPath: '/',
it.skip('takes browser location', () => {
const history = createMemoryHistory()
history.replace('/search?q=dog#footer')
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
expect(router.currentRoute).toEqual({
fullPath: '/search?q=dog#footer',
hash: '#footer',
it('calls history.push with router.push', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
jest.spyOn(history, 'push')
await router.push('/foo')
expect(history.push).toHaveBeenCalledTimes(1)
it('calls history.replace with router.replace', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
jest.spyOn(history, 'replace')
await router.replace('/foo')
expect(history.replace).toHaveBeenCalledTimes(1)
it('can pass replace option to push', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
jest.spyOn(history, 'replace')
await router.push({ path: '/foo', replace: true })
expect(history.replace).toHaveBeenCalledTimes(1)
const [p1, r1] = fakePromise()
const [p2, r2] = fakePromise()
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
router.beforeEach(async (to, from, next) => {
if (to.name !== 'Param') return next()
if (to.params.p === 'a') {
const [p1, r1] = fakePromise()
const [p2, r2] = fakePromise()
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
// navigate first to add entries to the history stack
await router.push('/foo')
await router.push('/p/a')
describe('matcher', () => {
it('handles one redirect from route record', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
const loc = await router.push('/to-foo')
expect(loc.name).toBe('Foo')
expect(loc.redirectedFrom).toMatchObject({
it('drops query and params on redirect if not provided', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
const loc = await router.push('/to-foo?hey=foo#fa')
expect(loc.name).toBe('Foo')
expect(loc.query).toEqual({})
it('allows object in redirect', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
const loc = await router.push('/to-foo-named')
expect(loc.name).toBe('Foo')
expect(loc.redirectedFrom).toMatchObject({
it('can pass on query and hash when redirecting', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
const loc = await router.push('/inc-query-hash?n=3#fa')
expect(loc).toMatchObject({
name: 'Foo',
it('handles multiple redirect fields in route record', async () => {
const history = createMemoryHistory()
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
const loc = await router.push('/to-foo2')
expect(loc.name).toBe('Foo')
expect(loc.redirectedFrom).toMatchObject({
it('allows base option in abstract history', async () => {
const history = createMemoryHistory('/app/')
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
expect(router.currentRoute).toEqual({
name: undefined,
fullPath: '/',
it('allows base option with html5 history', async () => {
const history = createHistory('/app/')
- const router = new Router({ history, routes })
+ const router = createRouter({ history, routes })
expect(router.currentRoute).toEqual({
name: undefined,
fullPath: '/',