From: Eduardo San Martin Morote Date: Mon, 6 May 2019 17:08:22 +0000 (+0200) Subject: test: add afterEach test for nested routes X-Git-Tag: v4.0.0-alpha.0~379 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ae89458d3893c225938efa0f149166d89bf51b8;p=thirdparty%2Fvuejs%2Frouter.git test: add afterEach test for nested routes --- diff --git a/__tests__/guards/global-after.spec.js b/__tests__/guards/global-after.spec.js index 0f55274b..7a436871 100644 --- a/__tests__/guards/global-after.spec.js +++ b/__tests__/guards/global-after.spec.js @@ -17,11 +17,20 @@ function createRouter(options) { const Home = { template: `
Home
` } const Foo = { template: `
Foo
` } +const Nested = { template: `
Nested
` } /** @type {import('../../src/types').RouteRecord[]} */ const routes = [ { path: '/', component: Home }, { path: '/foo', component: Foo }, + { + path: '/nested', + component: Nested, + children: [ + { path: '', name: 'nested-default', component: Foo }, + { path: 'home', name: 'nested-home', component: Home }, + ], + }, ] describe('router.afterEach', () => { @@ -52,6 +61,25 @@ describe('router.afterEach', () => { expect(spy).not.toHaveBeenCalled() }) + it('calls afterEach guards on push', async () => { + const spy = jest.fn() + const router = createRouter({ routes }) + await router.push('/nested') + router.afterEach(spy) + await router[navigationMethod]('/nested/home') + expect(spy).toHaveBeenCalledTimes(1) + expect(spy).toHaveBeenLastCalledWith( + expect.objectContaining({ name: 'nested-home' }), + expect.objectContaining({ name: 'nested-default' }) + ) + await router[navigationMethod]('/nested') + expect(spy).toHaveBeenLastCalledWith( + expect.objectContaining({ name: 'nested-default' }), + expect.objectContaining({ name: 'nested-home' }) + ) + expect(spy).toHaveBeenCalledTimes(2) + }) + it('does not call afterEach if navigation is cancelled', async () => { const spy = jest.fn() const router = createRouter({ routes }) diff --git a/__tests__/guards/global-beforeEach.spec.js b/__tests__/guards/global-beforeEach.spec.js index 91fde0d5..a2d288cb 100644 --- a/__tests__/guards/global-beforeEach.spec.js +++ b/__tests__/guards/global-beforeEach.spec.js @@ -20,8 +20,8 @@ function createRouter(options) { } const Home = { template: `
Home
` } -const Nested = { template: `
Nested
` } const Foo = { template: `
Foo
` } +const Nested = { template: `
Nested
` } /** @type {RouteRecord[]} */ const routes = [