Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

feat: add a jest preset for more confidence #10

Merged
merged 1 commit into from
Apr 28, 2019
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
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules
coverage
dist
.idea
node_modules/
coverage/
dist/
.idea/
.DS_Store

yarn-error.log
Expand Down
4 changes: 2 additions & 2 deletions examples/__tests__/input-event.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Text, TextInput, View } from 'react-native';
import { render, fireEvent } from 'native-testing-library';
import { TextInput } from 'react-native';
import { render, fireEvent } from '../../src';

class CostInput extends React.Component {
state = {
Expand Down
2 changes: 1 addition & 1 deletion examples/__tests__/react-context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'jest-native/extend-expect';
import React from 'react';
import { Text } from 'react-native';
import { render } from 'native-testing-library';
import { render } from '../../src';

import { NameContext, NameProvider, NameConsumer } from '../react-context';

Expand Down
2 changes: 1 addition & 1 deletion examples/__tests__/react-intl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FormattedDate } from 'react-intl-native';
import IntlPolyfill from 'intl';
import 'intl/locale-data/jsonp/pt';

import { getByText, render } from 'native-testing-library';
import { getByText, render } from '../../src';

const setupTests = () => {
if (global.Intl) {
Expand Down
27 changes: 4 additions & 23 deletions examples/__tests__/react-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { Button, Text, View } from 'react-native';
import { createStackNavigator, createAppContainer, withNavigation } from 'react-navigation';

import { render, fireEvent } from 'native-testing-library';
import { render, fireEvent } from '../../src';

jest.mock('NativeAnimatedHelper').mock('react-native-gesture-handler', () => {
const View = require('react-native').View;
Expand All @@ -15,25 +15,6 @@ jest.mock('NativeAnimatedHelper').mock('react-native-gesture-handler', () => {
};
});

const originalConsoleWarn = console.warn;
console.warn = arg => {
const warnings = [
'Calling .measureInWindow()',
'Calling .measureLayout()',
'Calling .setNativeProps()',
'Calling .focus()',
'Calling .blur()',
];

const finalArgs = warnings.reduce((acc, curr) => (arg.includes(curr) ? [...acc, arg] : acc), []);

if (finalArgs.length) {
return;
}

originalConsoleWarn(message);
};

const Home = ({ navigation }) => (
<View>
<Text testID="title">Home page</Text>
Expand Down Expand Up @@ -70,14 +51,14 @@ function renderWithNavigation({ screens = {}, navigatorConfig = {} } = {}) {

const App = createAppContainer(AppNavigator);

return { ...render(<App />), navigationContainer: App };
return { ...render(<App detached />), navigationContainer: App };
}

test('full app rendering/navigating', async () => {
const { findByText, getByTestId, getByText } = renderWithNavigation();
const { findByText, getByTestId, getByTitle } = renderWithNavigation();

expect(getByTestId('title')).toHaveTextContent('Home page');
fireEvent.press(getByText(/Go to about/i));
fireEvent.press(getByTitle(/Go to about/i));

const result = await findByText('About page');
expect(result).toHaveTextContent('About page');
Expand Down
16 changes: 8 additions & 8 deletions examples/__tests__/react-redux.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { createStore } from 'redux';
import { Provider, connect } from 'react-redux';
import { Button, Text, View } from 'react-native';
import { render, fireEvent } from 'native-testing-library';
import { render, fireEvent } from '../../src';

class Counter extends React.Component {
increment = () => {
Expand Down Expand Up @@ -53,26 +53,26 @@ function renderWithRedux(ui, { initialState, store = createStore(reducer, initia
}

test('can render with redux with defaults', () => {
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />);
fireEvent.press(getByText('+'));
const { getByTestId, getByTitle } = renderWithRedux(<ConnectedCounter />);
fireEvent.press(getByTitle('+'));
expect(getByTestId('count-value')).toHaveTextContent(1);
});

test('can render with redux with custom initial state', () => {
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />, {
const { getByTestId, getByTitle } = renderWithRedux(<ConnectedCounter />, {
initialState: { count: 3 },
});
fireEvent.press(getByText('-'));
fireEvent.press(getByTitle('-'));
expect(getByTestId('count-value')).toHaveTextContent(2);
});

test('can render with redux with custom store', () => {
const store = createStore(() => ({ count: 1000 }));
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />, {
const { getByTestId, getByTitle } = renderWithRedux(<ConnectedCounter />, {
store,
});
fireEvent.press(getByText('+'));
fireEvent.press(getByTitle('+'));
expect(getByTestId('count-value')).toHaveTextContent(1000);
fireEvent.press(getByText('-'));
fireEvent.press(getByTitle('-'));
expect(getByTestId('count-value')).toHaveTextContent(1000);
});
2 changes: 1 addition & 1 deletion examples/__tests__/update-props.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import 'jest-native/extend-expect';
import { Text, View } from 'react-native';
import { render } from 'native-testing-library';
import { render } from '../../src';

let idCounter = 1;

Expand Down
21 changes: 21 additions & 0 deletions examples/__tests__/use-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useState } from 'react';
import { renderHook, act } from 'react-hooks-testing-library';

describe('useState tests', () => {
test('should use setState value', () => {
const { result } = renderHook(() => useState('foo'));
const [value] = result.current;

expect(value).toBe('foo');
});

test('should update setState value using setter', () => {
const { result } = renderHook(() => useState('foo'));
const [_, setValue] = result.current;

act(() => setValue('bar'));

const [value] = result.current;
expect(value).toBe('bar');
});
});
9 changes: 0 additions & 9 deletions examples/jest.config.js

This file was deleted.

6 changes: 6 additions & 0 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const jestPreset = require('react-native/jest-preset');

module.exports = Object.assign(jestPreset, {
transformIgnorePatterns: ['node_modules/(?!(react-native.*|@?react-navigation.*)/)'],
setupFiles: [...jestPreset.setupFiles, require.resolve('./src/preset/setup.js')],
});
11 changes: 5 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const jestPreset = require('./jest-preset');

const ignores = ['/node_modules/', '/__tests__/helpers/', '__mocks__'];

module.exports = {
preset: 'react-native',
transformIgnorePatterns: ['node_modules/(?!(react-native.*|@?react-navigation.*)/)'],
collectCoverageFrom: ['src/**/*.+(js|jsx|ts|tsx)'],
testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'],
module.exports = Object.assign(jestPreset, {
collectCoverageFrom: ['src/lib/**/*.+(js|jsx|ts|tsx)'],
testPathIgnorePatterns: [...ignores],
coverageThreshold: {
global: {
Expand All @@ -14,4 +13,4 @@ module.exports = {
statements: 100,
},
},
};
});
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/**"
"dist/**",
"typings",
"jest-preset.js"
],
"engines": {
"node": ">=8"
Expand All @@ -16,8 +18,8 @@
"commit:all": "npm run commit:add && npm run commit",
"readme:toc": "doctoc README.md --maxlevel 3 --title '## Table of Contents'",
"test": "jest",
"pretty-quick": "pretty-quick --staged --pattern '**/*.(js|jsx|ts|tsx)'",
"prepublish": "rm -rf dist; babel src --out-dir dist --ignore 'src/__tests__/*' && cp src/index.d.ts dist/index.d.ts",
"pretty-quick": "pretty-quick --staged",
"prepublishOnly": "rm -rf dist; babel src --out-dir dist --ignore 'src/**/__tests__/*'",
"semantic-release": "semantic-release",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch --coverage"
Expand All @@ -35,6 +37,7 @@
"author": "Brandon Carroll <brandonvcarroll@gmail.com> (https://github.com/bcarroll22)",
"license": "MIT",
"dependencies": {
"jest-native": "2.0.0-alpha.5",
"pretty-format": "^24.5.0",
"react-test-renderer": "^16.8.5",
"wait-for-expect": "^1.1.1"
Expand All @@ -50,18 +53,17 @@
"jest": "24.5.0",
"jest-fetch-mock": "^2.1.1",
"jest-in-case": "^1.0.2",
"jest-native": "^1.2.0",
"metro-react-native-babel-preset": "^0.52.0",
"prettier": "^1.16.4",
"pretty-quick": "^1.10.0",
"react": "^16.8.5",
"react-hooks-testing-library": "^0.5.0",
"react-intl": "^2.8.0",
"react-intl-native": "^2.1.2",
"react-native": "^0.59.0",
"react-native-gesture-handler": "^1.1.0",
"react-navigation": "^3.5.1",
"react-redux": "6.0.1",
"react-test-renderer-tree-to-json": "^1.0.1",
"redux": "^4.0.0",
"semantic-release": "^15.13.3"
},
Expand Down
Empty file removed setup.js
Empty file.
28 changes: 0 additions & 28 deletions src/__tests__/__snapshots__/fetch.js.snap

This file was deleted.

21 changes: 0 additions & 21 deletions src/__tests__/filter.js

This file was deleted.

59 changes: 0 additions & 59 deletions src/__tests__/hooks/async-hook.js

This file was deleted.

Loading