From: Eduardo San Martin Morote Date: Mon, 6 May 2019 20:14:51 +0000 (+0200) Subject: test: beforeEnter nested routes X-Git-Tag: v4.0.0-alpha.0~372 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5bbceb12c38e8f814d7b8503815f77f0cbbb5bfa;p=thirdparty%2Fvuejs%2Frouter.git test: beforeEnter nested routes --- diff --git a/__tests__/guards/route-beforeEnter.spec.js b/__tests__/guards/route-beforeEnter.spec.js index ed48bcce..e3720cfa 100644 --- a/__tests__/guards/route-beforeEnter.spec.js +++ b/__tests__/guards/route-beforeEnter.spec.js @@ -24,6 +24,16 @@ const Foo = { template: `
Foo
` } const beforeEnter = jest.fn() const beforeEnters = [jest.fn(), jest.fn()] +const nested = { + parent: jest.fn(), + nestedEmpty: jest.fn(), + nestedA: jest.fn(), + nestedAbs: jest.fn(), + nestedNested: jest.fn(), + nestedNestedFoo: jest.fn(), + nestedNestedParam: jest.fn(), +} + /** @type {RouteRecord[]} */ const routes = [ { path: '/', component: Home }, @@ -39,6 +49,47 @@ const routes = [ beforeEnter: beforeEnters, component: Foo, }, + { + path: '/nested', + component: { + ...Home, + beforeRouteEnter: nested.parent, + }, + children: [ + { + path: '', + name: 'nested-empty-path', + component: { ...Home, beforeRouteEnter: nested.nestedEmpty }, + }, + { + path: 'a', + name: 'nested-path', + component: { ...Home, beforeRouteEnter: nested.nestedA }, + }, + { + path: '/abs-nested', + name: 'absolute-nested', + component: { ...Home, beforeRouteEnter: nested.nestedAbs }, + }, + { + path: 'nested', + name: 'nested-nested', + component: { ...Home, beforeRouteEnter: nested.nestedNested }, + children: [ + { + path: 'foo', + name: 'nested-nested-foo', + component: { ...Home, beforeRouteEnter: nested.nestedNestedFoo }, + }, + { + path: 'param/:p', + name: 'nested-nested-param', + component: { ...Home, beforeRouteEnter: nested.nestedNestedParam }, + }, + ], + }, + ], + }, ] function resetMocks() { @@ -47,6 +98,10 @@ function resetMocks() { spy.mockReset() spy.mockImplementationOnce(noGuard) }) + for (const key in nested) { + nested[key].mockReset() + nested[key].mockImplementation(noGuard) + } } beforeEach(() => { @@ -79,6 +134,27 @@ describe('beforeEnter', () => { ) }) + it('call beforeEnter in nested views', async () => { + const router = createRouter({ routes }) + await router.push('/nested/a') + resetMocks() + await router[navigationMethod]('/nested/nested/foo') + expect(nested.parent).not.toHaveBeenCalled() + expect(nested.nestedA).not.toHaveBeenCalled() + expect(nested.nestedNested).toHaveBeenCalledTimes(1) + expect(nested.nestedNestedFoo).toHaveBeenCalledTimes(1) + expect(nested.nestedNested).toHaveBeenCalledWith( + expect.objectContaining({ path: '/nested/nested/foo' }), + expect.objectContaining({ path: '/nested/a' }), + expect.any(Function) + ) + expect(nested.nestedNestedFoo).toHaveBeenCalledWith( + expect.objectContaining({ path: '/nested/nested/foo' }), + expect.objectContaining({ path: '/nested/a' }), + expect.any(Function) + ) + }) + it('calls beforeEnter different records, same component', async () => { const router = createRouter({ routes }) beforeEnter.mockImplementationOnce(noGuard)