forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmocks.spec.js
175 lines (162 loc) · 4.95 KB
/
mocks.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { createLocalVue, config } from '~vue/test-utils'
import Component from '~resources/components/component.vue'
import ComponentWithVuex from '~resources/components/component-with-vuex.vue'
import { describeWithMountingMethods } from '~resources/utils'
import { itDoNotRunIf } from 'conditional-specs'
describeWithMountingMethods('options.mocks', (mountingMethod) => {
let configMocksSave
let consoleError
beforeEach(() => {
configMocksSave = config.mocks
config.mocks = {}
consoleError = sinon.stub(console, 'error')
})
afterEach(() => {
config.mocks = configMocksSave
consoleError.restore()
})
it('adds variables to vm when passed', () => {
const TestComponent = {
template: `
<div>
{{$store.store}}
{{$route.path}}
</div>
`
}
const $store = { store: true }
const $route = { path: 'http://test.com' }
const wrapper = mountingMethod(TestComponent, {
mocks: {
$store,
$route
}
})
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).contains('true')
expect(HTML).contains('http://test.com')
})
// render returns a string so reactive does not apply
itDoNotRunIf(mountingMethod.name === 'renderToString',
'adds variables as reactive properties to vm when passed', () => {
const stub = sinon.stub()
const $reactiveMock = { value: 'value' }
const wrapper = mountingMethod({
template: `
<div>
{{value}}
</div>
`,
computed: {
value () {
return this.$reactiveMock.value
}
},
watch: {
value () {
stub()
}
}
}, {
mocks: { $reactiveMock }
})
expect(wrapper.text()).to.contain('value')
$reactiveMock.value = 'changed value'
expect(wrapper.text()).to.contain('changed value')
})
itDoNotRunIf(mountingMethod.name === 'shallowMount',
'adds variables available to nested vms', () => {
const count = 1
const wrapper = mountingMethod({
template: '<div><component-with-vuex /></div>',
components: {
ComponentWithVuex
}
}, {
mocks: { $store: { state: { count, foo: {}}}}
})
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).contains(count)
})
itDoNotRunIf(mountingMethod.name === 'shallowMount',
'adds variables available to nested vms using localVue', () => {
const localVue = createLocalVue()
const count = 1
const wrapper = mountingMethod({
template: '<div><component-with-vuex /></div>',
components: {
ComponentWithVuex
}
}, {
mocks: { $store: { state: { count, foo: {}}}},
localVue
})
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).contains(count)
})
itDoNotRunIf(mountingMethod.name === 'renderToString',
'does not affect global vue class when passed as mocks object', () => {
const $store = { store: true }
const wrapper = mountingMethod(Component, {
mocks: {
$store
}
})
expect(wrapper.vm.$store).to.equal($store)
const freshWrapper = mountingMethod(Component)
expect(typeof freshWrapper.vm.$store).to.equal('undefined')
})
it('logs that a property cannot be overwritten if there are problems writing', () => {
const localVue = createLocalVue()
Object.defineProperty(localVue.prototype, '$store', {
value: 42
})
const $store = 64
mountingMethod(Component, {
localVue,
mocks: {
$store
}
})
const msg = '[vue-test-utils]: could not overwrite property $store, this usually caused by a plugin that has added the property as a read-only value'
expect(consoleError.calledWith(msg)).to.equal(true)
})
it('prioritize mounting options over config', () => {
config.mocks['$global'] = 'globallyMockedValue'
const TestComponent = {
template: `
<div>{{ $global }}</div>
`
}
const wrapper = mountingMethod(TestComponent, {
mocks: {
'$global': 'locallyMockedValue'
}
})
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).to.contain('locallyMockedValue')
})
it('logs that a property cannot be overwritten if there are problems writing', () => {
const localVue = createLocalVue()
Object.defineProperty(localVue.prototype, '$val', {
value: 42
})
const $val = 64
mountingMethod(Component, {
localVue,
mocks: {
$val
}
})
const msg = '[vue-test-utils]: could not overwrite property $val, this usually caused by a plugin that has added the property as a read-only value'
expect(consoleError.calledWith(msg)).to.equal(true)
})
})