Skip to content

fix(TS): Add missing getAll and queryAll functions to TS definitions #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ facilitate testing implementation details). Read more about this in
* [`prettyDOM`](#prettydom)
* [`TextMatch`](#textmatch)
* [`query` APIs](#query-apis)
* [`queryAll` and `getAll` APIs](#queryall-and-getall-apis)
* [Examples](#examples)
* [FAQ](#faq)
* [Other Solutions](#other-solutions)
Expand Down Expand Up @@ -563,6 +564,16 @@ const submitButton = queryByText('submit')
expect(submitButton).toBeNull() // it doesn't exist
```
## `queryAll` and `getAll` APIs
Each of the `query` APIs have a corresponsing `queryAll` version that always returns an Array of matching nodes. `getAll` is the same but throws when the array has a length of 0.
```javascript
const submitButtons = queryAllByText('submit')
expect(submitButtons).toHaveLength(3) // expect 3 elements
expect(submitButtons[0]).toBeInTheDOM()
```
## Examples
You'll find examples of testing with different libraries in
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
"license": "MIT",
"dependencies": {
"dom-testing-library": "^2.0.0",
"dom-testing-library": "^2.3.1",
"wait-for-expect": "^0.5.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ReactDOM from 'react-dom'
import {Simulate} from 'react-dom/test-utils'
import {bindElementToQueries, prettyDOM} from 'dom-testing-library'
import {getQueriesForElement, prettyDOM} from 'dom-testing-library'

function render(ui, {container = document.createElement('div')} = {}) {
ReactDOM.render(ui, container)
Expand All @@ -14,7 +14,7 @@ function render(ui, {container = document.createElement('div')} = {}) {
// Intentionally do not return anything to avoid unnecessarily complicating the API.
// folks can use all the same utilities we return in the first place that are bound to the container
},
...bindElementToQueries(container),
...getQueriesForElement(container),
}
}

Expand Down
65 changes: 36 additions & 29 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import {Simulate as ReactSimulate} from 'react-dom/test-utils'
import {
AllByAttribute,
AllByText,
BoundFunction,
GetByAttribute,
GetByText,
getQueriesForElement,
QueryByAttribute,
QueryByText,
} from 'dom-testing-library'

type TextMatchFunction = (content: string, element: HTMLElement) => boolean
type TextMatch = string | RegExp | TextMatchFunction
Expand All @@ -8,38 +18,33 @@ type TextMatchOptions = {
collapseWhitespace?: boolean
}

interface RenderResult {
interface GetsAndQueries {
queryByTestId: BoundFunction<QueryByAttribute>
queryAllByTestId: BoundFunction<AllByAttribute>
getByTestId: BoundFunction<GetByAttribute>
getAllByTestId: BoundFunction<AllByAttribute>
queryByText: BoundFunction<QueryByText>
queryAllByText: BoundFunction<AllByText>
getByText: BoundFunction<GetByText>
getAllByText: BoundFunction<AllByText>
queryByPlaceholderText: BoundFunction<QueryByAttribute>
queryAllByPlaceholderText: BoundFunction<AllByAttribute>
getByPlaceholderText: BoundFunction<GetByAttribute>
getAllByPlaceholderText: BoundFunction<AllByAttribute>
queryByLabelText: BoundFunction<QueryByAttribute>
queryAllByLabelText: BoundFunction<AllByAttribute>
getByLabelText: BoundFunction<GetByAttribute>
getAllByLabelText: BoundFunction<AllByAttribute>
queryByAltText: BoundFunction<QueryByAttribute>
queryAllByAltText: BoundFunction<AllByAttribute>
getByAltText: BoundFunction<GetByAttribute>
getAllByAltText: BoundFunction<AllByAttribute>
}

interface RenderResult extends GetsAndQueries {
container: HTMLDivElement
rerender: (ui: React.ReactElement<any>) => void
unmount: VoidFunction
queryByTestId: (
id: TextMatch,
options?: TextMatchOptions,
) => HTMLElement | null
getByTestId: (id: TextMatch, options?: TextMatchOptions) => HTMLElement
queryByText: (id: TextMatch, options?: TextMatchOptions) => HTMLElement | null
getByText: (text: TextMatch, options?: TextMatchOptions) => HTMLElement
queryByPlaceholderText: (
id: TextMatch,
options?: TextMatchOptions,
) => HTMLElement | null
getByPlaceholderText: (
text: TextMatch,
options?: TextMatchOptions,
) => HTMLElement
queryByLabelText: (
text: TextMatch,
options?: TextMatchOptions,
) => HTMLElement | null
getByLabelText: (
id: TextMatch,
options?: {selector?: string} & TextMatchOptions,
) => HTMLElement
queryByAltText: (
text: TextMatch,
options?: TextMatchOptions,
) => HTMLElement | null
getByAltText: (text: TextMatch, options?: TextMatchOptions) => HTMLElement
}

export function render(
Expand Down Expand Up @@ -147,3 +152,5 @@ export const fireEvent: FireFunction & FireObject
export function renderIntoDocument(ui: React.ReactElement<any>): RenderResult

export function cleanup(): void

export function getQueriesForElement(element: HTMLElement): GetsAndQueries