Skip to content

Commit df8bdda

Browse files
committed
feat(hooks): add useRouter hook
1 parent d61e363 commit df8bdda

File tree

6 files changed

+87
-27
lines changed

6 files changed

+87
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export default createComponent({
4545

4646
## Hooks
4747

48+
- [`useRouter`](./src/useRouter.ts) — A hook for [`vue-router`](https://github.com/vuejs/vue-router).
4849
- [`useDate`](./src/useDate.ts) — Using [`dayjs`](https://github.com/iamkun/dayjs) to process date.
4950
- [`useWindowSize`](./src/useWindowSize.ts) — Tracks `window` dimensions.
5051

src/__tests__/useRouter.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import VueRouter, { Route } from 'vue-router';
2+
import useRouter from '../useRouter';
3+
import renderHook from '../util/renderHook';
4+
5+
declare module 'vue/types/vue' {
6+
interface Vue {
7+
route: Route;
8+
router: VueRouter;
9+
}
10+
}
11+
12+
describe('useRouter', () => {
13+
it('should be defined', () => {
14+
expect(useRouter).toBeDefined();
15+
});
16+
17+
it('shuold have route', () => {
18+
const { vm } = renderHook(useRouter);
19+
expect(vm).toHaveProperty('route');
20+
expect(vm).toHaveProperty('router');
21+
});
22+
23+
it('should route to be correct', () => {
24+
const { vm } = renderHook(useRouter);
25+
expect(vm.route.name).toBe('index');
26+
expect(vm.route.meta.title).toBe('Vue Hooks');
27+
28+
vm.router.push('/test');
29+
30+
expect(vm.route.name).toBe('404');
31+
expect(vm.route.meta.title).toBe('404 - Not Found');
32+
});
33+
});

src/useRouter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { computed } from 'vue-function-api';
2+
import withContext from './util/withContext';
3+
4+
export default withContext(function useRouter(vue) {
5+
const route = computed(() => vue.$route);
6+
const router = computed(() => vue.$router);
7+
return { route, router };
8+
});

src/util/renderHook.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
/* eslint import/no-extraneous-dependencies: off */
2-
import Vue from 'vue';
2+
import { shallowMount, createLocalVue } from '@vue/test-utils';
33
import { plugin, createComponent } from 'vue-function-api';
4-
import { mount } from '@vue/test-utils';
4+
import VueRouter from 'vue-router';
55

6-
Vue.use(plugin);
6+
const localVue = createLocalVue();
7+
8+
localVue.use(VueRouter);
9+
localVue.use(plugin);
10+
11+
const router = new VueRouter({
12+
routes: [
13+
{
14+
path: '/',
15+
name: 'index',
16+
meta: { title: 'Vue Hooks' },
17+
},
18+
{
19+
path: '*',
20+
name: '404',
21+
meta: { title: '404 - Not Found' },
22+
},
23+
],
24+
});
725

826
export default function renderHook(hook: Function) {
9-
const vm = createComponent({
10-
template: `<div></div>`,
11-
setup() {
12-
return hook();
27+
const App = createComponent({
28+
template: `
29+
<div id="app">
30+
<router-view></router-view>
31+
</div>
32+
`,
33+
34+
setup(_, context) {
35+
return hook(context);
1336
},
1437
});
1538

16-
return mount(vm);
39+
return shallowMount(App, {
40+
localVue,
41+
router,
42+
stubs: ['router-view'],
43+
});
1744
}

src/util/vue.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/util/withContext.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Vue from 'vue';
2+
import { Context } from 'vue-function-api/dist/types/vue';
3+
4+
export default function withContext(hook: (vue: Vue) => any) {
5+
return (context: Context) => {
6+
const test = process.env.NODE_ENV === 'test';
7+
const vue = test ? context.parent.$children[0] : context.root;
8+
return hook(vue);
9+
};
10+
}

0 commit comments

Comments
 (0)