1
1
import _getMetaInfo from '../src/shared/getMetaInfo'
2
- import { mount , defaultOptions , loadVueMetaPlugin } from './utils'
2
+ import { mount , loadVueMetaPlugin , vmTick } from './utils'
3
+ import { defaultOptions } from './utils/constants'
3
4
4
5
import GoodbyeWorld from './fixtures/goodbye-world.vue'
5
6
import HelloWorld from './fixtures/hello-world.vue'
@@ -8,10 +9,31 @@ import Changed from './fixtures/changed.vue'
8
9
9
10
const getMetaInfo = component => _getMetaInfo ( defaultOptions , component )
10
11
12
+ jest . mock ( '../src/shared/window' , ( ) => ( {
13
+ hasGlobalWindow : false
14
+ } ) )
15
+
11
16
describe ( 'client' , ( ) => {
12
17
let Vue
18
+ let html
19
+
20
+ beforeAll ( ( ) => {
21
+ Vue = loadVueMetaPlugin ( )
22
+
23
+ // force using timers, jest cant mock rAF
24
+ delete window . requestAnimationFrame
25
+ delete window . cancelAnimationFrame
13
26
14
- beforeAll ( ( ) => ( Vue = loadVueMetaPlugin ( ) ) )
27
+ html = document . createElement ( 'html' )
28
+ document . _getElementsByTagName = document . getElementsByTagName
29
+ jest . spyOn ( document , 'getElementsByTagName' ) . mockImplementation ( ( tag ) => {
30
+ if ( tag === 'html' ) {
31
+ return [ html ]
32
+ }
33
+
34
+ return document . _getElementsByTagName ( tag )
35
+ } )
36
+ } )
15
37
16
38
test ( 'meta-info refreshed on component\'s data change' , ( ) => {
17
39
const wrapper = mount ( HelloWorld , { localVue : Vue } )
@@ -78,20 +100,59 @@ describe('client', () => {
78
100
expect ( metaInfo . title . text ( ) ) . toEqual ( '<title data-vue-meta="true">Hello World</title>' )
79
101
} )
80
102
81
- test ( 'changed function is called' , ( ) => {
103
+ test ( 'doesnt update when ssr attribute is set' , ( ) => {
104
+ html . setAttribute ( defaultOptions . ssrAttribute , 'true' )
105
+ const wrapper = mount ( HelloWorld , { localVue : Vue } )
106
+
107
+ const { tags } = wrapper . vm . $meta ( ) . refresh ( )
108
+ expect ( tags ) . toBe ( false )
109
+ } )
110
+
111
+ test ( 'changed function is called' , async ( ) => {
82
112
const parentComponent = new Vue ( { render : h => h ( 'div' ) } )
83
113
const wrapper = mount ( Changed , { localVue : Vue , parentComponent } )
84
114
115
+ await vmTick ( wrapper . vm )
116
+ expect ( wrapper . vm . $root . _vueMeta . initialized ) . toBe ( true )
117
+
85
118
let context
86
119
const changed = jest . fn ( function ( ) {
87
120
context = this
88
121
} )
89
- wrapper . setData ( { changed } )
90
- wrapper . setData ( { childVisible : true } )
122
+ wrapper . setData ( { changed, childVisible : true } )
123
+ jest . runAllTimers ( )
91
124
92
- wrapper . vm . $parent . $meta ( ) . refresh ( )
93
125
expect ( changed ) . toHaveBeenCalledTimes ( 1 )
94
126
// TODO: this isnt what the docs say
95
127
expect ( context . _uid ) . not . toBe ( wrapper . vm . _uid )
96
128
} )
129
+
130
+ test ( 'afterNavigation function is called' , ( ) => {
131
+ const Vue = loadVueMetaPlugin ( false , { refreshOnceOnNavigation : true } )
132
+ const afterNavigation = jest . fn ( )
133
+ const component = Vue . component ( 'nav-component' , {
134
+ render : h => h ( 'div' ) ,
135
+ metaInfo : { afterNavigation }
136
+ } )
137
+
138
+ const guards = { }
139
+ Vue . prototype . $router = {
140
+ beforeEach ( fn ) {
141
+ guards . before = fn
142
+ } ,
143
+ afterEach ( fn ) {
144
+ guards . after = fn
145
+ }
146
+ }
147
+ const wrapper = mount ( component , { localVue : Vue } )
148
+
149
+ expect ( guards . before ) . toBeDefined ( )
150
+ expect ( guards . after ) . toBeDefined ( )
151
+
152
+ guards . before ( null , null , ( ) => { } )
153
+ expect ( wrapper . vm . $root . _vueMeta . paused ) . toBe ( true )
154
+
155
+ guards . after ( )
156
+ expect ( afterNavigation ) . toHaveBeenCalled ( )
157
+ } )
97
158
} )
0 commit comments