Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add check for container type on queries (#604)
This is intended to give an informative warning when a base-query is called with a container that's neitheir an Element, Document or DocumentFragment. Closes: #537
- Loading branch information
Showing
11 changed files
with
198 additions
and
7 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/__tests__/base-queries-warn-on-invalid-container.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { | ||
getByLabelText, | ||
getAllByLabelText, | ||
queryAllByLabelText, | ||
queryByLabelText, | ||
findByLabelText, | ||
findAllByLabelText, | ||
getByPlaceholderText, | ||
getAllByPlaceholderText, | ||
queryAllByPlaceholderText, | ||
queryByPlaceholderText, | ||
findByPlaceholderText, | ||
findAllByPlaceholderText, | ||
getByText, | ||
getAllByText, | ||
queryAllByText, | ||
queryByText, | ||
findByText, | ||
findAllByText, | ||
getByAltText, | ||
getAllByAltText, | ||
queryAllByAltText, | ||
queryByAltText, | ||
findByAltText, | ||
findAllByAltText, | ||
getByTitle, | ||
getAllByTitle, | ||
queryAllByTitle, | ||
queryByTitle, | ||
findByTitle, | ||
findAllByTitle, | ||
getByDisplayValue, | ||
getAllByDisplayValue, | ||
queryAllByDisplayValue, | ||
queryByDisplayValue, | ||
findByDisplayValue, | ||
findAllByDisplayValue, | ||
getByRole, | ||
getAllByRole, | ||
queryAllByRole, | ||
queryByRole, | ||
findByRole, | ||
findAllByRole, | ||
getByTestId, | ||
getAllByTestId, | ||
queryAllByTestId, | ||
queryByTestId, | ||
findByTestId, | ||
findAllByTestId, | ||
} from '..' | ||
|
||
describe('synchronous queries throw on invalid container type', () => { | ||
test.each([ | ||
['getByLabelText', getByLabelText], | ||
['getAllByLabelText', getAllByLabelText], | ||
['queryByLabelText', queryByLabelText], | ||
['queryAllByLabelText', queryAllByLabelText], | ||
['getByPlaceholderText', getByPlaceholderText], | ||
['getAllByPlaceholderText', getAllByPlaceholderText], | ||
['queryByPlaceholderText', queryByPlaceholderText], | ||
['queryAllByPlaceholderText', queryAllByPlaceholderText], | ||
['getByText', getByText], | ||
['getAllByText', getAllByText], | ||
['queryByText', queryByText], | ||
['queryAllByText', queryAllByText], | ||
['getByAltText', getByAltText], | ||
['getAllByAltText', getAllByAltText], | ||
['queryByAltText', queryByAltText], | ||
['queryAllByAltText', queryAllByAltText], | ||
['getByTitle', getByTitle], | ||
['getAllByTitle', getAllByTitle], | ||
['queryByTitle', queryByTitle], | ||
['queryAllByTitle', queryAllByTitle], | ||
['getByDisplayValue', getByDisplayValue], | ||
['getAllByDisplayValue', getAllByDisplayValue], | ||
['queryByDisplayValue', queryByDisplayValue], | ||
['queryAllByDisplayValue', queryAllByDisplayValue], | ||
['getByRole', getByRole], | ||
['getAllByRole', getAllByRole], | ||
['queryByRole', queryByRole], | ||
['queryAllByRole', queryAllByRole], | ||
['getByTestId', getByTestId], | ||
['getAllByTestId', getAllByTestId], | ||
['queryByTestId', queryByTestId], | ||
['queryAllByTestId', queryAllByTestId], | ||
])('%s', (_queryName, query) => { | ||
expect(() => | ||
query('invalid type for container', 'irrelevant text'), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Expected container to be an Element, a Document or a DocumentFragment but got string."`, | ||
) | ||
}) | ||
}) | ||
|
||
describe('asynchronous queries throw on invalid container type', () => { | ||
test.each([ | ||
['findByLabelText', findByLabelText], | ||
['findAllByLabelText', findAllByLabelText], | ||
['findByPlaceholderText', findByPlaceholderText], | ||
['findAllByPlaceholderText', findAllByPlaceholderText], | ||
['findByText', findByText], | ||
['findAllByText', findAllByText], | ||
['findByAltText', findByAltText], | ||
['findAllByAltText', findAllByAltText], | ||
['findByTitle', findByTitle], | ||
['findAllByTitle', findAllByTitle], | ||
['findByDisplayValue', findByDisplayValue], | ||
['findAllByDisplayValue', findAllByDisplayValue], | ||
['findByRole', findByRole], | ||
['findAllByRole', findAllByRole], | ||
['findByTestId', findByTestId], | ||
['findAllByTestId', findAllByTestId], | ||
])('%s', (_queryName, query) => { | ||
const queryOptions = {} | ||
const waitOptions = {timeout: 1} | ||
return expect( | ||
query( | ||
'invalid type for container', | ||
'irrelevant text', | ||
queryOptions, | ||
waitOptions, | ||
), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"Expected container to be an Element, a Document or a DocumentFragment but got string."`, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
import {getDocument} from '../helpers' | ||
import {getDocument, checkContainerType} from '../helpers' | ||
|
||
test('returns global document if exists', () => { | ||
expect(getDocument()).toBe(document) | ||
}) | ||
|
||
describe('query container validation throws when validation fails', () => { | ||
test('undefined as container', () => { | ||
expect(() => | ||
checkContainerType(undefined), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Expected container to be an Element, a Document or a DocumentFragment but got undefined."`, | ||
) | ||
}) | ||
test('null as container', () => { | ||
expect(() => checkContainerType(null)).toThrowErrorMatchingInlineSnapshot( | ||
`"Expected container to be an Element, a Document or a DocumentFragment but got null."`, | ||
) | ||
}) | ||
test('array as container', () => { | ||
expect(() => checkContainerType([])).toThrowErrorMatchingInlineSnapshot( | ||
`"Expected container to be an Element, a Document or a DocumentFragment but got Array."`, | ||
) | ||
}) | ||
test('object as container', () => { | ||
expect(() => checkContainerType({})).toThrowErrorMatchingInlineSnapshot( | ||
`"Expected container to be an Element, a Document or a DocumentFragment but got Object."`, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters