forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.spec.js
148 lines (139 loc) · 3.89 KB
/
sync.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
import sinon from 'sinon'
import { describeWithShallowAndMount } from '~resources/utils'
describeWithShallowAndMount('options.sync', (mountingMethod) => {
it('sets watchers to sync if set to true', () => {
const TestComponent = {
template: '<div>{{someData}}</div>',
data: () => ({
someData: 'hello'
})
}
const wrapper = mountingMethod(TestComponent, {
sync: true
})
expect(wrapper.text()).to.equal('hello')
wrapper.vm.someData = 'world'
expect(wrapper.text()).to.equal('world')
expect(wrapper.options.sync).to.equal(true)
})
it('sets watchers to sync if undefined', () => {
const TestComponent = {
template: '<div>{{someData}}</div>',
data: () => ({
someData: 'hello'
})
}
const wrapper = mountingMethod(TestComponent)
expect(wrapper.text()).to.equal('hello')
wrapper.vm.someData = 'world'
expect(wrapper.text()).to.equal('world')
expect(wrapper.options.sync).to.equal(true)
})
it('handles methods that update watchers', () => {
const TestComponent = {
template: `
<div id="app">
<div v-if="open">
<div>
<pre>data.text: <em>{{ text }}</em></pre>
</div>
<!-- Tests fail in 1.0.0-beta.13 with .fails portion of the code -->
<div class="fails">
<pre>computed.text: <em>{{ computedText }}</em></pre>
</div>
</div>
</div>
`,
data () {
return {
open: false,
text: '',
basket: []
}
},
computed: {
computedText () {
return this.text
}
},
created () {
window.addEventListener('click', this.clickHandler)
},
destroyed () {
window.removeEventListener('click', this.clickHandler)
},
watch: {
text () {
this.basket.push(this.computedText)
}
},
methods: {
clickHandler () {
this.open = !this.open
}
}
}
const wrapper = mountingMethod(TestComponent, {
attachToDocument: true
})
wrapper.trigger('click')
expect(wrapper.vm.text).to.equal('')
expect(wrapper.vm.basket.length).to.equal(0)
wrapper.setData({ text: 'foo' })
expect(wrapper.vm.text).to.equal('foo')
expect(wrapper.vm.computedText).to.equal('foo')
expect(wrapper.vm.basket[0]).to.equal('foo')
})
it('does not set watchers to sync if set to false', (done) => {
const TestComponent = {
template: '<div>{{someData}}</div>',
data: () => ({
someData: 'hello'
})
}
const wrapper = mountingMethod(TestComponent, {
sync: false
})
expect(wrapper.text()).to.equal('hello')
wrapper.vm.someData = 'world'
expect(wrapper.text()).to.equal('hello')
expect(wrapper.options.sync).to.equal(false)
setTimeout(() => {
expect(wrapper.text()).to.equal('world')
done()
})
})
it('call updated when sync is not false', () => {
const childComponentSpy = sinon.stub()
const ChildComponent = {
template: '<div>{{ foo }}</div>',
props: ['foo'],
updated () {
childComponentSpy()
}
}
const spy = sinon.stub()
const TestComponent = {
template: '<div>{{ foo }}<child-component :foo="foo" /></div>',
data () {
return {
foo: 'foo'
}
},
updated () {
spy()
}
}
const wrapper = mountingMethod(TestComponent, {
stubs: { 'child-component': ChildComponent },
sync: true
})
expect(spy.notCalled).to.equal(true)
expect(childComponentSpy.notCalled).to.equal(true)
expect(wrapper.html()).to.equal('<div>foo<div>foo</div></div>')
wrapper.vm.foo = 'bar'
expect(spy.calledOnce).to.equal(true)
expect(childComponentSpy.calledOnce).to.equal(true)
expect(wrapper.html()).to.equal('<div>bar<div>bar</div></div>')
})
})