forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitted.spec.js
96 lines (80 loc) · 2.9 KB
/
emitted.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
import { createLocalVue } from '~vue/test-utils'
import { describeWithShallowAndMount } from '~resources/utils'
describeWithShallowAndMount('emitted', (mountingMethod) => {
it('captures emitted events with a different api', () => {
const wrapper = mountingMethod({
render: h => h('div')
})
wrapper.vm.$emit('foo')
expect(wrapper.emitted('foo')).to.exist
expect(wrapper.emitted('foo').length).to.equal(1)
expect(wrapper.emitted('foo')[0]).to.eql([])
expect(wrapper.emitted('bar')).not.to.exist
wrapper.vm.$emit('bar', 1, 2, 3)
expect(wrapper.emitted('bar')).to.exist
expect(wrapper.emitted('bar').length).to.equal(1)
expect(wrapper.emitted('bar')[0]).to.eql([1, 2, 3])
wrapper.vm.$emit('foo', 2, 3, 4)
expect(wrapper.emitted('foo')).to.exist
expect(wrapper.emitted('foo').length).to.equal(2)
expect(wrapper.emitted('foo')[1]).to.eql([2, 3, 4])
})
it('captures emitted events', () => {
const wrapper = mountingMethod({
render: h => h('div')
})
wrapper.vm.$emit('foo')
expect(wrapper.emitted().foo).to.exist
expect(wrapper.emitted().foo.length).to.equal(1)
expect(wrapper.emitted().foo[0]).to.eql([])
expect(wrapper.emitted().bar).not.to.exist
wrapper.vm.$emit('bar', 1, 2, 3)
expect(wrapper.emitted().bar).to.exist
expect(wrapper.emitted().bar.length).to.equal(1)
expect(wrapper.emitted().bar[0]).to.eql([1, 2, 3])
wrapper.vm.$emit('foo', 2, 3, 4)
expect(wrapper.emitted().foo).to.exist
expect(wrapper.emitted().foo.length).to.equal(2)
expect(wrapper.emitted().foo[1]).to.eql([2, 3, 4])
})
it('throws error when called on non VueWrapper', () => {
const wrapper = mountingMethod({
template: '<div><p /></div>'
})
const message = '[vue-test-utils]: wrapper.emitted() can only be called on a Vue instance'
const fn = () => wrapper.find('p').emitted()
expect(fn).to.throw().with.property('message', message)
})
it('captures all events thrown after beforeCreate lifecycle hook', () => {
const wrapper = mountingMethod({
beforeCreate () {
this.$emit('foo')
},
mounted () {
this.$emit('bar', 1, 2)
},
render: () => {}
})
expect(wrapper.emitted().foo).to.eql([[]])
expect(wrapper.emitted().bar).to.eql([[1, 2]])
})
it('captures only events from its component without side effects on localVue', () => {
const localVue = createLocalVue()
const wrapper1 = mountingMethod({
render: () => {},
beforeCreate () {
this.$emit('foo')
}
}, { localVue })
const wrapper2 = mountingMethod({
render: () => {},
mounted () {
this.$emit('bar')
}
}, { localVue })
expect(wrapper1.emitted().foo).to.eql([[]])
expect(wrapper1.emitted().bar).to.eql(undefined)
expect(wrapper2.emitted().foo).to.eql(undefined)
expect(wrapper2.emitted().bar).to.eql([[]])
})
})