@@ -577,6 +597,11 @@ test('query/get title element of SVG', () => {
expect(queryByTitle('Close').id).toEqual('svg-title')
})
+test('queryByTitle matches case with non-string matcher', () => {
+ const {queryByTitle} = render(`
`)
+ expect(queryByTitle(1)).toBeTruthy()
+})
+
test('query/get element by its value', () => {
const {getByDisplayValue, queryByDisplayValue} = render(`
@@ -632,6 +657,15 @@ test('query/get select by text with multiple options selected', () => {
expect(queryByDisplayValue('Alaska').id).toEqual('state-select')
})
+test('queryByDisplayValue matches case with non-string matcher', () => {
+ const {queryByDisplayValue} = render(`
+
+ `)
+ expect(queryByDisplayValue(1)).toBeTruthy()
+})
+
describe('query by test id', () => {
afterEach(() => {
// Restore the default test id attribute
@@ -651,6 +685,11 @@ describe('query by test id', () => {
expect(queryByTestId('first-name')).not.toBeTruthy()
})
+ test('queryByTestId matches case with non-string matcher', () => {
+ const {queryByTestId} = render(`
`)
+ expect(queryByTestId(1)).toBeTruthy()
+ })
+
test('can override test id attribute', () => {
const {queryByTestId} = render(`
`)
@@ -732,6 +771,11 @@ test('queryAllByRole returns semantic html elements', () => {
expect(queryAllByRole('listbox')).toHaveLength(1)
})
+test('queryByRole matches case with non-string matcher', () => {
+ const {queryByRole} = render(`
`)
+ expect(queryByRole(1)).toBeTruthy()
+})
+
test('getAll* matchers return an array', () => {
const {
getAllByAltText,
diff --git a/src/matches.js b/src/matches.js
index baad25bb..d9672e42 100644
--- a/src/matches.js
+++ b/src/matches.js
@@ -31,12 +31,12 @@ function matches(textToMatch, node, matcher, normalizer) {
assertNotNullOrUndefined(matcher)
const normalizedText = normalizer(textToMatch)
- if (typeof matcher === 'string') {
- return normalizedText === matcher
- } else if (typeof matcher === 'function') {
+ if (matcher instanceof Function) {
return matcher(normalizedText, node)
- } else {
+ } else if (matcher instanceof RegExp) {
return matcher.test(normalizedText)
+ } else {
+ return normalizedText === String(matcher)
}
}
diff --git a/types/__tests__/type-tests.ts b/types/__tests__/type-tests.ts
index f26a0da4..10fcd540 100644
--- a/types/__tests__/type-tests.ts
+++ b/types/__tests__/type-tests.ts
@@ -27,6 +27,7 @@ export async function testQueries() {
// element queries
const element = document.createElement('div')
getByText(element, 'foo')
+ getByText(element, 1)
queryByText(element, 'foo')
await findByText(element, 'foo')
await findByText(element, 'foo', undefined, {timeout: 10})
diff --git a/types/matches.d.ts b/types/matches.d.ts
index 86fc007c..d4da667b 100644
--- a/types/matches.d.ts
+++ b/types/matches.d.ts
@@ -1,11 +1,11 @@
import {ARIARole} from 'aria-query'
export type MatcherFunction = (content: string, element: HTMLElement) => boolean
-export type Matcher = string | RegExp | MatcherFunction
+export type Matcher = MatcherFunction | {}
// Get autocomplete for ARIARole union types, while still supporting another string
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972
-export type ByRoleMatcher = ARIARole | (string & {}) | RegExp | MatcherFunction
+export type ByRoleMatcher = ARIARole | MatcherFunction | {}
export type NormalizerFn = (text: string) => string