feat: add getByType helper (#64)

* feat: add getByType helper

* add docs
This commit is contained in:
Michał Pierzchała
2018-11-19 13:44:51 +01:00
committed by GitHub
parent 235942bee3
commit fa198c4037
6 changed files with 74 additions and 0 deletions
+14
View File
@@ -96,6 +96,20 @@ test('getAllByName, queryAllByName', () => {
expect(queryAllByName('InExistent')).toHaveLength(0);
});
test('getAllByType, queryAllByType', () => {
const { getAllByType, queryAllByType } = render(<Banana />);
const [text, status, button] = getAllByType(Text);
const InExistent = () => null;
expect(text.props.children).toBe('Is the banana fresh?');
expect(status.props.children).toBe('not fresh');
expect(button.props.children).toBe('Change freshness!');
expect(() => getAllByType(InExistent)).toThrow('No instances found');
expect(queryAllByType(Text)[1]).toBe(status);
expect(queryAllByType(InExistent)).toHaveLength(0);
});
test('getByText, queryByText', () => {
const { getByText, queryByText } = render(<Banana />);
const button = getByText(/change/i);
+20
View File
@@ -40,6 +40,15 @@ export const getByName = (instance: ReactTestInstance) =>
}
};
export const getByType = (instance: ReactTestInstance) =>
function getByTypeFn(type: React.ComponentType<*>) {
try {
return instance.findByType(type);
} catch (error) {
throw new ErrorWithStack(prepareErrorMessage(error), getByTypeFn);
}
};
export const getByText = (instance: ReactTestInstance) =>
function getByTextFn(text: string | RegExp) {
try {
@@ -80,6 +89,15 @@ export const getAllByName = (instance: ReactTestInstance) =>
return results;
};
export const getAllByType = (instance: ReactTestInstance) =>
function getAllByTypeFn(type: React.ComponentType<*>) {
const results = instance.findAllByType(type);
if (results.length === 0) {
throw new ErrorWithStack('No instances found', getAllByTypeFn);
}
return results;
};
export const getAllByText = (instance: ReactTestInstance) =>
function getAllByTextFn(text: string | RegExp) {
const results = instance.findAll(node => getNodeByText(node, text));
@@ -107,9 +125,11 @@ export const getAllByProps = (instance: ReactTestInstance) =>
export const getByAPI = (instance: ReactTestInstance) => ({
getByTestId: getByTestId(instance),
getByName: getByName(instance),
getByType: getByType(instance),
getByText: getByText(instance),
getByProps: getByProps(instance),
getAllByName: getAllByName(instance),
getAllByType: getAllByType(instance),
getAllByText: getAllByText(instance),
getAllByProps: getAllByProps(instance),
});
+24
View File
@@ -3,9 +3,11 @@ import * as React from 'react';
import {
getByTestId,
getByName,
getByType,
getByText,
getByProps,
getAllByName,
getAllByType,
getAllByText,
getAllByProps,
} from './getByAPI';
@@ -20,6 +22,16 @@ export const queryByName = (instance: ReactTestInstance) => (
}
};
export const queryByType = (instance: ReactTestInstance) => (
type: React.ComponentType<*>
) => {
try {
return getByType(instance)(type);
} catch (error) {
return null;
}
};
export const queryByText = (instance: ReactTestInstance) => (
text: string | RegExp
) => {
@@ -60,6 +72,16 @@ export const queryAllByName = (instance: ReactTestInstance) => (
}
};
export const queryAllByType = (instance: ReactTestInstance) => (
type: React.ComponentType<*>
) => {
try {
return getAllByType(instance)(type);
} catch (error) {
return [];
}
};
export const queryAllByText = (instance: ReactTestInstance) => (
text: string | RegExp
) => {
@@ -83,9 +105,11 @@ export const queryAllByProps = (instance: ReactTestInstance) => (props: {
export const queryByAPI = (instance: ReactTestInstance) => ({
queryByTestId: queryByTestId(instance),
queryByName: queryByName(instance),
queryByType: queryByType(instance),
queryByText: queryByText(instance),
queryByProps: queryByProps(instance),
queryAllByName: queryAllByName(instance),
queryAllByType: queryAllByType(instance),
queryAllByText: queryAllByText(instance),
queryAllByProps: queryAllByProps(instance),
});