-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
87 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import VueRouter, { Route } from 'vue-router'; | ||
import useRouter from '../useRouter'; | ||
import renderHook from '../util/renderHook'; | ||
|
||
declare module 'vue/types/vue' { | ||
interface Vue { | ||
route: Route; | ||
router: VueRouter; | ||
} | ||
} | ||
|
||
describe('useRouter', () => { | ||
it('should be defined', () => { | ||
expect(useRouter).toBeDefined(); | ||
}); | ||
|
||
it('shuold have route', () => { | ||
const { vm } = renderHook(useRouter); | ||
expect(vm).toHaveProperty('route'); | ||
expect(vm).toHaveProperty('router'); | ||
}); | ||
|
||
it('should route to be correct', () => { | ||
const { vm } = renderHook(useRouter); | ||
expect(vm.route.name).toBe('index'); | ||
expect(vm.route.meta.title).toBe('Vue Hooks'); | ||
|
||
vm.router.push('/test'); | ||
|
||
expect(vm.route.name).toBe('404'); | ||
expect(vm.route.meta.title).toBe('404 - Not Found'); | ||
}); | ||
}); |
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,8 @@ | ||
import { computed } from 'vue-function-api'; | ||
import withContext from './util/withContext'; | ||
|
||
export default withContext(function useRouter(vue) { | ||
const route = computed(() => vue.$route); | ||
const router = computed(() => vue.$router); | ||
return { route, router }; | ||
}); |
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 |
---|---|---|
@@ -1,17 +1,44 @@ | ||
/* eslint import/no-extraneous-dependencies: off */ | ||
import Vue from 'vue'; | ||
import { shallowMount, createLocalVue } from '@vue/test-utils'; | ||
import { plugin, createComponent } from 'vue-function-api'; | ||
import { mount } from '@vue/test-utils'; | ||
import VueRouter from 'vue-router'; | ||
|
||
Vue.use(plugin); | ||
const localVue = createLocalVue(); | ||
|
||
localVue.use(VueRouter); | ||
localVue.use(plugin); | ||
|
||
const router = new VueRouter({ | ||
routes: [ | ||
{ | ||
path: '/', | ||
name: 'index', | ||
meta: { title: 'Vue Hooks' }, | ||
}, | ||
{ | ||
path: '*', | ||
name: '404', | ||
meta: { title: '404 - Not Found' }, | ||
}, | ||
], | ||
}); | ||
|
||
export default function renderHook(hook: Function) { | ||
const vm = createComponent({ | ||
template: `<div></div>`, | ||
setup() { | ||
return hook(); | ||
const App = createComponent({ | ||
template: ` | ||
<div id="app"> | ||
<router-view></router-view> | ||
</div> | ||
`, | ||
|
||
setup(_, context) { | ||
return hook(context); | ||
}, | ||
}); | ||
|
||
return mount(vm); | ||
return shallowMount(App, { | ||
localVue, | ||
router, | ||
stubs: ['router-view'], | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
import Vue from 'vue'; | ||
import { Context } from 'vue-function-api/dist/types/vue'; | ||
|
||
export default function withContext(hook: (vue: Vue) => any) { | ||
return (context: Context) => { | ||
const test = process.env.NODE_ENV === 'test'; | ||
const vue = test ? context.parent.$children[0] : context.root; | ||
return hook(vue); | ||
}; | ||
} |