-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathelement-queries.js
150 lines (135 loc) · 4.86 KB
/
element-queries.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
import React from 'react'
import {render} from '../'
import '../extend-expect'
test('query can return null', () => {
const {
queryByLabelText,
queryByPlaceholderText,
queryByText,
queryByTestId,
queryByAltText,
} = render(<div />)
expect(queryByTestId('LucyRicardo')).toBeNull()
expect(queryByLabelText('LucyRicardo')).toBeNull()
expect(queryByPlaceholderText('LucyRicardo')).toBeNull()
expect(queryByText('LucyRicardo')).toBeNull()
expect(queryByAltText('LucyRicardo')).toBeNull()
})
test('get throws a useful error message', () => {
const {
getByLabelText,
getByPlaceholderText,
getByText,
getByTestId,
getByAltText,
} = render(<div />)
expect(() => getByLabelText('LucyRicardo')).toThrowErrorMatchingSnapshot()
expect(() =>
getByPlaceholderText('LucyRicardo'),
).toThrowErrorMatchingSnapshot()
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingSnapshot()
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingSnapshot()
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingSnapshot()
})
test('get can get form controls by label text', () => {
const {getByLabelText} = render(
<div>
<label>
1st<input id="first-id" />
</label>
<div>
<label htmlFor="second-id">2nd</label>
<input id="second-id" />
</div>
<div>
<label id="third-label">3rd</label>
<input aria-labelledby="third-label" id="third-id" />
</div>
</div>,
)
expect(getByLabelText('1st').id).toBe('first-id')
expect(getByLabelText('2nd').id).toBe('second-id')
expect(getByLabelText('3rd').id).toBe('third-id')
})
test('get can get form controls by placeholder', () => {
const {getByPlaceholderText} = render(
<input id="username-id" placeholder="username" />,
)
expect(getByPlaceholderText('username').id).toBe('username-id')
})
test('label with no form control', () => {
const {getByLabelText, queryByLabelText} = render(<label>All alone</label>)
expect(queryByLabelText('alone')).toBeNull()
expect(() => getByLabelText('alone')).toThrowErrorMatchingSnapshot()
})
test('totally empty label', () => {
const {getByLabelText, queryByLabelText} = render(<label />)
expect(queryByLabelText('')).toBeNull()
expect(() => getByLabelText('')).toThrowErrorMatchingSnapshot()
})
test('getByLabelText with aria-label', () => {
// not recommended normally, but supported for completeness
const {queryByLabelText} = render(<input aria-label="batman" />)
expect(queryByLabelText('bat')).toBeInTheDOM()
})
test('get element by its alt text', () => {
const {getByAltText} = render(
<div>
<input data-info="no alt here" />
<img alt="finding nemo poster" src="/finding-nemo.png" />
</div>,
)
expect(getByAltText(/fin.*nem.*poster$/i).src).toBe('/finding-nemo.png')
})
test('using jest helpers to assert element states', () => {
const {queryByTestId} = render(<span data-testid="count-value">2</span>)
// other ways to assert your test cases, but you don't need all of them.
expect(queryByTestId('count-value')).toBeInTheDOM()
expect(queryByTestId('count-value1')).not.toBeInTheDOM()
expect(queryByTestId('count-value')).toHaveTextContent('2')
expect(queryByTestId('count-value')).not.toHaveTextContent('21')
expect(() =>
expect(queryByTestId('count-value2')).toHaveTextContent('2'),
).toThrowError()
// negative test cases wrapped in throwError assertions for coverage.
expect(() =>
expect(queryByTestId('count-value')).not.toBeInTheDOM(),
).toThrowError()
expect(() =>
expect(queryByTestId('count-value1')).toBeInTheDOM(),
).toThrowError()
expect(() =>
expect(queryByTestId('count-value')).toHaveTextContent('3'),
).toThrowError()
expect(() =>
expect(queryByTestId('count-value')).not.toHaveTextContent('2'),
).toThrowError()
})
test('using jest helpers to check element attributes', () => {
const {queryByTestId} = render(
<button data-testid="ok-button" type="submit" disabled>
OK
</button>,
)
expect(queryByTestId('ok-button')).toHaveAttribute('disabled')
expect(queryByTestId('ok-button')).toHaveAttribute('type')
expect(queryByTestId('ok-button')).not.toHaveAttribute('class')
expect(queryByTestId('ok-button')).toHaveAttribute('type', 'submit')
expect(queryByTestId('ok-button')).not.toHaveAttribute('type', 'button')
expect(() =>
expect(queryByTestId('ok-button')).not.toHaveAttribute('disabled'),
).toThrowError()
expect(() =>
expect(queryByTestId('ok-button')).not.toHaveAttribute('type'),
).toThrowError()
expect(() =>
expect(queryByTestId('ok-button')).toHaveAttribute('class'),
).toThrowError()
expect(() =>
expect(queryByTestId('ok-button')).not.toHaveAttribute('type', 'submit'),
).toThrowError()
expect(() =>
expect(queryByTestId('ok-button')).toHaveAttribute('type', 'button'),
).toThrowError()
})
/* eslint jsx-a11y/label-has-for:0 */