Skip to content

Commit 6491ef1

Browse files
authored
fix(types): add fourth param to build findAllBy and findBy queries (#574)
1 parent dcad11b commit 6491ef1

File tree

2 files changed

+91
-32
lines changed

2 files changed

+91
-32
lines changed

types/__tests__/type-tests.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import {
22
fireEvent,
33
isInaccessible,
44
queries,
5+
buildQueries,
6+
queryAllByAttribute,
57
screen,
68
waitFor,
79
waitForElementToBeRemoved,
10+
MatcherOptions,
811
} from '../index'
912

1013
const {
@@ -42,6 +45,42 @@ async function testQueries() {
4245
await screen.findAllByText('bar', undefined, {timeout: 10})
4346
}
4447

48+
async function testQueryHelpers() {
49+
const element = document.createElement('div')
50+
const includesAutomationId = (content: string, automationId: string) =>
51+
content.split(/\s+/).some(id => id === automationId)
52+
const queryAllByAutomationId = (
53+
container: HTMLElement,
54+
automationId: string | string[],
55+
options?: MatcherOptions,
56+
) =>
57+
queryAllByAttribute(
58+
'testId',
59+
container,
60+
content =>
61+
Array.isArray(automationId)
62+
? automationId.every(id => includesAutomationId(content, id))
63+
: includesAutomationId(content, automationId),
64+
options,
65+
)
66+
const [
67+
queryByAutomationId,
68+
getAllByAutomationId,
69+
getByAutomationId,
70+
findAllByAutomationId,
71+
findByAutomationId,
72+
] = buildQueries(
73+
queryAllByAutomationId,
74+
() => 'Multiple Error',
75+
() => 'Missing Error',
76+
)
77+
queryByAutomationId(element, 'id')
78+
getAllByAutomationId(element, 'id')
79+
getByAutomationId(element, ['id', 'automationId'])
80+
findAllByAutomationId(element, 'id', {}, {timeout: 1000})
81+
findByAutomationId(element, 'id', {}, {timeout: 1000})
82+
}
83+
4584
async function testByRole() {
4685
const element = document.createElement('button')
4786
element.setAttribute('aria-hidden', 'true')
@@ -116,7 +155,11 @@ async function testWaitFors() {
116155

117156
element.innerHTML = '<span>apple</span>'
118157

119-
await waitForElementToBeRemoved(() => getByText(element, 'apple'), {interval: 3000, container: element, timeout: 5000})
158+
await waitForElementToBeRemoved(() => getByText(element, 'apple'), {
159+
interval: 3000,
160+
container: element,
161+
timeout: 5000,
162+
})
120163
await waitForElementToBeRemoved(getByText(element, 'apple'))
121164
await waitForElementToBeRemoved(getAllByText(element, 'apple'))
122165
}

types/query-helpers.d.ts

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,62 @@
1-
import { Matcher, MatcherOptions } from './matches';
1+
import { Matcher, MatcherOptions } from './matches'
2+
import { waitForOptions } from './wait-for'
23

34
export interface SelectorMatcherOptions extends MatcherOptions {
4-
selector?: string;
5+
selector?: string
56
}
67

78
export type QueryByAttribute = (
8-
attribute: string,
9-
container: HTMLElement,
10-
id: Matcher,
11-
options?: MatcherOptions,
12-
) => HTMLElement | null;
9+
attribute: string,
10+
container: HTMLElement,
11+
id: Matcher,
12+
options?: MatcherOptions,
13+
) => HTMLElement | null
1314

1415
export type AllByAttribute = (
15-
attribute: string,
16-
container: HTMLElement,
17-
id: Matcher,
18-
options?: MatcherOptions,
19-
) => HTMLElement[];
16+
attribute: string,
17+
container: HTMLElement,
18+
id: Matcher,
19+
options?: MatcherOptions,
20+
) => HTMLElement[]
2021

21-
export const queryByAttribute: QueryByAttribute;
22-
export const queryAllByAttribute: AllByAttribute;
23-
export function getElementError(message: string, container: HTMLElement): Error;
22+
export const queryByAttribute: QueryByAttribute
23+
export const queryAllByAttribute: AllByAttribute
24+
export function getElementError(message: string, container: HTMLElement): Error
2425

2526
/**
2627
* query methods have a common call signature. Only the return type differs.
2728
*/
28-
export type QueryMethod<Arguments extends any[], Return> = (container: HTMLElement, ...args: Arguments) => Return;
29-
export type QueryBy<Arguments extends any[]> = QueryMethod<Arguments, HTMLElement | null>;
30-
export type GetAllBy<Arguments extends any[]> = QueryMethod<Arguments, HTMLElement[]>;
31-
export type FindAllBy<Arguments extends any[]> = QueryMethod<Arguments, Promise<HTMLElement[]>>;
32-
export type GetBy<Arguments extends any[]> = QueryMethod<Arguments, HTMLElement>;
33-
export type FindBy<Arguments extends any[]> = QueryMethod<Arguments, Promise<HTMLElement>>;
29+
export type QueryMethod<Arguments extends any[], Return> = (
30+
container: HTMLElement,
31+
...args: Arguments
32+
) => Return
33+
export type QueryBy<Arguments extends any[]> = QueryMethod<
34+
Arguments,
35+
HTMLElement | null
36+
>
37+
export type GetAllBy<Arguments extends any[]> = QueryMethod<
38+
Arguments,
39+
HTMLElement[]
40+
>
41+
export type FindAllBy<Arguments extends any[]> = QueryMethod<
42+
[Arguments[0], Arguments[1], waitForOptions],
43+
Promise<HTMLElement[]>
44+
>
45+
export type GetBy<Arguments extends any[]> = QueryMethod<Arguments, HTMLElement>
46+
export type FindBy<Arguments extends any[]> = QueryMethod<
47+
[Arguments[0], Arguments[1], waitForOptions],
48+
Promise<HTMLElement>
49+
>
3450

3551
export type BuiltQueryMethods<Arguments extends any[]> = [
36-
QueryBy<Arguments>,
37-
GetAllBy<Arguments>,
38-
GetBy<Arguments>,
39-
FindAllBy<Arguments>,
40-
FindBy<Arguments>
41-
];
52+
QueryBy<Arguments>,
53+
GetAllBy<Arguments>,
54+
GetBy<Arguments>,
55+
FindAllBy<Arguments>,
56+
FindBy<Arguments>,
57+
]
4258
export function buildQueries<Arguments extends any[]>(
43-
queryByAll: GetAllBy<Arguments>,
44-
getMultipleError: (container: HTMLElement, ...args: Arguments) => string,
45-
getMissingError: (container: HTMLElement, ...args: Arguments) => string,
46-
): BuiltQueryMethods<Arguments>;
59+
queryByAll: GetAllBy<Arguments>,
60+
getMultipleError: (container: HTMLElement, ...args: Arguments) => string,
61+
getMissingError: (container: HTMLElement, ...args: Arguments) => string,
62+
): BuiltQueryMethods<Arguments>

0 commit comments

Comments
 (0)