Skip to content

Commit fa198c4

Browse files
authored
feat: add getByType helper (#64)
* feat: add getByType helper * add docs
1 parent 235942b commit fa198c4

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ A method returning a `ReactTestInstance` with matching a React component type. T
113113

114114
A method returning an array of `ReactTestInstance`s with matching a React component type.
115115

116+
### `getByType: (type: React.ComponentType<*>)`
117+
118+
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
119+
120+
### `getAllByType: (type: React.ComponentType<*>)`
121+
122+
A method returning an array of `ReactTestInstance`s with matching a React component type.
123+
116124
### `getByText: (text: string | RegExp)`
117125

118126
A method returning a `ReactTestInstance` with matching text – may be a string or regular expression. Throws when no matches.

src/__tests__/render.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ test('getAllByName, queryAllByName', () => {
9696
expect(queryAllByName('InExistent')).toHaveLength(0);
9797
});
9898

99+
test('getAllByType, queryAllByType', () => {
100+
const { getAllByType, queryAllByType } = render(<Banana />);
101+
const [text, status, button] = getAllByType(Text);
102+
const InExistent = () => null;
103+
104+
expect(text.props.children).toBe('Is the banana fresh?');
105+
expect(status.props.children).toBe('not fresh');
106+
expect(button.props.children).toBe('Change freshness!');
107+
expect(() => getAllByType(InExistent)).toThrow('No instances found');
108+
109+
expect(queryAllByType(Text)[1]).toBe(status);
110+
expect(queryAllByType(InExistent)).toHaveLength(0);
111+
});
112+
99113
test('getByText, queryByText', () => {
100114
const { getByText, queryByText } = render(<Banana />);
101115
const button = getByText(/change/i);

src/helpers/getByAPI.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ export const getByName = (instance: ReactTestInstance) =>
4040
}
4141
};
4242

43+
export const getByType = (instance: ReactTestInstance) =>
44+
function getByTypeFn(type: React.ComponentType<*>) {
45+
try {
46+
return instance.findByType(type);
47+
} catch (error) {
48+
throw new ErrorWithStack(prepareErrorMessage(error), getByTypeFn);
49+
}
50+
};
51+
4352
export const getByText = (instance: ReactTestInstance) =>
4453
function getByTextFn(text: string | RegExp) {
4554
try {
@@ -80,6 +89,15 @@ export const getAllByName = (instance: ReactTestInstance) =>
8089
return results;
8190
};
8291

92+
export const getAllByType = (instance: ReactTestInstance) =>
93+
function getAllByTypeFn(type: React.ComponentType<*>) {
94+
const results = instance.findAllByType(type);
95+
if (results.length === 0) {
96+
throw new ErrorWithStack('No instances found', getAllByTypeFn);
97+
}
98+
return results;
99+
};
100+
83101
export const getAllByText = (instance: ReactTestInstance) =>
84102
function getAllByTextFn(text: string | RegExp) {
85103
const results = instance.findAll(node => getNodeByText(node, text));
@@ -107,9 +125,11 @@ export const getAllByProps = (instance: ReactTestInstance) =>
107125
export const getByAPI = (instance: ReactTestInstance) => ({
108126
getByTestId: getByTestId(instance),
109127
getByName: getByName(instance),
128+
getByType: getByType(instance),
110129
getByText: getByText(instance),
111130
getByProps: getByProps(instance),
112131
getAllByName: getAllByName(instance),
132+
getAllByType: getAllByType(instance),
113133
getAllByText: getAllByText(instance),
114134
getAllByProps: getAllByProps(instance),
115135
});

src/helpers/queryByAPI.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import * as React from 'react';
33
import {
44
getByTestId,
55
getByName,
6+
getByType,
67
getByText,
78
getByProps,
89
getAllByName,
10+
getAllByType,
911
getAllByText,
1012
getAllByProps,
1113
} from './getByAPI';
@@ -20,6 +22,16 @@ export const queryByName = (instance: ReactTestInstance) => (
2022
}
2123
};
2224

25+
export const queryByType = (instance: ReactTestInstance) => (
26+
type: React.ComponentType<*>
27+
) => {
28+
try {
29+
return getByType(instance)(type);
30+
} catch (error) {
31+
return null;
32+
}
33+
};
34+
2335
export const queryByText = (instance: ReactTestInstance) => (
2436
text: string | RegExp
2537
) => {
@@ -60,6 +72,16 @@ export const queryAllByName = (instance: ReactTestInstance) => (
6072
}
6173
};
6274

75+
export const queryAllByType = (instance: ReactTestInstance) => (
76+
type: React.ComponentType<*>
77+
) => {
78+
try {
79+
return getAllByType(instance)(type);
80+
} catch (error) {
81+
return [];
82+
}
83+
};
84+
6385
export const queryAllByText = (instance: ReactTestInstance) => (
6486
text: string | RegExp
6587
) => {
@@ -83,9 +105,11 @@ export const queryAllByProps = (instance: ReactTestInstance) => (props: {
83105
export const queryByAPI = (instance: ReactTestInstance) => ({
84106
queryByTestId: queryByTestId(instance),
85107
queryByName: queryByName(instance),
108+
queryByType: queryByType(instance),
86109
queryByText: queryByText(instance),
87110
queryByProps: queryByProps(instance),
88111
queryAllByName: queryAllByName(instance),
112+
queryAllByType: queryAllByType(instance),
89113
queryAllByText: queryAllByText(instance),
90114
queryAllByProps: queryAllByProps(instance),
91115
});

typings/__tests__/index.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const tree = render(<TestComponent />);
2323
// getByAPI tests
2424
const getByNameString: ReactTestInstance = tree.getByName('View');
2525
const getByNameContructor: ReactTestInstance = tree.getByName(View);
26+
const getByType: ReactTestInstance = tree.getByType(View);
2627
const getByTextString: ReactTestInstance = tree.getByText('<View />');
2728
const getByTextRegExp: ReactTestInstance = tree.getByText(/View/g);
2829
const getByProps: ReactTestInstance = tree.getByProps({ value: 2 });
@@ -31,6 +32,7 @@ const getAllByNameString: Array<ReactTestInstance> = tree.getAllByName('View');
3132
const getAllByNameConstructor: Array<ReactTestInstance> = tree.getAllByName(
3233
View
3334
);
35+
const getAllByType: Array<ReactTestInstance> = tree.getAllByType(View);
3436
const getAllByTextString: Array<ReactTestInstance> = tree.getAllByText(
3537
'<View />'
3638
);
@@ -42,6 +44,7 @@ const getAllByProps: Array<ReactTestInstance> = tree.getAllByProps({
4244
// queuryByAPI tests
4345
const queryByNameString: ReactTestInstance | null = tree.queryByName('View');
4446
const queryByNameConstructor: ReactTestInstance | null = tree.queryByName(View);
47+
const queryByType: ReactTestInstance | null = tree.queryByType(View);
4548
const queryByTextString: ReactTestInstance | null = tree.queryByText(
4649
'<View />'
4750
);
@@ -54,6 +57,7 @@ const queryAllByNameString: Array<ReactTestInstance> = tree.getAllByName(
5457
const queryAllByNameConstructor: Array<ReactTestInstance> = tree.getAllByName(
5558
View
5659
);
60+
const queryAllByType: Array<ReactTestInstance> = tree.getAllByType(View);
5761
const queryAllByTextString: Array<ReactTestInstance> = tree.queryAllByText(
5862
'View'
5963
);

typings/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ import { ReactTestInstance, ReactTestRendererJSON } from 'react-test-renderer';
33

44
export interface GetByAPI {
55
getByName: (name: React.ReactType) => ReactTestInstance;
6+
getByType: (type: React.ComponentType) => ReactTestInstance;
67
getByText: (text: string | RegExp) => ReactTestInstance;
78
getByProps: (props: Record<string, any>) => ReactTestInstance;
89
getByTestId: (testID: string) => ReactTestInstance;
910
getAllByName: (name: React.ReactType) => Array<ReactTestInstance>;
11+
getAllByType: (type: React.ComponentType) => Array<ReactTestInstance>;
1012
getAllByText: (text: string | RegExp) => Array<ReactTestInstance>;
1113
getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>;
1214
}
1315

1416
export interface QueryByAPI {
1517
queryByName: (name: React.ReactType) => ReactTestInstance | null;
18+
queryByType: (type: React.ComponentType) => ReactTestInstance | null;
1619
queryByText: (name: string | RegExp) => ReactTestInstance | null;
1720
queryByProps: (props: Record<string, any>) => ReactTestInstance | null;
1821
queryByTestId: (testID: string) => ReactTestInstance | null;
1922
queryAllByName: (name: React.ReactType) => Array<ReactTestInstance> | [];
23+
queryAllByType: (type: React.ComponentType) => Array<ReactTestInstance> | [];
2024
queryAllByText: (text: string | RegExp) => Array<ReactTestInstance> | [];
2125
queryAllByProps: (
2226
props: Record<string, any>

0 commit comments

Comments
 (0)