forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.spec.js
113 lines (105 loc) · 2.91 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
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()
})
})
})