File tree Expand file tree Collapse file tree 6 files changed +87
-27
lines changed Expand file tree Collapse file tree 6 files changed +87
-27
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ export default createComponent({
4545
4646## Hooks  
4747
48+ -  [ ` useRouter ` ] ( ./src/useRouter.ts )  &mdash ;  A hook for [ ` vue-router ` ] ( https://github.com/vuejs/vue-router ) .
4849-  [ ` useDate ` ] ( ./src/useDate.ts )  &mdash ;  Using [ ` dayjs ` ] ( https://github.com/iamkun/dayjs )  to process date.
4950-  [ ` useWindowSize ` ] ( ./src/useWindowSize.ts )  &mdash ;  Tracks ` window `  dimensions.
5051
Original file line number Diff line number Diff line change 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+ } ) ; 
Original file line number Diff line number Diff line change 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+ } ) ; 
Original file line number Diff line number Diff line change 11/* eslint import/no-extraneous-dependencies: off */ 
2- import  Vue   from  'vue' ; 
2+ import  {   shallowMount ,   createLocalVue   }   from  '@ vue/test-utils ' ; 
33import  {  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
826export  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} 
  Load Diff This file was deleted. 
Original file line number Diff line number Diff line change 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+ } 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments