Skip to content

Commit

Permalink
fix(TS): Add missing getAll and queryAll functions to TS definitions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pbomb authored and Kent C. Dodds committed May 18, 2018
1 parent de769ec commit fc09a99
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
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 <[email protected]> (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

0 comments on commit fc09a99

Please sign in to comment.