forked from testing-library/dom-testing-library
/
pretty-dom.js
168 lines (148 loc) 路 4.42 KB
/
pretty-dom.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {prettyDOM, logDOM} from '../pretty-dom'
import {getUserCodeFrame} from '../get-user-code-frame'
import {render, renderIntoDocument} from './helpers/test-utils'
jest.mock('../get-user-code-frame')
beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
console.log.mockRestore()
})
test('prettyDOM prints out the given DOM element tree', () => {
const {container} = render('<div>Hello World!</div>')
expect(prettyDOM(container)).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
`)
})
test('prettyDOM supports truncating the output length', () => {
const {container} = render('<div>Hello World!</div>')
expect(prettyDOM(container, 5)).toMatch(/\.\.\./)
expect(prettyDOM(container, 0)).toMatch('')
expect(prettyDOM(container, Number.POSITIVE_INFINITY)).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
`)
})
test('prettyDOM defaults to document.body', () => {
const defaultInlineSnapshot = `
<body>
<div>
Hello World!
</div>
</body>
`
renderIntoDocument('<div>Hello World!</div>')
expect(prettyDOM()).toMatchInlineSnapshot(defaultInlineSnapshot)
expect(prettyDOM(null)).toMatchInlineSnapshot(defaultInlineSnapshot)
})
test('prettyDOM supports receiving the document element', () => {
expect(prettyDOM(document)).toMatchInlineSnapshot(`
<html>
<head />
<body />
</html>
`)
})
test('logDOM logs prettyDOM to the console', () => {
const {container} = render('<div>Hello World!</div>')
logDOM(container)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
`)
})
test('logDOM logs prettyDOM with code frame to the console', () => {
getUserCodeFrame.mockImplementationOnce(
() => `"/home/john/projects/sample-error/error-example.js:7:14
5 | document.createTextNode('Hello World!')
6 | )
> 7 | screen.debug()
| ^
"
`,
)
const {container} = render('<div>Hello World!</div>')
logDOM(container)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<div>
<div>
Hello World!
</div>
</div>
"/home/john/projects/sample-error/error-example.js:7:14
5 | document.createTextNode('Hello World!')
6 | )
> 7 | screen.debug()
| ^
"
`)
})
describe('prettyDOM fails with first parameter without outerHTML field', () => {
test('with array', () => {
expect(() => prettyDOM(['outerHTML'])).toThrowErrorMatchingInlineSnapshot(
`Expected an element or document but got Array`,
)
})
test('with number', () => {
expect(() => prettyDOM(1)).toThrowErrorMatchingInlineSnapshot(
`Expected an element or document but got number`,
)
})
test('with object', () => {
expect(() => prettyDOM({})).toThrowErrorMatchingInlineSnapshot(
`Expected an element or document but got Object`,
)
})
})
test('prettyDOM ignores script elements and comments nodes by default', () => {
const {container} = renderIntoDocument(
'<body><script src="context.js"></script><!-- Some comment --><p>Hello, Dave</p></body>',
)
expect(prettyDOM(container)).toMatchInlineSnapshot(`
<body>
<p>
Hello, Dave
</p>
</body>
`)
})
test('prettyDOM can include all elements with a custom filter', () => {
const {container} = renderIntoDocument(
'<body><script src="context.js"></script><!-- Some comment --><p>Hello, Dave</p></body>',
)
expect(
prettyDOM(container, Number.POSITIVE_INFINITY, {filterNode: () => true}),
).toMatchInlineSnapshot(`
<body>
<script
src="context.js"
/>
<!-- Some comment -->
<p>
Hello, Dave
</p>
</body>
`)
})
test('prettyDOM supports a COLORS environment variable', () => {
const {container} = render('<div>Hello World!</div>')
const noColors = prettyDOM(container, undefined, {highlight: false})
const withColors = prettyDOM(container, undefined, {highlight: true})
// process.env.COLORS is a string, so make sure we test it as such
process.env.COLORS = 'false'
expect(prettyDOM(container)).toEqual(noColors)
process.env.COLORS = 'true'
expect(prettyDOM(container)).toEqual(withColors)
})