Skip to content

feat: Prepare for 2.x release #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions docs/Queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,18 @@ const element = getByA11yValue({ min: 40 });

The interface is the same as for other queries, but we won't provide full names so that they're harder to find by search engines.

### `UNSAFE_ByType`, `ByType`
### `UNSAFE_ByType`

> Note: added in v1.4

> This method has been **deprecated** and has been prepended with `UNSAFE_` prefix. In react-native-testing-library 2.x only the prefixed version will work.
> This method has been marked unsafe, since it requires knowledge about implementation details of the component. Use responsibly.

A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.

### `UNSAFE_ByProps`, `ByProps`
### `UNSAFE_ByProps`

> This method has been **deprecated** and has been prepended with `UNSAFE_` prefix. In react-native-testing-library 2.x only the prefixed version will work.
> This method has been marked unsafe, since it requires knowledge about implementation details of the component. Use responsibly.

A method returning a `ReactTestInstance` with matching props object

### `ByName`

> This method has been **deprecated** because using it results in fragile tests that may break between minor React Native versions. **DON'T USE IT**. It will be removed in next major release (v2.0). Use [`getByTestId`](#bytestid) instead. It's listed here only for back-compat purposes for early adopters of the library
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.

</details>
21 changes: 0 additions & 21 deletions src/__tests__/__snapshots__/shallow.test.js.snap

This file was deleted.

4 changes: 2 additions & 2 deletions src/__tests__/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ test('debug.deep', () => {
test('debug.deep async test', async () => {
// $FlowFixMe
console.log = jest.fn();
const { toJSON, getByName } = render(
const { toJSON, getByText } = render(
<Button onPress={jest.fn} text="Press me" />
);

fireEvent.press(getByName('TouchableOpacity'));
fireEvent.press(getByText(/Press me/g));
await flushMicrotasksQueue();

debug.deep(toJSON());
Expand Down
79 changes: 23 additions & 56 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,51 +109,18 @@ test('getAllByTestId, queryAllByTestId', () => {
expect(queryAllByTestId('nonExistentTestId')).toHaveLength(0);
});

test('getByName, queryByName', () => {
const { getByTestId, getByName, queryByName } = render(<Banana />);
const bananaFresh = getByTestId('bananaFresh');
const button = getByName('Button');

button.props.onPress();

expect(bananaFresh.props.children).toBe('fresh');

const sameButton = getByName(Button);
sameButton.props.onPress();

expect(bananaFresh.props.children).toBe('not fresh');
expect(() => getByName('InExistent')).toThrow('No instances found');
expect(() => getByName(Text)).toThrow('Expected 1 but found 6');

expect(queryByName('Button')).toBe(button);
expect(queryByName('InExistent')).toBeNull();
});

test('getAllByName, queryAllByName', () => {
const { getAllByName, queryAllByName } = render(<Banana />);
const [text, status, button] = getAllByName('Text');

expect(text.props.children).toBe('Is the banana fresh?');
expect(status.props.children).toBe('not fresh');
expect(button.props.children).toBe('Change freshness!');
expect(() => getAllByName('InExistent')).toThrow('No instances found');

expect(queryAllByName('Text')[1]).toBe(status);
expect(queryAllByName('InExistent')).toHaveLength(0);
});

test('getAllByType, queryAllByType', () => {
const { getAllByType, queryAllByType } = render(<Banana />);
const [text, status, button] = getAllByType(Text);
test('UNSAFE_getAllByType, UNSAFE_queryAllByType', () => {
const { UNSAFE_getAllByType, UNSAFE_queryAllByType } = render(<Banana />);
const [text, status, button] = UNSAFE_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(() => UNSAFE_getAllByType(InExistent)).toThrow('No instances found');

expect(queryAllByType(Text)[1]).toBe(status);
expect(queryAllByType(InExistent)).toHaveLength(0);
expect(UNSAFE_queryAllByType(Text)[1]).toBe(status);
expect(UNSAFE_queryAllByType(InExistent)).toHaveLength(0);
});

test('getByText, queryByText', () => {
Expand Down Expand Up @@ -267,38 +234,38 @@ test('getAllByDisplayValue, queryAllByDisplayValue', () => {
expect(queryAllByDisplayValue('no value')).toHaveLength(0);
});

test('getByProps, queryByProps', () => {
const { getByProps, queryByProps } = render(<Banana />);
const primaryType = getByProps({ type: 'primary' });
test('UNSAFE_getByProps, UNSAFE_queryByProps', () => {
const { UNSAFE_getByProps, UNSAFE_queryByProps } = render(<Banana />);
const primaryType = UNSAFE_getByProps({ type: 'primary' });

expect(primaryType.props.children).toBe('Change freshness!');
expect(() => getByProps({ type: 'inexistent' })).toThrow(
expect(() => UNSAFE_getByProps({ type: 'inexistent' })).toThrow(
'No instances found'
);

expect(queryByProps({ type: 'primary' })).toBe(primaryType);
expect(queryByProps({ type: 'inexistent' })).toBeNull();
expect(UNSAFE_queryByProps({ type: 'primary' })).toBe(primaryType);
expect(UNSAFE_queryByProps({ type: 'inexistent' })).toBeNull();
});

test('getAllByProp, queryAllByProps', () => {
const { getAllByProps, queryAllByProps } = render(<Banana />);
const primaryTypes = getAllByProps({ type: 'primary' });
test('getAllByProp, UNSAFE_queryAllByProps', () => {
const { UNSAFE_getAllByProps, UNSAFE_queryAllByProps } = render(<Banana />);
const primaryTypes = UNSAFE_getAllByProps({ type: 'primary' });

expect(primaryTypes).toHaveLength(1);
expect(() => getAllByProps({ type: 'inexistent' })).toThrow(
expect(() => UNSAFE_getAllByProps({ type: 'inexistent' })).toThrow(
'No instances found'
);

expect(queryAllByProps({ type: 'primary' })).toEqual(primaryTypes);
expect(queryAllByProps({ type: 'inexistent' })).toHaveLength(0);
expect(UNSAFE_queryAllByProps({ type: 'primary' })).toEqual(primaryTypes);
expect(UNSAFE_queryAllByProps({ type: 'inexistent' })).toHaveLength(0);
});

test('update', () => {
const fn = jest.fn();
const { getByName, update, rerender } = render(<Banana onUpdate={fn} />);
const button = getByName('Button');
const { getByText, update, rerender } = render(<Banana onUpdate={fn} />);
const button = getByText(/Change/g);

button.props.onPress();
fireEvent.press(button);

update(<Banana onUpdate={fn} />);
rerender(<Banana onUpdate={fn} />);
Expand Down Expand Up @@ -345,9 +312,9 @@ test('debug', () => {
test('debug changing component', () => {
jest.spyOn(console, 'log').mockImplementation(x => x);

const { getByProps, debug } = render(<Banana />);
const { UNSAFE_getByProps, debug } = render(<Banana />);

fireEvent.press(getByProps({ type: 'primary' }));
fireEvent.press(UNSAFE_getByProps({ type: 'primary' }));

debug();

Expand Down
27 changes: 0 additions & 27 deletions src/__tests__/shallow.test.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/__tests__/waitForElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class BananaContainer extends React.Component<{}, any> {
}

test('waits for element until it stops throwing', async () => {
const { getByTestId, getByName, queryByTestId } = render(<BananaContainer />);
const { getByTestId, getByText, queryByTestId } = render(<BananaContainer />);

fireEvent.press(getByName('TouchableOpacity'));
fireEvent.press(getByText(/Change/g));

expect(queryByTestId('fresh')).toBeNull();

Expand All @@ -48,9 +48,9 @@ test('waits for element until it stops throwing', async () => {
});

test('waits for element until timeout is met', async () => {
const { getByTestId, getByName } = render(<BananaContainer />);
const { getByTestId, getByText } = render(<BananaContainer />);

fireEvent.press(getByName('TouchableOpacity'));
fireEvent.press(getByText(/Change/g));

await expect(
waitForElement(() => getByTestId('fresh'), 100)
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/debugShallow.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import * as React from 'react';
import { shallowInternal } from '../shallow';
import shallow from '../shallow';
import format from './format';

/**
Expand All @@ -10,7 +10,7 @@ export default function debugShallow(
instance: ReactTestInstance | React.Element<any>,
message?: any
) {
const { output } = shallowInternal(instance);
const { output } = shallow(instance);

if (message) {
console.log(`${message}\n\n`, format(output));
Expand Down
42 changes: 12 additions & 30 deletions src/helpers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,26 @@ export const createQueryByError = (error: Error, callsite: Function) => {
};

const warned = {
getByName: false,
getAllByName: false,
queryByName: false,
queryAllByName: false,

getByProps: false,
getAllByProps: false,
queryByProps: false,
queryAllByProps: false,

getByType: false,
getAllByType: false,
queryByType: false,
queryAllByType: false,
UNSAFE_getByProps: false,
UNSAFE_getAllByProps: false,
UNSAFE_queryByProps: false,
UNSAFE_queryAllByProps: false,

UNSAFE_getByType: false,
UNSAFE_getAllByType: false,
UNSAFE_queryByType: false,
UNSAFE_queryAllByType: false,
};

export function printDeprecationWarning(functionName: string) {
if (warned[functionName]) {
return;
}

console.warn(`
Deprecation Warning:
${functionName} is not recommended for use and will be deleted in react-native-testing-library 2.x.
`);

warned[functionName] = true;
}

export function printUnsafeWarning(functionName: string) {
if (warned[functionName]) {
return;
}

console.warn(`
Deprecation Warning:
${functionName} is not recommended for use and has been renamed to UNSAFE_${functionName}.
In react-native-testing-library 2.x only the UNSAFE_${functionName} name will work.
Warning:
${functionName} promotes testing implementation details and is not recommended.
It may be removed in the future. Please test observable outcomes of rendering components.
`);

warned[functionName] = true;
Expand Down
44 changes: 4 additions & 40 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ import {
ErrorWithStack,
createLibraryNotSupportedError,
prepareErrorMessage,
printDeprecationWarning,
printUnsafeWarning,
} from './errors';

const filterNodeByType = (node, type) => node.type === type;

const filterNodeByName = (node, name) =>
typeof node.type !== 'string' &&
(node.type.displayName === name || node.type.name === name);

const getNodeByText = (node, text) => {
try {
// eslint-disable-next-line
Expand Down Expand Up @@ -69,18 +64,6 @@ const getTextInputNodeByDisplayValue = (node, value) => {
}
};

export const getByName = (instance: ReactTestInstance, warnFn?: Function) =>
function getByNameFn(name: string | React.ComponentType<any>) {
warnFn && warnFn('getByName');
try {
return typeof name === 'string'
? instance.find(node => filterNodeByName(node, name))
: instance.findByType(name);
} catch (error) {
throw new ErrorWithStack(prepareErrorMessage(error), getByNameFn);
}
};

export const getByType = (instance: ReactTestInstance, warnFn?: Function) =>
function getByTypeFn(type: React.ComponentType<any>) {
warnFn && warnFn('getByType');
Expand Down Expand Up @@ -141,19 +124,6 @@ export const getByTestId = (instance: ReactTestInstance) =>
}
};

export const getAllByName = (instance: ReactTestInstance, warnFn?: Function) =>
function getAllByNameFn(name: string | React.ComponentType<any>) {
warnFn && warnFn('getAllByName');
const results =
typeof name === 'string'
? instance.findAll(node => filterNodeByName(node, name))
: instance.findAllByType(name);
if (results.length === 0) {
throw new ErrorWithStack('No instances found', getAllByNameFn);
}
return results;
};

export const getAllByType = (instance: ReactTestInstance, warnFn?: Function) =>
function getAllByTypeFn(type: React.ComponentType<any>) {
warnFn && warnFn('getAllByType');
Expand Down Expand Up @@ -234,23 +204,17 @@ export const getAllByTestId = (instance: ReactTestInstance) =>

export const getByAPI = (instance: ReactTestInstance) => ({
getByTestId: getByTestId(instance),
getByName: getByName(instance, printDeprecationWarning),
getByType: getByType(instance, printUnsafeWarning),
getByText: getByText(instance),
getByPlaceholder: getByPlaceholder(instance),
getByDisplayValue: getByDisplayValue(instance),
getByProps: getByProps(instance, printUnsafeWarning),
getAllByTestId: getAllByTestId(instance),
getAllByName: getAllByName(instance, printDeprecationWarning),
getAllByType: getAllByType(instance, printUnsafeWarning),
getAllByText: getAllByText(instance),
getAllByPlaceholder: getAllByPlaceholder(instance),
getAllByDisplayValue: getAllByDisplayValue(instance),
getAllByProps: getAllByProps(instance, printUnsafeWarning),

// Unsafe aliases
UNSAFE_getByType: getByType(instance),
UNSAFE_getAllByType: getAllByType(instance),
UNSAFE_getByProps: getByProps(instance),
UNSAFE_getAllByProps: getAllByProps(instance),
UNSAFE_getByType: getByType(instance, printUnsafeWarning),
UNSAFE_getAllByType: getAllByType(instance, printUnsafeWarning),
UNSAFE_getByProps: getByProps(instance, printUnsafeWarning),
UNSAFE_getAllByProps: getAllByProps(instance, printUnsafeWarning),
});
Loading