mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
feat: add "cleanup" function (#237)
* feat: add "cleanup" function * docs: add "cleanup" section * test: "cleanup" function * don't expose cleanup queue Co-authored-by: Alec Larson <alec.stanford.larson@gmail.com> Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
This commit is contained in:
committed by
Kacper Wiszczuk
parent
69d148023f
commit
4c77f4a7dc
@@ -0,0 +1,27 @@
|
||||
// @flow
|
||||
/* eslint-disable react/no-multi-comp */
|
||||
import React from 'react';
|
||||
import { View } from 'react-native';
|
||||
import { cleanup, render } from '..';
|
||||
|
||||
class Test extends React.Component<*> {
|
||||
componentWillUnmount() {
|
||||
if (this.props.onUnmount) {
|
||||
this.props.onUnmount();
|
||||
}
|
||||
}
|
||||
render() {
|
||||
return <View />;
|
||||
}
|
||||
}
|
||||
|
||||
test('cleanup', () => {
|
||||
const fn = jest.fn();
|
||||
|
||||
render(<Test onUnmount={fn} />);
|
||||
render(<Test onUnmount={fn} />);
|
||||
expect(fn).not.toHaveBeenCalled();
|
||||
|
||||
cleanup();
|
||||
expect(fn).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// @flow
|
||||
let cleanupQueue = new Set();
|
||||
|
||||
export default function cleanup() {
|
||||
cleanupQueue.forEach(fn => fn());
|
||||
cleanupQueue.clear();
|
||||
}
|
||||
|
||||
export function addToCleanupQueue(
|
||||
fn: (nextElement?: React$Element<any>) => void
|
||||
) {
|
||||
cleanupQueue.add(fn);
|
||||
}
|
||||
+9
-7
@@ -1,16 +1,18 @@
|
||||
// @flow
|
||||
import act from './act';
|
||||
import render from './render';
|
||||
import shallow from './shallow';
|
||||
import flushMicrotasksQueue from './flushMicrotasksQueue';
|
||||
import cleanup from './cleanup';
|
||||
import debug from './debug';
|
||||
import fireEvent from './fireEvent';
|
||||
import flushMicrotasksQueue from './flushMicrotasksQueue';
|
||||
import render from './render';
|
||||
import shallow from './shallow';
|
||||
import waitForElement from './waitForElement';
|
||||
|
||||
export { render };
|
||||
export { shallow };
|
||||
export { flushMicrotasksQueue };
|
||||
export { act };
|
||||
export { cleanup };
|
||||
export { debug };
|
||||
export { fireEvent };
|
||||
export { flushMicrotasksQueue };
|
||||
export { render };
|
||||
export { shallow };
|
||||
export { waitForElement };
|
||||
export { act };
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import * as React from 'react';
|
||||
import TestRenderer, { type ReactTestRenderer } from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies
|
||||
import act from './act';
|
||||
import { addToCleanupQueue } from './cleanup';
|
||||
import { getByAPI } from './helpers/getByAPI';
|
||||
import { queryByAPI } from './helpers/queryByAPI';
|
||||
import a11yAPI from './helpers/a11yAPI';
|
||||
@@ -34,6 +35,8 @@ export default function render<T>(
|
||||
const update = updateWithAct(renderer, wrap);
|
||||
const instance = renderer.root;
|
||||
|
||||
addToCleanupQueue(renderer.unmount);
|
||||
|
||||
return {
|
||||
...getByAPI(instance),
|
||||
...queryByAPI(instance),
|
||||
|
||||
Reference in New Issue
Block a user