-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from kitbagjs/plugins
New Feature: Router Plugins
- Loading branch information
Showing
11 changed files
with
302 additions
and
41 deletions.
There are no files selected for viewing
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
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
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,106 @@ | ||
import { test, expect, vi } from 'vitest' | ||
import { createRoute } from './createRoute' | ||
import { createRouter } from './createRouter' | ||
import { createRouterPlugin } from './createRouterPlugin' | ||
import { component, routes } from '@/utilities/testHelpers' | ||
import { flushPromises } from '@vue/test-utils' | ||
import { mount } from '@vue/test-utils' | ||
|
||
test('given a plugin, adds the routes to the router', async () => { | ||
const plugin = createRouterPlugin({ | ||
routes: [createRoute({ name: 'plugin', path: '/plugin', component })], | ||
rejections: { | ||
plugin: component, | ||
}, | ||
}) | ||
|
||
const router = createRouter([], { initialUrl: '/plugin' }, [plugin]) | ||
|
||
await router.start() | ||
|
||
expect(router.route.name).toBe('plugin') | ||
}) | ||
|
||
test('given a plugin, adds the rejections to the router', async () => { | ||
const plugin = createRouterPlugin({ | ||
rejections: { | ||
plugin: { template: '<div>This is a plugin rejection</div>' }, | ||
}, | ||
}) | ||
|
||
const router = createRouter([], { initialUrl: '/' }, [plugin]) | ||
|
||
await router.start() | ||
|
||
const root = { | ||
template: '<RouterView/>', | ||
} | ||
|
||
const wrapper = mount(root, { | ||
global: { | ||
plugins: [router], | ||
}, | ||
}) | ||
|
||
await router.reject('plugin') | ||
|
||
await flushPromises() | ||
|
||
expect(wrapper.html()).toBe('<div>This is a plugin rejection</div>') | ||
}) | ||
|
||
test('given a plugin, adds the hooks to the router', async () => { | ||
const plugin = createRouterPlugin({ | ||
onBeforeRouteEnter: vi.fn(), | ||
onBeforeRouteUpdate: vi.fn(), | ||
onBeforeRouteLeave: vi.fn(), | ||
onAfterRouteEnter: vi.fn(), | ||
onAfterRouteUpdate: vi.fn(), | ||
onAfterRouteLeave: vi.fn(), | ||
}) | ||
|
||
const router = createRouter(routes, { initialUrl: '/parentA/valueA' }, [plugin]) | ||
|
||
expect(plugin.onBeforeRouteEnter).toHaveBeenCalledTimes(0) | ||
expect(plugin.onBeforeRouteUpdate).toHaveBeenCalledTimes(0) | ||
expect(plugin.onBeforeRouteLeave).toHaveBeenCalledTimes(0) | ||
expect(plugin.onAfterRouteLeave).toHaveBeenCalledTimes(0) | ||
expect(plugin.onAfterRouteUpdate).toHaveBeenCalledTimes(0) | ||
expect(plugin.onAfterRouteEnter).toHaveBeenCalledTimes(0) | ||
|
||
await router.start() | ||
|
||
expect(plugin.onBeforeRouteEnter).toHaveBeenCalledTimes(1) | ||
expect(plugin.onBeforeRouteUpdate).toHaveBeenCalledTimes(0) | ||
expect(plugin.onBeforeRouteLeave).toHaveBeenCalledTimes(1) | ||
expect(plugin.onAfterRouteLeave).toHaveBeenCalledTimes(1) | ||
expect(plugin.onAfterRouteUpdate).toHaveBeenCalledTimes(0) | ||
expect(plugin.onAfterRouteEnter).toHaveBeenCalledTimes(1) | ||
|
||
await router.push('parentA.childA', { paramA: 'valueA', paramB: 'valueB' }) | ||
|
||
expect(plugin.onBeforeRouteEnter).toHaveBeenCalledTimes(1) | ||
expect(plugin.onBeforeRouteUpdate).toHaveBeenCalledTimes(1) | ||
expect(plugin.onBeforeRouteLeave).toHaveBeenCalledTimes(1) | ||
expect(plugin.onAfterRouteLeave).toHaveBeenCalledTimes(1) | ||
expect(plugin.onAfterRouteUpdate).toHaveBeenCalledTimes(1) | ||
expect(plugin.onAfterRouteEnter).toHaveBeenCalledTimes(1) | ||
|
||
await router.push('parentA.childB', { paramA: 'valueB', paramD: 'valueD' }) | ||
|
||
expect(plugin.onBeforeRouteEnter).toHaveBeenCalledTimes(1) | ||
expect(plugin.onBeforeRouteUpdate).toHaveBeenCalledTimes(2) | ||
expect(plugin.onBeforeRouteLeave).toHaveBeenCalledTimes(1) | ||
expect(plugin.onAfterRouteLeave).toHaveBeenCalledTimes(1) | ||
expect(plugin.onAfterRouteUpdate).toHaveBeenCalledTimes(2) | ||
expect(plugin.onAfterRouteEnter).toHaveBeenCalledTimes(1) | ||
|
||
await router.push('parentB') | ||
|
||
expect(plugin.onBeforeRouteEnter).toHaveBeenCalledTimes(2) | ||
expect(plugin.onBeforeRouteUpdate).toHaveBeenCalledTimes(2) | ||
expect(plugin.onBeforeRouteLeave).toHaveBeenCalledTimes(2) | ||
expect(plugin.onAfterRouteLeave).toHaveBeenCalledTimes(2) | ||
expect(plugin.onAfterRouteUpdate).toHaveBeenCalledTimes(2) | ||
expect(plugin.onAfterRouteEnter).toHaveBeenCalledTimes(2) | ||
}) |
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,42 @@ | ||
import { Routes } from '@/types/route' | ||
import { Router } from '@/types/router' | ||
import { RouterPlugin } from '@/types/routerPlugin' | ||
import { asArray } from '@/utilities/array' | ||
import { Component } from 'vue' | ||
|
||
export function createRouterPlugin< | ||
TRoutes extends Routes = [], | ||
TRejections extends Record<string, Component> = {} | ||
>(plugin: Partial<RouterPlugin<TRoutes, TRejections>>): RouterPlugin<TRoutes, TRejections> { | ||
return { | ||
routes: plugin.routes ?? [] as unknown as TRoutes, | ||
rejections: plugin.rejections ?? {} as TRejections, | ||
...plugin, | ||
} | ||
} | ||
|
||
export function addRouterPluginHooks(router: Router, plugin: RouterPlugin): void { | ||
if (plugin.onBeforeRouteEnter) { | ||
asArray(plugin.onBeforeRouteEnter).forEach((hook) => router.onBeforeRouteEnter(hook)) | ||
} | ||
|
||
if (plugin.onAfterRouteEnter) { | ||
asArray(plugin.onAfterRouteEnter).forEach((hook) => router.onAfterRouteEnter(hook)) | ||
} | ||
|
||
if (plugin.onBeforeRouteUpdate) { | ||
asArray(plugin.onBeforeRouteUpdate).forEach((hook) => router.onBeforeRouteUpdate(hook)) | ||
} | ||
|
||
if (plugin.onAfterRouteUpdate) { | ||
asArray(plugin.onAfterRouteUpdate).forEach((hook) => router.onAfterRouteUpdate(hook)) | ||
} | ||
|
||
if (plugin.onBeforeRouteLeave) { | ||
asArray(plugin.onBeforeRouteLeave).forEach((hook) => router.onBeforeRouteLeave(hook)) | ||
} | ||
|
||
if (plugin.onAfterRouteLeave) { | ||
asArray(plugin.onAfterRouteLeave).forEach((hook) => router.onAfterRouteLeave(hook)) | ||
} | ||
} |
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
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
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
Oops, something went wrong.