Skip to content

Commit

Permalink
feat: jest preset for more confidence 👍
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Carroll committed Apr 28, 2019
1 parent a283e57 commit 1e36898
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 27 deletions.
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const ignores = ['/node_modules/', '/__tests__/helpers/', '__mocks__'];

module.exports = {
preset: 'react-native',
transformIgnorePatterns: ['node_modules/(?!(react-native.*|@?react-navigation.*)/)'],
preset: 'native-testing-library',
setupFilesAfterEnv: ['<rootDir>/setup-tests.js'],
collectCoverageFrom: ['src/**/*.+(js|jsx|ts|tsx)'],
testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'],
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@
"husky": "^1.3.1",
"jest": "^24.7.1",
"metro-react-native-babel-preset": "^0.52.0",
"native-testing-library": "1.x",
"native-testing-library": "^3.0.0-beta.1",
"prettier": "^1.16.4",
"pretty-quick": "^1.10.0",
"react": "16.8.3",
"react-native": "^0.59.3",
"semantic-release": "^15.13.3"
},
"peerDependencies": {
"native-testing-library": ">=1.2.0",
"react": "*",
"react-native": "*"
"react-native": ">0.55"
},
"husky": {
"hooks": {
Expand Down
1 change: 1 addition & 0 deletions setup-tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { plugins } from 'pretty-format';

import './src/extend-expect';

expect.addSnapshotSerializer(plugins.ConvertAnsi);
22 changes: 9 additions & 13 deletions src/__tests__/to-be-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { render } from 'native-testing-library';

test('.toBeDisabled', () => {
const { queryByTestId, queryByText } = render(
const { queryByTestId, queryByText, queryByTitle } = render(
<View disabled testID="view">
<Button disabled testID="button" title="button" />
<TouchableHighlight disabled testID="highlight">
Expand All @@ -28,10 +28,8 @@ test('.toBeDisabled', () => {
expect(queryByTestId('view')).toBeDisabled();
expect(() => expect(queryByTestId('view')).not.toBeDisabled()).toThrowError();

expect(queryByTestId('button')).toBeDisabled();
expect(queryByText('button')).toBeDisabled();
expect(() => expect(queryByTestId('button')).not.toBeDisabled()).toThrowError();
expect(() => expect(queryByText('button')).not.toBeDisabled()).toThrowError();
expect(queryByTitle('button')).toBeDisabled();
expect(() => expect(queryByTitle('button')).not.toBeDisabled()).toThrowError();

expect(queryByTestId('highlight')).toBeDisabled();
expect(queryByText('highlight')).toBeDisabled();
Expand All @@ -50,9 +48,9 @@ test('.toBeDisabled', () => {
});

test('.toBeEnabled', () => {
const { queryByTestId, queryByText } = render(
const { queryByTestId, queryByText, queryByTitle } = render(
<View testID="view">
<Button testID="button" title="button" />
<Button title="button" />
<TouchableHighlight testID="highlight">
<Text>highlight</Text>
</TouchableHighlight>
Expand All @@ -68,10 +66,8 @@ test('.toBeEnabled', () => {
expect(queryByTestId('view')).toBeEnabled();
expect(() => expect(queryByTestId('view')).not.toBeEnabled()).toThrowError();

expect(queryByTestId('button')).toBeEnabled();
expect(queryByText('button')).toBeEnabled();
expect(() => expect(queryByTestId('button')).not.toBeEnabled()).toThrowError();
expect(() => expect(queryByText('button')).not.toBeEnabled()).toThrowError();
expect(queryByTitle('button')).toBeEnabled();
expect(() => expect(queryByTitle('button')).not.toBeEnabled()).toThrowError();

expect(queryByTestId('highlight')).toBeEnabled();
expect(queryByText('highlight')).toBeEnabled();
Expand All @@ -90,13 +86,13 @@ test('.toBeEnabled', () => {
});

test('matcher misses', () => {
const { queryByTestId, queryByText } = render(
const { queryByTestId, queryByTitle } = render(
<View testID="view">
<Button testID="enabled" title="enabled" />
<Button disabled testID="disabled" title="disabled" />
</View>,
);

expect(() => expect(queryByTestId('enabled')).toBeDisabled()).toThrowError();
expect(() => expect(queryByText('disabled')).toBeEnabled()).toThrowError();
expect(() => expect(queryByTitle('disabled')).toBeEnabled()).toThrowError();
});
17 changes: 8 additions & 9 deletions src/__tests__/to-have-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Button, Text, View } from 'react-native';
import { render } from 'native-testing-library';

test('.toHaveProp', () => {
const { debug, queryByTestId } = render(
const { queryByTestId } = render(
<View>
<Text allowFontScaling={false} testID="text">
text
Expand All @@ -12,20 +12,19 @@ test('.toHaveProp', () => {
</View>,
);

expect(queryByTestId('button')).toHaveProp('accessibilityStates', ['disabled']);
expect(queryByTestId('button')).toHaveProp('accessible');
expect(queryByTestId('button')).not.toHaveProp('disabled');
expect(queryByTestId('button')).not.toHaveProp('title', 'ok');
expect(queryByTestId('button')).toHaveProp('disabled', true);
expect(queryByTestId('button')).toHaveProp('disabled');
expect(queryByTestId('button')).toHaveProp('title', 'ok');

expect(queryByTestId('text')).toHaveProp('allowFontScaling', false);
expect(queryByTestId('text')).not.toHaveProp('style');

expect(() =>
expect(queryByTestId('button')).not.toHaveProp('accessibilityStates', ['disabled']),
expect(queryByTestId('button')).toHaveProp('accessibilityStates', ['disabled']),
).toThrowError();
expect(() => expect(queryByTestId('button')).not.toHaveProp('accessible')).toThrowError();
expect(() => expect(queryByTestId('button')).toHaveProp('disabled')).toThrowError();
expect(() => expect(queryByTestId('button')).toHaveProp('title', 'ok')).toThrowError();
expect(() => expect(queryByTestId('button')).toHaveProp('accessible')).toThrowError();
expect(() => expect(queryByTestId('button')).not.toHaveProp('disabled')).toThrowError();
expect(() => expect(queryByTestId('button')).not.toHaveProp('title', 'ok')).toThrowError();

expect(() =>
expect(queryByTestId('text')).not.toHaveProp('allowFontScaling', false),
Expand Down
1 change: 1 addition & 0 deletions src/to-be-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { checkReactElement, getType, printElement } from './utils';

// Elements that support 'disabled'
const DISABLE_TYPES = [
'Button',
'Slider',
'Switch',
'Text',
Expand Down

0 comments on commit 1e36898

Please sign in to comment.