Skip to content

chore(breaking): Remove deprecated APIs #179

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 3 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
4 changes: 2 additions & 2 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function setAnswer(question, answer) {
}

test('should verify two questions', () => {
const { getAllByType, getByText } = render(<QuestionsBoard {...props} />);
const allQuestions = getAllByType(Question);
const { getAllByText, getByText } = render(<QuestionsBoard {...props} />);
const allQuestions = getAllByText('Question?');

setAnswer(allQuestions[0], 'a1');
setAnswer(allQuestions[1], 'a2');
Expand Down
29 changes: 0 additions & 29 deletions docs/Queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,32 +139,3 @@ import { render } from 'react-native-testing-library';
const { getByA11yRole } = render(<MyComponent />);
const element = getByA11yRole('button');
```

## Unit testing helpers

> Use sparingly and responsibly, escape hatches here

`render` from `react-native-testing-library` exposes additional queries that **should not be used in component integration testing**, but some users (like component library creators) interested in unit testing some components may find helpful.

<details>
<summary>Queries helpful in unit testing</summary>

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.

### `ByType`

> Note: added in v1.4

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

### `ByProps`

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>
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ exports[`debug: shallow 1`] = `
/>
<Button
onPress={[Function anonymous]}
testID=\\"changeFreshness\\"
type=\\"primary\\"
>
Change freshness!
Expand Down Expand Up @@ -153,6 +154,7 @@ exports[`debug: shallow with message 1`] = `
/>
<Button
onPress={[Function anonymous]}
testID=\\"changeFreshness\\"
type=\\"primary\\"
>
Change freshness!
Expand Down
25 changes: 0 additions & 25 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 @@ -72,11 +72,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
87 changes: 9 additions & 78 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class Banana extends React.Component<*, *> {
placeholder={PLACEHOLDER_FRESHNESS}
/>
<TextInput testID="bananaChef" placeholder={PLACEHOLDER_CHEF} />
<Button onPress={this.changeFresh} type="primary">
<Button
testID="changeFreshness"
onPress={this.changeFresh}
type="primary"
>
Change freshness!
</Button>
</View>
Expand All @@ -72,53 +76,6 @@ test('getByTestId, queryByTestId', () => {
expect(queryByTestId('InExistent')).toBeNull();
});

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 3');

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);
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);
Expand Down Expand Up @@ -200,36 +157,10 @@ test('getAllByPlaceholder, queryAllByPlaceholder', () => {
expect(queryAllByPlaceholder('no placeholder')).toHaveLength(0);
});

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

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

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

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

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

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

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

button.props.onPress();

Expand Down Expand Up @@ -278,9 +209,9 @@ test('debug', () => {
test('debug changing component', () => {
jest.spyOn(console, 'log').mockImplementation(x => x);

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

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

debug();

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

This file was deleted.

10 changes: 5 additions & 5 deletions src/__tests__/waitForElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Banana extends React.Component<*, *> {
return (
<View>
{this.props.fresh && <Text testID="fresh">Fresh</Text>}
<TouchableOpacity onPress={this.changeFresh}>
<TouchableOpacity testID="changeFreshness" onPress={this.changeFresh}>
<Text>Change freshness!</Text>
</TouchableOpacity>
</View>
Expand All @@ -37,9 +37,9 @@ class BananaContainer extends React.Component<*, *> {
}

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

fireEvent.press(getByName('TouchableOpacity'));
fireEvent.press(getByTestId('changeFreshness'));

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

Expand All @@ -49,9 +49,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 } = render(<BananaContainer />);

fireEvent.press(getByName('TouchableOpacity'));
fireEvent.press(getByTestId('changeFreshness'));

await expect(
waitForElement(() => getByTestId('fresh'), 100)
Expand Down
Loading