Skip to content

Commit fc57998

Browse files
committed
test: fix ssr hydration tests
1 parent b2a7a84 commit fc57998

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

test/e2e/ssr.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('basic browser with ssr page', () => {
1010

1111
test('validate ssr', () => {
1212
const htmlTag = html.match(/<html([^>]+)>/)[0]
13-
expect(htmlTag).toContain('data-vue-meta-server-rendered')
13+
expect(htmlTag).toContain('data-vue-meta-server-rendered ')
1414
expect(htmlTag).toContain(' lang="en" ')
1515
expect(htmlTag).toContain(' amp ')
1616
expect(htmlTag).not.toContain('allowfullscreen')

test/unit/components.test.js

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _getMetaInfo from '../../src/shared/getMetaInfo'
2-
import { mount, loadVueMetaPlugin, vmTick } from '../utils'
2+
import { mount, createWrapper, loadVueMetaPlugin, vmTick } from '../utils'
33
import { defaultOptions } from '../../src/shared/constants'
44

55
import GoodbyeWorld from '../components/goodbye-world.vue'
@@ -102,12 +102,26 @@ describe('client', () => {
102102

103103
test('doesnt update when ssr attribute is set', () => {
104104
html.setAttribute(defaultOptions.ssrAttribute, 'true')
105-
const wrapper = mount(HelloWorld, { localVue: Vue })
105+
106+
const el = document.createElement('div')
107+
el.setAttribute('id', 'app')
108+
el.setAttribute('data-server-rendered', true)
109+
document.body.appendChild(el)
110+
111+
const Component = Vue.extend({
112+
metaInfo: { title: 'Test' },
113+
render(h) {
114+
return h('div', null, 'Test')
115+
}
116+
})
117+
118+
const vm = new Component().$mount(el)
119+
const wrapper = createWrapper(vm, { attachToDocument: true })
106120

107121
const { tags } = wrapper.vm.$meta().refresh()
108-
// TODO: fix this test, not sure how to create a wrapper with a attri
109-
// bute data-server-rendered="true"
110-
expect(tags).not.toBe(false)
122+
expect(tags).toBe(false)
123+
124+
wrapper.destroy()
111125
})
112126

113127
test('changed function is called', async () => {
@@ -203,9 +217,14 @@ describe('client', () => {
203217
test('changes before hydration initialization trigger an update', async () => {
204218
html.setAttribute(defaultOptions.ssrAttribute, 'true')
205219

220+
const el = document.createElement('div')
221+
el.setAttribute('id', 'app')
222+
el.setAttribute('data-server-rendered', true)
223+
document.body.appendChild(el)
224+
206225
// this component uses a computed prop to simulate a non-synchronous
207226
// metaInfo update like you would have with a Vuex mutation
208-
const component = Vue.component('test-component', {
227+
const Component = Vue.extend({
209228
data() {
210229
return {
211230
hiddenTheme: 'light'
@@ -229,20 +248,28 @@ describe('client', () => {
229248
}
230249
})
231250

232-
const wrapper = mount(component, { localVue: Vue })
251+
const vm = new Component().$mount(el)
252+
const wrapper = createWrapper(vm, { attachToDocument: true })
233253
expect(html.getAttribute('theme')).not.toBe('dark')
234254

235255
await vmTick(wrapper.vm)
236256
jest.runAllTimers()
237257

238258
expect(html.getAttribute('theme')).toBe('dark')
239259
html.removeAttribute('theme')
260+
261+
wrapper.destroy()
240262
})
241263

242264
test('changes during hydration initialization trigger an update', async () => {
243265
html.setAttribute(defaultOptions.ssrAttribute, 'true')
244266

245-
const component = Vue.component('test-component', {
267+
const el = document.createElement('div')
268+
el.setAttribute('id', 'app')
269+
el.setAttribute('data-server-rendered', true)
270+
document.body.appendChild(el)
271+
272+
const Component = Vue.extend({
246273
data() {
247274
return {
248275
hiddenTheme: 'light'
@@ -266,13 +293,16 @@ describe('client', () => {
266293
}
267294
})
268295

269-
const wrapper = mount(component, { localVue: Vue })
296+
const vm = new Component().$mount(el)
297+
const wrapper = createWrapper(vm, { attachToDocument: true })
270298
expect(html.getAttribute('theme')).not.toBe('dark')
271299

272300
await vmTick(wrapper.vm)
273301
jest.runAllTimers()
274302

275303
expect(html.getAttribute('theme')).toBe('dark')
276304
html.removeAttribute('theme')
305+
306+
wrapper.destroy()
277307
})
278308
})

test/utils/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { mount, createLocalVue } from '@vue/test-utils'
1+
import { mount, shallowMount, createWrapper, createLocalVue } from '@vue/test-utils'
22
import { renderToString } from '@vue/server-test-utils'
33
import { defaultOptions } from '../../src/shared/constants'
44
import VueMetaBrowserPlugin from '../../src/browser'
55
import VueMetaServerPlugin from '../../src'
66

77
export {
88
mount,
9+
shallowMount,
10+
createWrapper,
911
renderToString,
1012
VueMetaBrowserPlugin,
1113
VueMetaServerPlugin

0 commit comments

Comments
 (0)