forked from vuejs/router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow inject within global navigation guards
- Loading branch information
Showing
2 changed files
with
71 additions
and
8 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
packages/router/__tests__/guards/navigatioGuardsInjections.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import { createDom, newRouter as createRouter } from '../utils' | ||
import { mount } from '@vue/test-utils' | ||
import { inject } from 'vue' | ||
import { mockWarn } from 'jest-mock-warn' | ||
import type { Router } from '../../src' | ||
|
||
describe('inject() within navigation guards', () => { | ||
mockWarn() | ||
beforeAll(() => { | ||
createDom() | ||
}) | ||
|
||
const PageComponent = { | ||
template: `<div>Page</div>`, | ||
} | ||
|
||
function factory(router: Router) { | ||
return mount( | ||
{ | ||
template: `<RouterView />`, | ||
}, | ||
{ | ||
global: { | ||
plugins: [router], | ||
provide: { | ||
test: 'hello', | ||
}, | ||
}, | ||
} | ||
) | ||
} | ||
|
||
const globalGuards = ['beforeEach', 'beforeResolve', 'afterEach'] as const | ||
|
||
for (const guardName of globalGuards) { | ||
it(`router.${guardName}()`, async () => { | ||
expect.assertions(1) | ||
const router = createRouter({ | ||
routes: [{ path: '/', component: PageComponent }], | ||
}) | ||
router[guardName](() => { | ||
expect(inject('test')).toBe('hello') | ||
}) | ||
factory(router) | ||
await router.isReady() | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters