forked from vuejs/test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetComponent.spec.ts
89 lines (78 loc) · 2.61 KB
/
getComponent.spec.ts
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
import { describe, expect, it, vi } from 'vitest'
import { defineComponent } from 'vue'
import { mount, RouterLinkStub, shallowMount } from '../src'
import Issue425 from './components/Issue425.vue'
const compA = defineComponent({
template: `<div class="A"></div>`
})
const compB = defineComponent({
name: 'CompB',
template: `<div class="A"></div>`
})
describe('getComponent', () => {
it('should delegate to findComponent', () => {
const wrapper = mount(compA)
vi.spyOn(wrapper, 'findComponent').mockReturnThis()
wrapper.getComponent('.domElement')
expect(wrapper.findComponent).toHaveBeenCalledWith('.domElement')
})
it('should throw if not found with a string selector', () => {
const wrapper = mount(compA)
expect(() => wrapper.getComponent('.domElement')).toThrowError(
'Unable to get component with selector .domElement within: <div class="A"></div>'
)
})
it('should throw if not found with a name selector', () => {
const wrapper = mount(compA)
expect(() => wrapper.getComponent({ name: 'CompB' })).toThrowError(
'Unable to get component with name CompB within: <div class="A"></div>'
)
})
it('should throw if not found with a component selector that has a name', () => {
const wrapper = mount(compA)
expect(() => wrapper.getComponent(compB)).toThrowError(
'Unable to get component with name CompB within: <div class="A"></div>'
)
})
it('should throw if not found with a ref selector', () => {
const wrapper = mount(compA)
expect(() => wrapper.getComponent({ ref: 'CompB' })).toThrowError(
'Unable to get component with ref CompB within: <div class="A"></div>'
)
})
it('should throw if not found with a component selector that has no name', () => {
const wrapper = mount(compB)
expect(() => wrapper.getComponent(compA)).toThrowError(
'Unable to get specified component within: <div class="A"></div>'
)
})
const name = 'some-route'
const options = {
props: {
name
},
global: {
stubs: {
RouterLink: RouterLinkStub
}
}
}
// https://github.com/vuejs/test-utils/issues/425
it('works with router-link and mount', () => {
const wrapper = mount(Issue425, options)
expect(
wrapper.getComponent<typeof RouterLinkStub>('.link').props('to')
).toEqual({
name
})
})
// https://github.com/vuejs/test-utils/issues/425
it('works with router-link and shallowMount', () => {
const wrapper = shallowMount(Issue425, options)
expect(
wrapper.getComponent<typeof RouterLinkStub>('.link').props('to')
).toEqual({
name
})
})
})