Skip to content

feat: Breaking changes for v2 #350

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

Merged
merged 16 commits into from
May 28, 2020
Merged
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ The [public API](https://callstack.github.io/react-native-testing-library/docs/a

- [`render`](https://callstack.github.io/react-native-testing-library/docs/api#render) – deeply renders given React element and returns helpers to query the output components.
- [`fireEvent`](https://callstack.github.io/react-native-testing-library/docs/api#fireevent) - invokes named event handler on the element.
- [`waitForElement`](https://callstack.github.io/react-native-testing-library/docs/api#waitforelement) - waits for non-deterministic periods of time until your element appears or times out.
- [`waitFor`](https://callstack.github.io/react-native-testing-library/docs/api#waitfor) - waits for non-deterministic periods of time until your element appears or times out.
- [`within`](https://callstack.github.io/react-native-testing-library/docs/api#within) - creates a queries object scoped for given element.
- [`flushMicrotasksQueue`](https://callstack.github.io/react-native-testing-library/docs/api#flushmicrotasksqueue) - waits for microtasks queue to flush.

**Note to users who are more familiar with `react-testing-library`:** This API does not expose `cleanup` because it doesn't interact with the DOM. There's nothing to clean up.
## Migration Guides

- [Migration to 2.0](https://callstack.github.io/react-native-testing-library/docs/migration-v2)

## Made with ❤️ at Callstack

Expand Down
15 changes: 14 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
presets: [
'@babel/preset-flow',
'@babel/preset-react',
[
'@babel/preset-env',
{
targets: {
node: '10',
},
bugfixes: true,
},
],
],
plugins: ['@babel/plugin-proposal-class-properties'],
};
1 change: 1 addition & 0 deletions dont-cleanup-after-each.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.RNTL_SKIP_AUTO_CLEANUP = true;
4 changes: 1 addition & 3 deletions examples/reactnavigation/src/__tests__/AppNavigator.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { render, fireEvent, cleanup } from 'react-native-testing-library';
import { render, fireEvent } from 'react-native-testing-library';

import AppNavigator from '../AppNavigator';

// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

describe('Testing react navigation', () => {
afterEach(cleanup);

test('page contains the header and 10 items', () => {
const component = (
<NavigationContainer>
Expand Down
4 changes: 1 addition & 3 deletions examples/redux/components/AddTodo.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React from 'react';
import { Provider } from 'react-redux';
import { cleanup, fireEvent, render } from 'react-native-testing-library';
import { fireEvent, render } from 'react-native-testing-library';
import configureStore from '../store';
import AddTodo from './AddTodo';

describe('Application test', () => {
afterEach(cleanup);

test('adds a new test when entry has been included', () => {
const store = configureStore();

Expand Down
4 changes: 1 addition & 3 deletions examples/redux/components/TodoList.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React from 'react';
import { Provider } from 'react-redux';
import { cleanup, fireEvent, render } from 'react-native-testing-library';
import { fireEvent, render } from 'react-native-testing-library';
import configureStore from '../store';
import TodoList from './TodoList';

describe('Application test', () => {
afterEach(cleanup);

test('it should execute with a store with 4 elements', () => {
const initialState = {
todos: [
Expand Down
1 change: 0 additions & 1 deletion examples/redux/reducers/todoReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default function todoReducer(state = [], action) {
return state.concat(action.payload);

case actions.REMOVE:
console.log(action);
return state.filter((todo) => todo.id !== action.payload.id);

case actions.MODIFY:
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-testing-library",
"version": "1.14.0",
"version": "2.0.0-rc.1",
"description": "Simple React Native testing utilities helping you write better tests with less effort",
"main": "build/index.js",
"typings": "./typings/index.d.ts",
Expand All @@ -21,6 +21,10 @@
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/preset-env": "^7.9.6",
"@babel/preset-flow": "^7.9.0",
"@babel/preset-react": "^7.9.4",
"@callstack/eslint-config": "^10.0.0",
"@release-it/conventional-changelog": "^1.1.0",
"@testing-library/jest-native": "~3.1.0",
Expand All @@ -34,7 +38,6 @@
"flow-bin": "^0.122.0",
"flow-copy-source": "^2.0.9",
"jest": "^26.0.1",
"metro-react-native-babel-preset": "^0.58.0",
"react": "^16.13.1",
"react-native": "^0.63.0-rc.1",
"react-test-renderer": "^16.13.1",
Expand Down
2 changes: 2 additions & 0 deletions pure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// makes it so people can import from 'react-native-testing-library/pure'
module.exports = require('./build/pure');
57 changes: 0 additions & 57 deletions src/__tests__/__snapshots__/debug.test.js.snap

This file was deleted.

8 changes: 4 additions & 4 deletions src/__tests__/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ exports[`debug: shallow 1`] = `
underlineColorAndroid=\\"transparent\\"
value=\\"I inspected freshie\\"
/>
<Button
<MyButton
onPress={[Function anonymous]}
type=\\"primary\\"
>
Change freshness!
</Button>
</MyButton>
<Text
testID=\\"duplicateText\\"
>
Expand Down Expand Up @@ -200,12 +200,12 @@ exports[`debug: shallow with message 1`] = `
underlineColorAndroid=\\"transparent\\"
value=\\"I inspected freshie\\"
/>
<Button
<MyButton
onPress={[Function anonymous]}
type=\\"primary\\"
>
Change freshness!
</Button>
</MyButton>
<Text
testID=\\"duplicateText\\"
>
Expand Down
21 changes: 0 additions & 21 deletions src/__tests__/__snapshots__/shallow.test.js.snap

This file was deleted.

39 changes: 39 additions & 0 deletions src/__tests__/auto-cleanup-skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { View } from 'react-native';

let render;
beforeAll(() => {
process.env.RNTL_SKIP_AUTO_CLEANUP = 'true';
const rtl = require('../');
render = rtl.render;
});

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 pure mode in an environment which supports
// afterEach (like jest) we won't get automatic cleanup between tests.
test('component is mounted, but not umounted before test ends', () => {
const fn = jest.fn();
render(<Test onUnmount={fn} />);
expect(fn).not.toHaveBeenCalled();
});

test('component is NOT automatically umounted after first test ends', () => {
expect(isMounted).toEqual(true);
});
33 changes: 33 additions & 0 deletions src/__tests__/auto-cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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('component is mounted, but not umounted before test ends', () => {
const fn = jest.fn();
render(<Test onUnmount={fn} />);
expect(fn).not.toHaveBeenCalled();
});

test('component is automatically umounted after first test ends', () => {
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
Loading