Skip to content

Commit ee05201

Browse files
thymikeeMattAgn
authored and
MattAgn
committed
rebase and rearrange to a bit
1 parent 791c5d5 commit ee05201

File tree

3 files changed

+76
-70
lines changed

3 files changed

+76
-70
lines changed

src/__tests__/byTestId.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ test('getByTestId returns only native elements', () => {
9696
expect(getAllByTestId('button')).toHaveLength(1);
9797

9898
expect(() => getByTestId('myComponent')).toThrowError(
99-
'No instances found with testID: myComponent'
99+
'Unable to find an element with testID: myComponent'
100100
);
101101
expect(() => getAllByTestId('myComponent')).toThrowError(
102-
'No instances found with testID: myComponent'
102+
'Unable to find an element with testID: myComponent'
103103
);
104104
});
105105

@@ -123,7 +123,9 @@ test('getByTestId, queryByTestId', () => {
123123
const component = getByTestId('bananaFresh');
124124

125125
expect(component.props.children).toBe('not fresh');
126-
expect(() => getByTestId('InExistent')).toThrow('No instances found');
126+
expect(() => getByTestId('InExistent')).toThrow(
127+
'Unable to find an element with testID: InExistent'
128+
);
127129

128130
expect(getByTestId('bananaFresh')).toBe(component);
129131
expect(queryByTestId('InExistent')).toBeNull();
@@ -137,7 +139,7 @@ test('getAllByTestId, queryAllByTestId', () => {
137139
expect(textElements[0].props.children).toBe('First Text');
138140
expect(textElements[1].props.children).toBe('Second Text');
139141
expect(() => getAllByTestId('nonExistentTestId')).toThrow(
140-
'No instances found'
142+
'Unable to find an element with testID: nonExistentTestId'
141143
);
142144

143145
const queriedTextElements = queryAllByTestId('duplicateText');

src/helpers/byTestId.js

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,43 @@ const getNodeByTestId = (node, testID) => {
1313
: testID.test(node.props.testID);
1414
};
1515

16-
export const queryAllByTestId = (instance: ReactTestInstance) =>
17-
function getAllByTestIdFn(testID: string | RegExp): ReactTestInstance[] {
16+
export const queryAllByTestId = (
17+
instance: ReactTestInstance
18+
): ((testId: string | RegExp) => Array<ReactTestInstance>) =>
19+
function getAllByTestIdFn(testId) {
1820
const results = instance
19-
.findAll((node) => getNodeByTestId(node, testID))
21+
.findAll((node) => getNodeByTestId(node, testId))
2022
.filter((element) => typeof element.type === 'string');
2123

2224
return results;
2325
};
2426

25-
export const getAllByTestId = (instance: ReactTestInstance) =>
26-
makeGetAllQuery(
27-
queryAllByTestId,
28-
instance,
29-
(testId) => `No instances found with testID: ${String(testId)}`
30-
);
31-
32-
export const queryByTestId = (instance: ReactTestInstance) =>
33-
makeSingleQuery(
34-
queryAllByTestId,
35-
instance,
36-
(testId: string, nbResults: number) =>
37-
` Expected 1 but found ${nbResults} instances with testID: ${String(
38-
testId
39-
)}`
40-
);
41-
42-
export const getByTestId = (instance: ReactTestInstance) =>
43-
makeGetQuery(
44-
queryAllByTestId,
45-
instance,
46-
(testId: string, nbResults: number) =>
47-
nbResults > 1
48-
? ` Expected 1 but found ${nbResults} instances with testID: ${String(
49-
testId
50-
)}`
51-
: `No instances found with testID: ${String(testId)}`
52-
);
53-
54-
export const findAllByTestId = (instance: ReactTestInstance) =>
27+
const getMultipleError = (testId) =>
28+
`Found multiple elements with testID: ${String(testId)}`;
29+
const getMissingError = (testId) =>
30+
`Unable to find an element with testID: ${String(testId)}`;
31+
32+
export const getAllByTestId = (
33+
instance: ReactTestInstance
34+
): ((testId: string | RegExp) => Array<ReactTestInstance>) =>
35+
makeGetAllQuery(queryAllByTestId, instance, getMissingError);
36+
37+
export const queryByTestId = (
38+
instance: ReactTestInstance
39+
): ((testId: string | RegExp) => ReactTestInstance | null) =>
40+
makeSingleQuery(queryAllByTestId, instance, getMultipleError);
41+
42+
export const getByTestId = (
43+
instance: ReactTestInstance
44+
): ((testId: string | RegExp) => ReactTestInstance) =>
45+
makeGetQuery(queryAllByTestId, instance, getMultipleError, getMissingError);
46+
47+
export const findAllByTestId = (
48+
instance: ReactTestInstance
49+
): ((testId: string | RegExp) => Promise<Array<ReactTestInstance>>) =>
5550
makeFindAllQuery(getAllByTestId, instance);
5651

57-
export const findByTestId = (instance: ReactTestInstance) =>
52+
export const findByTestId = (
53+
instance: ReactTestInstance
54+
): ((testId: string | RegExp) => Promise<ReactTestInstance>) =>
5855
makeFindQuery(getByTestId, instance);

src/helpers/makeQueries.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,36 @@ import waitFor from '../waitFor';
33
import type { WaitForOptions } from '../waitFor';
44
import { ErrorWithStack } from './errors';
55

6-
type AllQuery = (
6+
type AllQuery = <T>(
77
instance: ReactTestInstance
8-
) => (args: any) => Array<ReactTestInstance>;
8+
) => (...args: T) => Array<ReactTestInstance>;
99

10-
export const makeGetAllQuery = (
10+
export function makeGetAllQuery<T>(
1111
allQuery: AllQuery,
1212
instance: ReactTestInstance,
13-
getMissingElementError: (args: any) => string
14-
) =>
15-
function getAllFn(...args: Array<any>) {
13+
getMissingError: (args: T) => string
14+
): (...args: Array<T>) => Array<ReactTestInstance> {
15+
return function getAllFn(...args: Array<T>) {
1616
const results = allQuery(instance)(...args);
17-
if (!results.length) {
18-
throw new ErrorWithStack(getMissingElementError(...args), getAllFn);
17+
18+
if (results.length === 0) {
19+
throw new ErrorWithStack(getMissingError(...args), getAllFn);
1920
}
2021

2122
return results;
2223
};
24+
}
2325

24-
export const makeSingleQuery = (
26+
export function makeSingleQuery<T>(
2527
allQuery: AllQuery,
2628
instance: ReactTestInstance,
27-
getMissingElementError: (args: any, nbResults: number) => string
28-
) =>
29-
function singleQueryFn(...args: Array<any>) {
29+
getMultipleError: (args: T) => string
30+
): (...args: Array<T>) => null | ReactTestInstance {
31+
return function singleQueryFn(...args: Array<T>) {
3032
const results = allQuery(instance)(...args);
3133

3234
if (results.length > 1) {
33-
throw new ErrorWithStack(
34-
getMissingElementError(...args, results.length),
35-
singleQueryFn
36-
);
35+
throw new ErrorWithStack(getMultipleError(...args), singleQueryFn);
3736
}
3837

3938
if (results.length === 0) {
@@ -42,37 +41,45 @@ export const makeSingleQuery = (
4241

4342
return results[0];
4443
};
44+
}
4545

46-
export const makeGetQuery = (
46+
export function makeGetQuery<T>(
4747
allQuery: AllQuery,
4848
instance: ReactTestInstance,
49-
getMissingElementError: (args: any, nbResults: number) => string
50-
) =>
51-
function getFn(...args: Array<any>) {
49+
getMultipleError: (args: T) => string,
50+
getMissingError: (args: T) => string
51+
): (...args: Array<T>) => ReactTestInstance {
52+
return function getFn(...args: Array<T>) {
5253
const results = allQuery(instance)(...args);
5354

54-
if (results.length !== 1) {
55-
throw new ErrorWithStack(
56-
getMissingElementError(...args, results.length),
57-
getFn
58-
);
55+
if (results.length > 1) {
56+
throw new ErrorWithStack(getMultipleError(...args), getFn);
57+
}
58+
59+
if (results.length === 0) {
60+
throw new ErrorWithStack(getMissingError(...args), getFn);
5961
}
6062

6163
return results[0];
6264
};
65+
}
6366

64-
export const makeFindAllQuery = (
67+
export function makeFindAllQuery<T>(
6568
getAllQuery: AllQuery,
6669
instance: ReactTestInstance
67-
) =>
68-
function findAllFn(args: any, waitForOptions: WaitForOptions = {}) {
70+
): (
71+
args: T,
72+
waitForOptions?: WaitForOptions
73+
) => Promise<Array<ReactTestInstance>> {
74+
return function findAllFn(args: T, waitForOptions: WaitForOptions = {}) {
6975
return waitFor(() => getAllQuery(instance)(args), waitForOptions);
7076
};
71-
72-
export const makeFindQuery = (
77+
}
78+
export function makeFindQuery<T>(
7379
getQuery: (instance: ReactTestInstance) => (args: any) => ReactTestInstance,
7480
instance: ReactTestInstance
75-
) =>
76-
function findFn(args: any, waitForOptions: WaitForOptions = {}) {
81+
): (args: T, waitForOptions?: WaitForOptions) => Promise<ReactTestInstance> {
82+
return function findFn(args: T, waitForOptions: WaitForOptions = {}) {
7783
return waitFor(() => getQuery(instance)(args), waitForOptions);
7884
};
85+
}

0 commit comments

Comments
 (0)