Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
34 changes: 34 additions & 0 deletions src/__tests__/auto-cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { View } from 'react-native';
import { render } from '..';

let isMounted = false;

class Test extends React.Component<*> {
componentDidMount() {
isMounted = true;
}

componentWillUnmount() {
isMounted = false;
if (this.props.onUnmount) {
this.props.onUnmount();
}
}
render() {
return <View />;
}
}

// This just verifies that by importing RNTL in an
// environment which supports afterEach (like jest)
// we'll get automatic cleanup between tests.
test('first', () => {
const fn = jest.fn();
render(<Test onUnmount={fn} />);
expect(fn).not.toHaveBeenCalled();
});

test('second', () => {
expect(isMounted).toEqual(false);
});
2 changes: 1 addition & 1 deletion src/__tests__/cleanup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable react/no-multi-comp */
import React from 'react';
import { View } from 'react-native';
import { cleanup, render } from '..';
import { cleanup, render } from '../pure';

class Test extends React.Component<*> {
componentWillUnmount() {
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/waitFor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ test('waits for element until timeout is met', async () => {
await expect(
waitFor(() => getByText('Fresh'), { timeout: 100 })
).rejects.toThrow();

await waitFor(() => getByText('Fresh'));
});

test('waits for element with custom interval', async () => {
Expand Down
31 changes: 15 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// @flow
import act from './act';
import cleanup from './cleanup';
import fireEvent from './fireEvent';
import flushMicrotasksQueue from './flushMicrotasksQueue';
import render from './render';
import shallow from './shallow';
import waitFor, { waitForElement } from './waitFor';
import within from './within';
import { cleanup } from './pure';

export { act };
export { cleanup };
export { fireEvent };
export { flushMicrotasksQueue };
export { render };
export { shallow };
export { waitFor, waitForElement };
export { within };
// If we're running in a test runner that supports afterEach
// then we'll automatically run cleanup afterEach test
// this ensures that tests run in isolation from each other
// if you don't like this then either import the `pure` module
// or set the RNTL_SKIP_AUTO_CLEANUP env variable to 'true'.
if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) {
// eslint-disable-next-line no-undef
afterEach(async () => {
await flushMicrotasksQueue();
cleanup();
});
}

export * from './pure';
18 changes: 18 additions & 0 deletions src/pure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @flow
import act from './act';
import cleanup from './cleanup';
import fireEvent from './fireEvent';
import flushMicrotasksQueue from './flushMicrotasksQueue';
import render from './render';
import shallow from './shallow';
import waitFor, { waitForElement } from './waitFor';
import within from './within';

export { act };
export { cleanup };
export { fireEvent };
export { flushMicrotasksQueue };
export { render };
export { shallow };
export { waitFor, waitForElement };
export { within };
2 changes: 2 additions & 0 deletions website/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const cleanup: () => void;

Unmounts React trees that were mounted with `render`.

> Please note that this is done automatically if the testing framework you're using supports the `afterEach` global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.

For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:

```jsx
Expand Down