forked from testing-library/dom-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggestions.js
175 lines (138 loc) 路 4.61 KB
/
suggestions.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
169
170
171
172
173
174
175
import {configure} from '../config'
import {screen} from '..'
import {renderIntoDocument} from './helpers/test-utils'
beforeEach(() => {
configure({showSuggestions: true})
})
it('should not suggest when using getByRole', () => {
renderIntoDocument(`<button data-testid="foo">submit</button>`)
expect(() => screen.getByRole('button', {name: /submit/})).not.toThrowError()
})
test('should not suggest when nothing available', () => {
renderIntoDocument(`<span data-testid="foo" />`)
expect(() => screen.queryByTestId('foo')).not.toThrowError()
})
test(`should not suggest if the suggestion would give different results`, () => {
renderIntoDocument(`
<input type="text" data-testid="foo" /><span data-testid="foo" />
`)
expect(() => screen.getAllByTestId('foo')).not.toThrowError()
})
test('should suggest getByRole when used with getBy', () => {
renderIntoDocument(`<button data-testid="foo">submit</button>`)
expect(() => screen.getByTestId('foo')).toThrowErrorMatchingInlineSnapshot(`
"A better query is available, try this:
*ByRole("button", {name:/submit/})
<body>
<button
data-testid="foo"
>
submit
</button>
</body>"
`)
})
test('should suggest *ByRole when used with getAllBy', () => {
renderIntoDocument(`
<button data-testid="foo">submit</button>
<button data-testid="foo">submit</button>`)
expect(() => screen.getAllByTestId('foo'))
.toThrowErrorMatchingInlineSnapshot(`
"A better query is available, try this:
*ByRole("button", {name:/submit/})
<body>
<button
data-testid="foo"
>
submit
</button>
<button
data-testid="foo"
>
submit
</button>
</body>"
`)
})
test('should suggest img role w/ alt text', () => {
renderIntoDocument(`<img data-testid="img" alt="Incredibles 2 Poster" />`)
expect(() => screen.getByAltText('Incredibles 2 Poster')).toThrowError(
/\*ByRole\("img", \{name:\/Incredibles 2 Poster\/\}\)/,
)
})
test('should suggest *ByLabelText when no role available', () => {
renderIntoDocument(
`<label for="foo">Username</label><input data-testid="foo" id="foo" />`,
)
expect(() => screen.getByTestId('foo')).toThrowError(
/\*ByLabelText\("Username"\)/,
)
})
test(`should suggest *ByLabel on non form elements`, () => {
renderIntoDocument(`
<div data-testid="foo" aria-labelledby="section-one-header">
<span id="section-one-header">Section One</span>
<p>some content</p>
</div>
`)
expect(() => screen.getByTestId('foo')).toThrowError(
/\*ByLabelText\("Section One"\)/,
)
})
test.each([
`<label id="username-label">Username</label><input aria-labelledby="username-label" type="text" />`,
`<label><span>Username</span><input type="text" /></label>`,
`<label for="foo">Username</label><input id="foo" type="text" />`,
])('should suggest *ByRole over label %s', html => {
renderIntoDocument(html)
expect(() => screen.getByLabelText('Username')).toThrowError(
/\*ByRole\("textbox", \{name:\/Username\/\}\)/,
)
})
test(`should suggest *ByPlaceholderText`, () => {
renderIntoDocument(`<input data-testid="foo" placeholder="Username" />`)
expect(() => screen.getByTestId('foo')).toThrowError(
/\*ByPlaceholderText\("Username"\)/,
)
})
test(`should suggest *ByText for simple elements`, () => {
renderIntoDocument(`<div data-testid="foo">hello there</div>`)
expect(() => screen.getByTestId('foo')).toThrowError(
/\*ByText\("hello there"\)/,
)
})
test(`should suggest *ByDisplayValue`, () => {
renderIntoDocument(`<input id="lastName" data-testid="lastName" />`)
document.getElementById('lastName').value = 'Prine' // RIP John Prine
expect(() => screen.getByTestId('lastName')).toThrowError(
/\*ByDisplayValue\("Prine"\)/,
)
})
test(`should suggest *ByAltText`, () => {
renderIntoDocument(`
<input data-testid="input" alt="last name" />
<map name="workmap">
<area data-testid="area" shape="rect" coords="34,44,270,350" alt="Computer">
</map>
`)
expect(() => screen.getByTestId('input')).toThrowError(
/\*ByAltText\("last name"\)/,
)
expect(() => screen.getByTestId('area')).toThrowError(
/\*ByAltText\("Computer"\)/,
)
})
test(`should suggest *ByTitle`, () => {
renderIntoDocument(`
<span title="Delete" data-testid="delete"></span>
<svg>
<title data-testid="svg">Close</title>
<g><path /></g>
</svg>`)
expect(() => screen.getByTestId('delete')).toThrowError(
/\*ByTitle\("Delete"\)/,
)
//Since `ByTitle` and `ByText` will both return the <title> element
//`getByText` will always be the suggested query as it is higher up the list.
expect(() => screen.getByTestId('svg')).toThrowError(/\*ByText\("Close"\)/)
})