Skip to content

Commit b23a2bc

Browse files
smeijerkentcdodds
andauthored
feat: use regex based TextMatch for suggestions (#617)
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
1 parent 281eb8b commit b23a2bc

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/__tests__/suggestions.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ test(`should not suggest if the suggestion would give different results`, () =>
7272
test('should suggest by label over title', () => {
7373
renderIntoDocument(`<label><span>bar</span><input title="foo" /></label>`)
7474

75-
expect(() => screen.getByTitle('foo')).toThrowError(/getByLabelText\('bar'\)/)
75+
expect(() => screen.getByTitle('foo')).toThrowError(
76+
/getByLabelText\(\/bar\/i\)/,
77+
)
7678
})
7779

7880
test('should not suggest if there would be mixed suggestions', () => {
@@ -178,7 +180,7 @@ test('should suggest getByLabelText when no role available', () => {
178180
`<label for="foo">Username</label><input data-testid="foo" id="foo" />`,
179181
)
180182
expect(() => screen.getByTestId('foo')).toThrowError(
181-
/getByLabelText\('Username'\)/,
183+
/getByLabelText\(\/username\/i\)/,
182184
)
183185
})
184186

@@ -191,7 +193,7 @@ test(`should suggest getByLabel on non form elements`, () => {
191193
`)
192194

193195
expect(() => screen.getByTestId('foo')).toThrowError(
194-
/getByLabelText\('Section One'\)/,
196+
/getByLabelText\(\/section one\/i\)/,
195197
)
196198
})
197199

@@ -230,23 +232,23 @@ test(`should suggest label over placeholder text`, () => {
230232
)
231233

232234
expect(() => screen.getByPlaceholderText('Username')).toThrowError(
233-
/getByLabelText\('Username'\)/,
235+
/getByLabelText\(\/username\/i\)/,
234236
)
235237
})
236238

237239
test(`should suggest getByPlaceholderText`, () => {
238240
renderIntoDocument(`<input data-testid="foo" placeholder="Username" />`)
239241

240242
expect(() => screen.getByTestId('foo')).toThrowError(
241-
/getByPlaceholderText\('Username'\)/,
243+
/getByPlaceholderText\(\/username\/i\)/,
242244
)
243245
})
244246

245247
test(`should suggest getByText for simple elements`, () => {
246248
renderIntoDocument(`<div data-testid="foo">hello there</div>`)
247249

248250
expect(() => screen.getByTestId('foo')).toThrowError(
249-
/getByText\('hello there'\)/,
251+
/getByText\(\/hello there\/i\)/,
250252
)
251253
})
252254

@@ -256,7 +258,7 @@ test(`should suggest getByDisplayValue`, () => {
256258
document.getElementById('lastName').value = 'Prine' // RIP John Prine
257259

258260
expect(() => screen.getByTestId('lastName')).toThrowError(
259-
/getByDisplayValue\('Prine'\)/,
261+
/getByDisplayValue\(\/prine\/i\)/,
260262
)
261263
})
262264

@@ -269,10 +271,10 @@ test(`should suggest getByAltText`, () => {
269271
`)
270272

271273
expect(() => screen.getByTestId('input')).toThrowError(
272-
/getByAltText\('last name'\)/,
274+
/getByAltText\(\/last name\/i\)/,
273275
)
274276
expect(() => screen.getByTestId('area')).toThrowError(
275-
/getByAltText\('Computer'\)/,
277+
/getByAltText\(\/computer\/i\)/,
276278
)
277279
})
278280

@@ -285,27 +287,29 @@ test(`should suggest getByTitle`, () => {
285287
</svg>`)
286288

287289
expect(() => screen.getByTestId('delete')).toThrowError(
288-
/getByTitle\('Delete'\)/,
290+
/getByTitle\(\/delete\/i\)/,
289291
)
290292
expect(() => screen.getAllByTestId('delete')).toThrowError(
291-
/getAllByTitle\('Delete'\)/,
293+
/getAllByTitle\(\/delete\/i\)/,
292294
)
293295
expect(() => screen.queryByTestId('delete')).toThrowError(
294-
/queryByTitle\('Delete'\)/,
296+
/queryByTitle\(\/delete\/i\)/,
295297
)
296298
expect(() => screen.queryAllByTestId('delete')).toThrowError(
297-
/queryAllByTitle\('Delete'\)/,
299+
/queryAllByTitle\(\/delete\/i\)/,
298300
)
299301
expect(() => screen.queryAllByTestId('delete')).toThrowError(
300-
/queryAllByTitle\('Delete'\)/,
302+
/queryAllByTitle\(\/delete\/i\)/,
301303
)
302304
expect(() => screen.queryAllByTestId('delete')).toThrowError(
303-
/queryAllByTitle\('Delete'\)/,
305+
/queryAllByTitle\(\/delete\/i\)/,
304306
)
305307

306308
// Since `ByTitle` and `ByText` will both return the <title> element
307309
// `getByText` will always be the suggested query as it is higher up the list.
308-
expect(() => screen.getByTestId('svg')).toThrowError(/getByText\('Close'\)/)
310+
expect(() => screen.getByTestId('svg')).toThrowError(
311+
/getByText\(\/close\/i\)/,
312+
)
309313
})
310314

311315
test('getSuggestedQuery handles `variant` and defaults to `get`', () => {
@@ -343,9 +347,9 @@ test('getSuggestedQuery returns rich data for tooling', () => {
343347
expect(getSuggestedQuery(div)).toMatchObject({
344348
queryName: 'Text',
345349
queryMethod: 'getByText',
346-
queryArgs: ['cancel'],
350+
queryArgs: [/cancel/i],
347351
variant: 'get',
348352
})
349353

350-
expect(getSuggestedQuery(div).toString()).toEqual(`getByText('cancel')`)
354+
expect(getSuggestedQuery(div).toString()).toEqual(`getByText(/cancel/i)`)
351355
})

src/suggestions.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ function escapeRegExp(string) {
3030
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string
3131
}
3232

33+
function getRegExpMatcher(string) {
34+
return new RegExp(string.toLowerCase(), 'i')
35+
}
36+
3337
function makeSuggestion(queryName, content, {variant = 'get', name}) {
34-
const queryArgs = [content]
38+
const queryArgs = [queryName === 'Role' ? content : getRegExpMatcher(content)]
3539

3640
if (name) {
3741
queryArgs.push({name: new RegExp(escapeRegExp(name.toLowerCase()), 'i')})
@@ -45,12 +49,17 @@ function makeSuggestion(queryName, content, {variant = 'get', name}) {
4549
queryArgs,
4650
variant,
4751
toString() {
48-
const options = queryArgs[1]
49-
? `, { ${Object.entries(queryArgs[1])
52+
let [text, options] = queryArgs
53+
54+
text = typeof text === 'string' ? `'${text}'` : text
55+
56+
options = options
57+
? `, { ${Object.entries(options)
5058
.map(([k, v]) => `${k}: ${v}`)
5159
.join(', ')} }`
5260
: ''
53-
return `${queryMethod}('${content}'${options})`
61+
62+
return `${queryMethod}(${text}${options})`
5463
},
5564
}
5665
}

0 commit comments

Comments
 (0)