forked from testing-library/vue-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
119 lines (93 loc) · 2.55 KB
/
render.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
import {render, cleanup} from '..'
import {h, defineComponent} from 'vue'
import '@testing-library/jest-dom'
test('baseElement defaults to document.body', () => {
const {baseElement} = render({template: '<div />'})
expect(baseElement).toBe(document.body)
})
test('renders custom baseElement', () => {
const Component = {template: '<span />'}
const {baseElement, container} = render(Component, {
baseElement: document.createElement('blink'),
})
expect(baseElement).toMatchInlineSnapshot(`
<blink>
<div>
<span />
</div>
</blink>
`)
expect(container).toMatchInlineSnapshot(`
<div>
<span />
</div>
`)
})
test('renders container', () => {
const {container, getByTestId} = render({
template: '<div data-testid="myDiv">my content</div>',
})
expect(container.firstChild).toHaveTextContent(
getByTestId('myDiv').textContent,
)
})
test('container defaults to div', () => {
const {container} = render({template: '<div />'})
expect(container.tagName).toBe('DIV')
})
test('renders custom container', () => {
const blink = document.createElement('blink')
const Component = {template: '<div />'}
const {container} = render(Component, {
container: document.body.appendChild(blink),
})
expect(container).toBe(blink)
})
test('baseElement matches container if not custom baseElement is provided', () => {
const blink = document.createElement('blink')
const Component = {template: '<div />'}
const {container, baseElement} = render(Component, {
container: document.body.appendChild(blink),
})
expect(container).toMatchInlineSnapshot(`
<blink>
<div />
</blink>
`)
expect(baseElement).toMatchInlineSnapshot(`
<blink>
<div />
</blink>
`)
})
test('unmounts', () => {
const {getByTestId, unmount, queryByTestId} = render({
template: `<div data-testid="node">Hi</div>`,
})
expect(getByTestId('node')).toBeInTheDocument()
unmount()
expect(queryByTestId('node')).not.toBeInTheDocument()
})
test('use unmount before cleanup', () => {
const TestComponent = defineComponent((_, {slots}) => {
return () => slots.default?.()
})
const {getByTestId, unmount, queryByTestId} = render({
render() {
return h(
TestComponent,
{},
{
default: () =>
h('div', {
'data-testid': 'node',
}),
},
)
},
})
expect(getByTestId('node')).toBeInTheDocument()
unmount()
expect(queryByTestId('node')).not.toBeInTheDocument()
expect(() => cleanup()).not.toThrow()
})