Skip to content
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

feat: support changes for native-testing-library preset #2

Merged
merged 4 commits 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
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

## Table of Contents

- [The problem](#the-problem)
Expand All @@ -40,10 +41,10 @@
- [`toBeDisabled`](#tobedisabled)
- [`toBeEnabled`](#tobeenabled)
- [`toBeEmpty`](#tobeempty)
- [`toContainElement(element)`](#tocontainelementelement)
- [`toHaveProp(prop, value)`](#tohavepropprop-value)
- [`toHaveTextContent(text)`](#tohavetextcontenttext)
- [`toHaveStyle(styles)`](#tohavestylestyles)
- [`toContainElement`](#tocontainelementelement)
- [`toHaveProp`](#tohavepropprop-value)
- [`toHaveTextContent`](#tohavetextcontenttext)
- [`toHaveStyle`](#tohavestylestyles)
- [Inspiration](#inspiration)
- [Other solutions](#other-solutions)
- [Contributors](#contributors)
Expand Down Expand Up @@ -162,10 +163,10 @@ const { getByTestId } = render(<View testID="empty" />);
expect(getByTestId('empty')).toBeEmpty();
```

### `toContainElement(element)`
### `toContainElement`

```javascript
toContainElement();
```typescript
toContainElement(element: ReactTestInstance | null);
```

Check if an element contains another element as a descendant. Again, will only work for native
Expand Down Expand Up @@ -195,10 +196,10 @@ expect(parent).toContainElement(child);
expect(parent).not.toContainElement(grandparent);
```

### `toHaveProp(prop, value)`
### `toHaveProp`

```javascript
toHaveProp(prop, value);
```typescript
toHaveProp(prop: string, value?: any);
```

Check that an element has a given prop. Only works for native elements, so this is similar to
Expand All @@ -224,10 +225,10 @@ expect(queryByTestId('button')).not.toHaveProp('disabled');
expect(queryByTestId('button')).not.toHaveProp('title', 'ok');
```

### `toHaveTextContent(text)`
### `toHaveTextContent`

```javascript
toHaveTextContent(text);
```typescript
toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean });
```

Check if an element has the supplied text.
Expand All @@ -248,10 +249,10 @@ expect(queryByTestId('count-value')).toHaveTextContent(/2/);
expect(queryByTestId('count-value')).not.toHaveTextContent('21');
```

### `toHaveStyle(styles)`
### `toHaveStyle`

```javascript
toHaveStyle(styles);
toHaveStyle(style: object[] | object);
```

Check if an element has the supplied styles.
Expand Down
13 changes: 13 additions & 0 deletions extend-expect.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ReactTestInstance } from 'react-test-renderer';

declare namespace jest {
interface Matchers<R> {
toBeDisabled(): R;
toContainElement(element: ReactTestInstance | null): R;
toBeEmpty(): R;
toHaveProp(attr: string, value?: any): R;
toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R;
toBeEnabled(): R;
toHaveStyle(style: object[] | object): R;
}
}
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
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
"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)'",
"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"
},
"files": [
"dist",
"extend-expect.js"
"extend-expect.js",
"extend-expect.d.ts"
],
"keywords": [
"testing",
Expand All @@ -31,6 +32,7 @@
"jest-matcher-utils": "^24.0.0",
"ramda": "^0.26.1",
"pretty-format": "^24.0.0",
"react-test-renderer": "^16.8.0",
"redent": "^2.0.0"
},
"devDependencies": {
Expand All @@ -42,17 +44,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