forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.spec.js
116 lines (107 loc) · 3.29 KB
/
context.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import Vue from 'vue'
import { vueVersion } from '~resources/utils'
import { describeWithMountingMethods } from '~resources/utils'
describeWithMountingMethods('options.context', mountingMethod => {
it('mounts functional component when passed context object', () => {
if (vueVersion <= 2.2) {
console.log(
'WARN: no current way to test functional component in [email protected]'
)
return
}
const Component = {
functional: true,
render (h, { props }) {
return h('div')
},
name: 'common'
}
const context = {
data: { hello: true },
props: { show: true }
}
mountingMethod(Component, { context })
})
it('throws error if non functional component is passed with context option', () => {
const Component = {
render: h => h('div')
}
const context = {}
const message =
'[vue-test-utils]: mount.context can only be used when mounting a functional component'
const fn = () => mountingMethod(Component, { context })
expect(fn)
.to.throw()
.with.property('message', message)
})
it('does not throw error if functional component with Vue.extend', () => {
const Component = Vue.extend({
functional: true,
render: h => h('div')
})
const context = {}
const fn = () => mountingMethod(Component, { context, stubs: false, mocks: false })
expect(fn).not.to.throw()
})
it('throws error if context option is not an object', () => {
const Component = {
functional: true,
render: h => h('div')
}
const context = 'string'
const message = '[vue-test-utils]: mount.context must be an object'
const fn = () => mountingMethod(Component, { context })
expect(fn)
.to.throw()
.with.property('message', message)
})
it('mounts functional component with a defined context when no context object passed in options', () => {
const defaultValue = '[vue-test-utils]: testProp default value'
const Component = {
functional: true,
props: {
testProp: {
type: String,
default: defaultValue
}
},
render: (h, { props }) => h('div', props.testProp)
}
const wrapper = mountingMethod(Component)
const HTML =
mountingMethod.name === 'renderToString' ? wrapper : wrapper.html()
expect(HTML).to.contain(defaultValue)
})
it('mounts functional component with a defined context.children text', () => {
const Component = {
functional: true,
render: (h, { children }) => {
return h('div', children)
}
}
const wrapper = mountingMethod(Component, {
context: {
children: ['render text']
}
})
const HTML =
mountingMethod.name === 'renderToString' ? wrapper : wrapper.html()
expect(HTML).to.contain('render text')
})
it('mounts functional component with a defined context.children element', () => {
const Component = {
functional: true,
render: (h, { children }) => {
return h('div', children)
}
}
const wrapper = mountingMethod(Component, {
context: {
children: [h => h('div', 'render component')]
}
})
const HTML =
mountingMethod.name === 'renderToString' ? wrapper : wrapper.html()
expect(HTML).to.contain('render component')
})
})