Skip to content

Commit 9b688f8

Browse files
authored
feat: allow TextMatch to be any non-nullable type (#829)
1 parent 54fe9ba commit 9b688f8

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

src/__tests__/element-queries.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ test('matches case with RegExp matcher', () => {
144144
expect(queryByText(/Step 1 of 4/)).not.toBeTruthy()
145145
})
146146

147+
test('queryByText matches case with non-string matcher', () => {
148+
const {queryByText} = render(`<span>1</span>`)
149+
expect(queryByText(1)).toBeTruthy()
150+
})
151+
147152
test('can get form controls by label text', () => {
148153
const {getByLabelText} = render(`
149154
<div>
@@ -338,6 +343,11 @@ test('get can get form controls by placeholder', () => {
338343
expect(getByPlaceholderText('username').id).toBe('username-id')
339344
})
340345

346+
test('queryByPlaceholderText matches case with non-string matcher', () => {
347+
const {queryByPlaceholderText} = render(`<input placeholder="1" />`)
348+
expect(queryByPlaceholderText(1)).toBeTruthy()
349+
})
350+
341351
test('label with no form control', () => {
342352
const {getByLabelText, queryByLabelText} = render(`<label>All alone</label>`)
343353
expect(queryByLabelText(/alone/)).toBeNull()
@@ -535,6 +545,11 @@ test('getByLabelText with aria-label', () => {
535545
expect(queryByLabelText(/bat/)).toBeTruthy()
536546
})
537547

548+
test('queryByLabelText matches case with non-string matcher', () => {
549+
const {queryByLabelText} = render(`<input aria-label="1" />`)
550+
expect(queryByLabelText(1)).toBeTruthy()
551+
})
552+
538553
test('get element by its alt text', () => {
539554
const {getByAltText} = render(`
540555
<div>
@@ -545,6 +560,11 @@ test('get element by its alt text', () => {
545560
expect(getByAltText(/fin.*nem.*poster$/i).src).toContain('/finding-nemo.png')
546561
})
547562

563+
test('queryByAltText matches case with non-string matcher', () => {
564+
const {queryByAltText} = render(`<img alt="1" src="/finding-nemo.png" />`)
565+
expect(queryByAltText(1)).toBeTruthy()
566+
})
567+
548568
test('query/get element by its title', () => {
549569
const {getByTitle, queryByTitle} = render(`
550570
<div>
@@ -577,6 +597,11 @@ test('query/get title element of SVG', () => {
577597
expect(queryByTitle('Close').id).toEqual('svg-title')
578598
})
579599

600+
test('queryByTitle matches case with non-string matcher', () => {
601+
const {queryByTitle} = render(`<span title="1" />`)
602+
expect(queryByTitle(1)).toBeTruthy()
603+
})
604+
580605
test('query/get element by its value', () => {
581606
const {getByDisplayValue, queryByDisplayValue} = render(`
582607
<div>
@@ -632,6 +657,15 @@ test('query/get select by text with multiple options selected', () => {
632657
expect(queryByDisplayValue('Alaska').id).toEqual('state-select')
633658
})
634659

660+
test('queryByDisplayValue matches case with non-string matcher', () => {
661+
const {queryByDisplayValue} = render(`
662+
<select multiple id="state-select">
663+
<option selected value="one">1</option>
664+
</select>
665+
`)
666+
expect(queryByDisplayValue(1)).toBeTruthy()
667+
})
668+
635669
describe('query by test id', () => {
636670
afterEach(() => {
637671
// Restore the default test id attribute
@@ -651,6 +685,11 @@ describe('query by test id', () => {
651685
expect(queryByTestId('first-name')).not.toBeTruthy()
652686
})
653687

688+
test('queryByTestId matches case with non-string matcher', () => {
689+
const {queryByTestId} = render(`<span data-testid="1" />`)
690+
expect(queryByTestId(1)).toBeTruthy()
691+
})
692+
654693
test('can override test id attribute', () => {
655694
const {queryByTestId} = render(`<div data-my-test-id="theTestId"></div>`)
656695

@@ -732,6 +771,11 @@ test('queryAllByRole returns semantic html elements', () => {
732771
expect(queryAllByRole('listbox')).toHaveLength(1)
733772
})
734773

774+
test('queryByRole matches case with non-string matcher', () => {
775+
const {queryByRole} = render(`<span role="1" />`)
776+
expect(queryByRole(1)).toBeTruthy()
777+
})
778+
735779
test('getAll* matchers return an array', () => {
736780
const {
737781
getAllByAltText,

src/matches.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ function matches(textToMatch, node, matcher, normalizer) {
3131
assertNotNullOrUndefined(matcher)
3232

3333
const normalizedText = normalizer(textToMatch)
34-
if (typeof matcher === 'string') {
35-
return normalizedText === matcher
36-
} else if (typeof matcher === 'function') {
34+
if (matcher instanceof Function) {
3735
return matcher(normalizedText, node)
38-
} else {
36+
} else if (matcher instanceof RegExp) {
3937
return matcher.test(normalizedText)
38+
} else {
39+
return normalizedText === String(matcher)
4040
}
4141
}
4242

types/__tests__/type-tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export async function testQueries() {
2727
// element queries
2828
const element = document.createElement('div')
2929
getByText(element, 'foo')
30+
getByText(element, 1)
3031
queryByText(element, 'foo')
3132
await findByText(element, 'foo')
3233
await findByText(element, 'foo', undefined, {timeout: 10})

types/matches.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {ARIARole} from 'aria-query'
22

33
export type MatcherFunction = (content: string, element: HTMLElement) => boolean
4-
export type Matcher = string | RegExp | MatcherFunction
4+
export type Matcher = MatcherFunction | {}
55

66
// Get autocomplete for ARIARole union types, while still supporting another string
77
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
8-
export type ByRoleMatcher = ARIARole | (string & {}) | RegExp | MatcherFunction
8+
export type ByRoleMatcher = ARIARole | MatcherFunction | {}
99

1010
export type NormalizerFn = (text: string) => string
1111

0 commit comments

Comments
 (0)