-
Notifications
You must be signed in to change notification settings - Fork 379
/
to-contain-html.js
112 lines (105 loc) · 4.09 KB
/
to-contain-html.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
import {render} from './helpers/test-utils'
/* eslint-disable max-statements */
describe('.toContainHTML', () => {
test('handles positive and negative cases', () => {
const {queryByTestId} = render(`
<span data-testid="grandparent">
<span data-testid="parent">
<span data-testid="child"></span>
</span>
<svg data-testid="svg-element"></svg>
</span>
`)
const grandparent = queryByTestId('grandparent')
const parent = queryByTestId('parent')
const child = queryByTestId('child')
const nonExistantElement = queryByTestId('not-exists')
const fakeElement = {thisIsNot: 'an html element'}
const stringChildElement = '<span data-testid="child"></span>'
const stringChildElementSelfClosing = '<span data-testid="child" />'
const incorrectStringHtml = '<span data-testid="child"></div>'
const nonExistantString = '<span> Does not exists </span>'
const svgElement = queryByTestId('svg-element')
expect(grandparent).toContainHTML(stringChildElement)
expect(parent).toContainHTML(stringChildElement)
expect(child).toContainHTML(stringChildElement)
expect(child).toContainHTML(stringChildElementSelfClosing)
expect(grandparent).not.toContainHTML(nonExistantString)
expect(parent).not.toContainHTML(nonExistantString)
expect(child).not.toContainHTML(nonExistantString)
expect(child).not.toContainHTML(nonExistantString)
expect(grandparent).toContainHTML(incorrectStringHtml)
expect(parent).toContainHTML(incorrectStringHtml)
expect(child).toContainHTML(incorrectStringHtml)
// negative test cases wrapped in throwError assertions for coverage.
expect(() =>
expect(nonExistantElement).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(nonExistantElement).not.toContainHTML(nonExistantElement),
).toThrowError()
expect(() =>
expect(stringChildElement).not.toContainHTML(fakeElement),
).toThrowError()
expect(() =>
expect(svgElement).toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(grandparent).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(parent).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElement),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(stringChildElementSelfClosing),
).toThrowError()
expect(() => expect(child).toContainHTML(nonExistantString)).toThrowError()
expect(() => expect(parent).toContainHTML(nonExistantString)).toThrowError()
expect(() =>
expect(grandparent).toContainHTML(nonExistantString),
).toThrowError()
expect(() => expect(child).toContainHTML(nonExistantElement)).toThrowError()
expect(() =>
expect(parent).toContainHTML(nonExistantElement),
).toThrowError()
expect(() =>
expect(grandparent).toContainHTML(nonExistantElement),
).toThrowError()
expect(() =>
expect(nonExistantElement).not.toContainHTML(incorrectStringHtml),
).toThrowError()
expect(() =>
expect(grandparent).not.toContainHTML(incorrectStringHtml),
).toThrowError()
expect(() =>
expect(child).not.toContainHTML(incorrectStringHtml),
).toThrowError()
expect(() =>
expect(parent).not.toContainHTML(incorrectStringHtml),
).toThrowError()
})
test('throws with an expected text', () => {
const {queryByTestId} = render('<span data-testid="child"></span>')
const htmlElement = queryByTestId('child')
const nonExistantString = '<div> non-existant element </div>'
let errorMessage
try {
expect(htmlElement).toContainHTML(nonExistantString)
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).toContainHTML()</>
Expected:
<green><div> non-existant element </div></>
Received:
<red><span data-testid=\\"child\\" /></>"
`)
})
})