diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index 3556dae1..3e3ee6de 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -144,6 +144,11 @@ test('matches case with RegExp matcher', () => { expect(queryByText(/Step 1 of 4/)).not.toBeTruthy() }) +test('queryByText matches case with non-string matcher', () => { + const {queryByText} = render(`1`) + expect(queryByText(1)).toBeTruthy() +}) + test('can get form controls by label text', () => { const {getByLabelText} = render(`
@@ -338,6 +343,11 @@ test('get can get form controls by placeholder', () => { expect(getByPlaceholderText('username').id).toBe('username-id') }) +test('queryByPlaceholderText matches case with non-string matcher', () => { + const {queryByPlaceholderText} = render(``) + expect(queryByPlaceholderText(1)).toBeTruthy() +}) + test('label with no form control', () => { const {getByLabelText, queryByLabelText} = render(``) expect(queryByLabelText(/alone/)).toBeNull() @@ -535,6 +545,11 @@ test('getByLabelText with aria-label', () => { expect(queryByLabelText(/bat/)).toBeTruthy() }) +test('queryByLabelText matches case with non-string matcher', () => { + const {queryByLabelText} = render(``) + expect(queryByLabelText(1)).toBeTruthy() +}) + test('get element by its alt text', () => { const {getByAltText} = render(`
@@ -545,6 +560,11 @@ test('get element by its alt text', () => { expect(getByAltText(/fin.*nem.*poster$/i).src).toContain('/finding-nemo.png') }) +test('queryByAltText matches case with non-string matcher', () => { + const {queryByAltText} = render(`1`) + expect(queryByAltText(1)).toBeTruthy() +}) + test('query/get element by its title', () => { const {getByTitle, queryByTitle} = render(`
@@ -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