Skip to content

Commit ca6b334

Browse files
authored
Merge branch 'main' into replacewith
2 parents 430e952 + a86c54c commit ca6b334

21 files changed

+291
-93
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,15 @@
16371637
"code",
16381638
"bug"
16391639
]
1640+
},
1641+
{
1642+
"login": "sieem",
1643+
"name": "sieem",
1644+
"avatar_url": "https://avatars.githubusercontent.com/u/36861085?v=4",
1645+
"profile": "https://github.com/sieem",
1646+
"contributions": [
1647+
"code"
1648+
]
16401649
}
16411650
],
16421651
"repoHost": "https://github.com",

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Thanks goes to these people ([emoji key][emojis]):
134134
<tbody>
135135
<tr>
136136
<td align="center" valign="top" width="14.28%"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3?s=100" width="100px;" alt="Kent C. Dodds"/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=kentcdodds" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=kentcdodds" title="Documentation">📖</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=kentcdodds" title="Tests">⚠️</a></td>
137-
<td align="center" valign="top" width="14.28%"><a href="https://www.smooth-code.com"><img src="https://avatars2.githubusercontent.com/u/266302?v=4?s=100" width="100px;" alt="Greg Bergé"/><br /><sub><b>Greg Bergé</b></sub></a><br /><a href="#ideas-neoziro" title="Ideas, Planning, & Feedback">🤔</a></td>
137+
<td align="center" valign="top" width="14.28%"><a href="https://argos-ci.com"><img src="https://avatars2.githubusercontent.com/u/266302?v=4?s=100" width="100px;" alt="Greg Bergé"/><br /><sub><b>Greg Bergé</b></sub></a><br /><a href="#ideas-neoziro" title="Ideas, Planning, & Feedback">🤔</a></td>
138138
<td align="center" valign="top" width="14.28%"><a href="http://audiolion.github.io"><img src="https://avatars1.githubusercontent.com/u/2430381?v=4?s=100" width="100px;" alt="Ryan Castner"/><br /><sub><b>Ryan Castner</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=audiolion" title="Documentation">📖</a></td>
139139
<td align="center" valign="top" width="14.28%"><a href="https://www.dnlsandiego.com"><img src="https://avatars0.githubusercontent.com/u/8008023?v=4?s=100" width="100px;" alt="Daniel Sandiego"/><br /><sub><b>Daniel Sandiego</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=dnlsandiego" title="Code">💻</a></td>
140140
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Miklet"><img src="https://avatars2.githubusercontent.com/u/12592677?v=4?s=100" width="100px;" alt="Paweł Mikołajczyk"/><br /><sub><b>Paweł Mikołajczyk</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Miklet" title="Code">💻</a></td>
@@ -348,6 +348,9 @@ Thanks goes to these people ([emoji key][emojis]):
348348
<td align="center" valign="top" width="14.28%"><a href="http://everlong.org/"><img src="https://avatars.githubusercontent.com/u/454175?v=4?s=100" width="100px;" alt="Julien Wajsberg"/><br /><sub><b>Julien Wajsberg</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=julienw" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/issues?q=author%3Ajulienw" title="Bug reports">🐛</a></td>
349349
<td align="center" valign="top" width="14.28%"><a href="https://github.com/KevinBon"><img src="https://avatars.githubusercontent.com/u/1910927?v=4?s=100" width="100px;" alt="Kevin BON"/><br /><sub><b>Kevin BON</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=KevinBon" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/issues?q=author%3AKevinBon" title="Bug reports">🐛</a></td>
350350
</tr>
351+
<tr>
352+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/sieem"><img src="https://avatars.githubusercontent.com/u/36861085?v=4?s=100" width="100px;" alt="sieem"/><br /><sub><b>sieem</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=sieem" title="Code">💻</a></td>
353+
</tr>
351354
</tbody>
352355
</table>
353356

src/DOMElementFilter.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,30 @@ const FRAGMENT_NODE = 11
158158

159159
const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/
160160

161+
const isCustomElement = (val: any) => {
162+
const {tagName} = val
163+
return Boolean(
164+
(typeof tagName === 'string' && tagName.includes('-')) ||
165+
(typeof val.hasAttribute === 'function' && val.hasAttribute('is')),
166+
)
167+
}
168+
161169
const testNode = (val: any) => {
162170
const constructorName = val.constructor.name
163-
const {nodeType, tagName} = val
164-
const isCustomElement =
165-
(typeof tagName === 'string' && tagName.includes('-')) ||
166-
(typeof val.hasAttribute === 'function' && val.hasAttribute('is'))
171+
172+
const {nodeType} = val
167173

168174
return (
169175
(nodeType === ELEMENT_NODE &&
170-
(ELEMENT_REGEXP.test(constructorName) || isCustomElement)) ||
176+
(ELEMENT_REGEXP.test(constructorName) || isCustomElement(val))) ||
171177
(nodeType === TEXT_NODE && constructorName === 'Text') ||
172178
(nodeType === COMMENT_NODE && constructorName === 'Comment') ||
173179
(nodeType === FRAGMENT_NODE && constructorName === 'DocumentFragment')
174180
)
175181
}
176182

177183
export const test: NewPlugin['test'] = (val: any) =>
178-
val?.constructor?.name && testNode(val)
184+
(val?.constructor?.name || isCustomElement(val)) && testNode(val)
179185

180186
type HandledType = Element | Text | Comment | DocumentFragment
181187

@@ -195,7 +201,8 @@ export default function createDOMElementFilter(
195201
filterNode: (node: Node) => boolean,
196202
): NewPlugin {
197203
return {
198-
test: (val: any) => val?.constructor?.name && testNode(val),
204+
test: (val: any) =>
205+
(val?.constructor?.name || isCustomElement(val)) && testNode(val),
199206
serialize: (
200207
node: HandledType,
201208
config: Config,

src/__node_tests__/pretty-dom.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import {JSDOM} from 'jsdom'
2+
import {prettyDOM} from '../pretty-dom'
3+
4+
function render(html) {
5+
const {window} = new JSDOM()
6+
const container = window.document.createElement('div')
7+
container.innerHTML = html
8+
return {container}
9+
}
10+
11+
jest.mock('../get-user-code-frame')
12+
13+
test('prettyDOM supports a COLORS environment variable', () => {
14+
const {container} = render('<div>Hello World!</div>')
15+
16+
// process.env.COLORS is a string, so make sure we test it as such
17+
process.env.COLORS = 'false'
18+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
19+
<div>
20+
<div>
21+
Hello World!
22+
</div>
23+
</div>
24+
`)
25+
26+
process.env.COLORS = 'true'
27+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
28+
<div>
29+
<div>
30+
Hello World!
31+
</div>
32+
</div>
33+
`)
34+
})
35+
36+
test('prettyDOM handles a COLORS env variable of unexpected object type by colorizing for node', () => {
37+
const {container} = render('<div>Hello World!</div>')
38+
39+
const originalNodeVersion = process.versions.node
40+
process.env.COLORS = '{}'
41+
delete process.versions.node
42+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
43+
<div>
44+
<div>
45+
Hello World!
46+
</div>
47+
</div>
48+
`)
49+
process.versions.node = '1.2.3'
50+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
51+
<div>
52+
<div>
53+
Hello World!
54+
</div>
55+
</div>
56+
`)
57+
process.versions.node = originalNodeVersion
58+
})
59+
60+
test('prettyDOM handles a COLORS env variable of undefined by colorizing for node', () => {
61+
const {container} = render('<div>Hello World!</div>')
62+
63+
const originalNodeVersion = process.versions.node
64+
process.env.COLORS = undefined
65+
delete process.versions.node
66+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
67+
<div>
68+
<div>
69+
Hello World!
70+
</div>
71+
</div>
72+
`)
73+
process.versions.node = '1.2.3'
74+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
75+
<div>
76+
<div>
77+
Hello World!
78+
</div>
79+
</div>
80+
`)
81+
process.versions.node = originalNodeVersion
82+
})
83+
84+
test('prettyDOM handles a COLORS env variable of empty string by colorizing for node', () => {
85+
const {container} = render('<div>Hello World!</div>')
86+
87+
const originalNodeVersion = process.versions.node
88+
process.env.COLORS = ''
89+
delete process.versions.node
90+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
91+
<div>
92+
<div>
93+
Hello World!
94+
</div>
95+
</div>
96+
`)
97+
process.versions.node = '1.2.3'
98+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
99+
<div>
100+
<div>
101+
Hello World!
102+
</div>
103+
</div>
104+
`)
105+
process.versions.node = originalNodeVersion
106+
})

src/__tests__/element-queries.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import jestSnapshotSerializerAnsi from 'jest-snapshot-serializer-ansi'
12
import {configure, getConfig} from '../config'
23
import {render, renderIntoDocument} from './helpers/test-utils'
34

5+
expect.addSnapshotSerializer(jestSnapshotSerializerAnsi)
6+
47
// set original config
58
let originalConfig
69
beforeAll(() => {

src/__tests__/events.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ const eventTypes = [
7777
elementType: 'img',
7878
},
7979
{
80-
type: '',
81-
events: ['offline', 'online'],
80+
type: 'Window',
81+
events: ['offline', 'online', 'pageHide', 'pageShow'],
8282
elementType: 'window',
8383
},
8484
{

src/__tests__/get-user-code-frame.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import fs from 'fs'
2+
import jestSnapshotSerializerAnsi from 'jest-snapshot-serializer-ansi'
23
import {getUserCodeFrame} from '../get-user-code-frame'
34

5+
expect.addSnapshotSerializer(jestSnapshotSerializerAnsi)
6+
47
jest.mock('fs', () => ({
58
// We setup the contents of a sample file
69
readFileSync: jest.fn(

src/__tests__/log-dom.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {getUserCodeFrame} from '../get-user-code-frame'
2+
import {logDOM} from '../pretty-dom'
3+
import {render} from './helpers/test-utils'
4+
5+
jest.mock('../get-user-code-frame')
6+
7+
beforeEach(() => {
8+
jest.spyOn(console, 'log').mockImplementation(() => {})
9+
})
10+
11+
afterEach(() => {
12+
console.log.mockRestore()
13+
})
14+
15+
test('logDOM logs highlighted prettyDOM to the console', () => {
16+
const {container} = render('<div>Hello World!</div>')
17+
logDOM(container)
18+
expect(console.log).toHaveBeenCalledTimes(1)
19+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
20+
<div>
21+
<div>
22+
Hello World!
23+
</div>
24+
</div>
25+
`)
26+
})
27+
28+
test('logDOM logs highlighted prettyDOM with code frame to the console', () => {
29+
getUserCodeFrame.mockImplementationOnce(
30+
() => `"/home/john/projects/sample-error/error-example.js:7:14
31+
5 | document.createTextNode('Hello World!')
32+
6 | )
33+
> 7 | screen.debug()
34+
| ^
35+
"
36+
`,
37+
)
38+
const {container} = render('<div>Hello World!</div>')
39+
logDOM(container)
40+
expect(console.log).toHaveBeenCalledTimes(1)
41+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
42+
<div>
43+
<div>
44+
Hello World!
45+
</div>
46+
</div>
47+
48+
"/home/john/projects/sample-error/error-example.js:7:14
49+
5 | document.createTextNode('Hello World!')
50+
6 | )
51+
> 7 | screen.debug()
52+
| ^
53+
"
54+
55+
`)
56+
})

0 commit comments

Comments
 (0)