Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
bcarroll22 committed May 16, 2019
2 parents ddeb2dc + 828ceff commit 5813dc8
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projectName": "jest-native",
"projectOwner": "bcarroll22",
"projectOwner": "testing-library",
"repoType": "github",
"repoHost": "https://github.com",
"files": [
Expand Down
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ cache:
- coverage
notifications:
email: false
install: npm install
script: npm run test:coverage
install: npm install && npm install -g codecov
script: npm run test:coverage && codecov
jobs:
include:
- stage: release
if: branch = master AND type != pull_request
node_js: '10'
install: npm install -g codecov
script: codecov && npm run semantic-release
script: npm run semantic-release
60 changes: 35 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
height="80"
width="80"
alt="goat"
src="https://raw.githubusercontent.com/bcarroll22/jest-native/master/other/eagle.png"
src="https://raw.githubusercontent.com/testing-library/jest-native/master/other/eagle.png"
/>
</a>

Expand All @@ -15,35 +15,37 @@

<hr />

[![Build Status](https://travis-ci.org/bcarroll22/jest-native.svg?branch=master)](https://travis-ci.org/bcarroll22/jest-native)
[![Code Coverage](https://img.shields.io/codecov/c/github/bcarroll22/jest-native.svg?style=flat-square)](https://codecov.io/github/bcarroll22/jest-native)
[![Build Status](https://travis-ci.org/testing-library/jest-native.svg?branch=master)](https://travis-ci.org/testing-library/jest-native)
[![Code Coverage](https://img.shields.io/codecov/c/github/testing-library/jest-native.svg?style=flat-square)](https://codecov.io/github/testing-library/jest-native)
[![version](https://img.shields.io/npm/v/jest-native.svg?style=flat-square)](https://www.npmjs.com/package/jest-native)
[![downloads](https://img.shields.io/npm/dm/jest-native.svg?style=flat-square)](http://www.npmtrends.com/jest-native)
[![MIT License](https://img.shields.io/npm/l/jest-native.svg?style=flat-square)](https://github.com/bcarroll22/jest-native/blob/master/LICENSE)
[![MIT License](https://img.shields.io/npm/l/jest-native.svg?style=flat-square)](https://github.com/testing-library/jest-native/blob/master/LICENSE)

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/bcarroll22/jest-native/blob/master/CODE_OF_CONDUCT.md)
[![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/testing-library/jest-native/blob/master/CODE_OF_CONDUCT.md)

[![Watch on GitHub](https://img.shields.io/github/watchers/bcarroll22/jest-native.svg?style=social)](https://github.com/bcarroll22/jest-native/watchers)
[![Star on GitHub](https://img.shields.io/github/stars/bcarroll22/jest-native.svg?style=social)](https://github.com/bcarroll22/jest-native/stargazers)
[![Watch on GitHub](https://img.shields.io/github/watchers/testing-library/jest-native.svg?style=social)](https://github.com/testing-library/jest-native/watchers)
[![Star on GitHub](https://img.shields.io/github/stars/testing-library/jest-native.svg?style=social)](https://github.com/testing-library/jest-native/stargazers)

<!-- 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)
- [This solution](#this-solution)
- [Compatibility](#compatibility)
- [Installation](#installation)
- [Usage](#usage)
- [Matchers](#matchers)
- [`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 All @@ -53,7 +55,7 @@
## The problem

You want to use [jest](https://facebook.github.io/jest/) to write tests that assert various things
about the state of a React Native tree. As part of that goal, you want to avoid all the repetitive
about the state of a React Native app. As part of that goal, you want to avoid all the repetitive
patterns that arise in doing so like checking for a native element's props, its text content, its
styles, and more.

Expand All @@ -62,6 +64,14 @@ styles, and more.
The `jest-native` library provides a set of custom jest matchers that you can use to extend jest.
These will make your tests more declarative, clear to read and to maintain.

## Compatibility

These matchers should, for the most part, be agnostic enough to work with any React Native testing
utilities, but they are primarily intended to be used with
[native-testing-library](https://github.com/testing-library/native-testing-library). Any issues
raised with existing matchers or any newly proposed matchers must be viewed through compatibility
with that library and its guiding principles first.

## Installation

This module should be installed as one of your project's `devDependencies`:
Expand Down Expand Up @@ -162,10 +172,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 +205,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 +234,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 +258,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 Expand Up @@ -279,7 +289,7 @@ expect(queryByText('Hello World')).not.toHaveStyle({ color: 'white' });
## Inspiration

This library was made to be a companion for
[native-testing-library](https://github.com/bcarroll22/native-testing-library).
[native-testing-library](https://github.com/testing-library/native-testing-library).

It was inspired by [jest-dom](https://github.com/gnapse/jest-dom/), the companion library for
[dom-testing-library](https://github.com/kentcdodds/dom-testing-library/). We emulated as many of
Expand All @@ -295,7 +305,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
<table><tr><td align="center"><a href="https://github.com/bcarroll22"><img src="https://avatars2.githubusercontent.com/u/11020406?v=4" width="100px;" alt="Brandon Carroll"/><br /><sub><b>Brandon Carroll</b></sub></a><br /><a href="https://github.com/bcarroll22/jest-native/commits?author=bcarroll22" title="Code">💻</a> <a href="https://github.com/bcarroll22/jest-native/commits?author=bcarroll22" title="Documentation">📖</a> <a href="#infra-bcarroll22" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/bcarroll22/jest-native/commits?author=bcarroll22" title="Tests">⚠️</a></td></tr></table>
<table><tr><td align="center"><a href="https://github.com/bcarroll22"><img src="https://avatars2.githubusercontent.com/u/11020406?v=4" width="100px;" alt="Brandon Carroll"/><br /><sub><b>Brandon Carroll</b></sub></a><br /><a href="https://github.com/bcarroll22/jest-native/commits?author=bcarroll22" title="Code">💻</a> <a href="https://github.com/testing-library/jest-native/commits?author=bcarroll22" title="Documentation">📖</a> <a href="#infra-bcarroll22" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/testing-library/jest-native/commits?author=bcarroll22" title="Tests">⚠️</a></td></tr></table>

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
5 changes: 5 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
overrides: [
{
compact: false,
},
],
};
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
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
"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"],
"files": [
"dist",
"extend-expect.js",
"extend-expect.d.ts"
],
"keywords": [
"testing",
"native-testing-library",
Expand All @@ -28,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 @@ -39,17 +44,16 @@
"husky": "^1.3.1",
"jest": "^24.7.1",
"metro-react-native-babel-preset": "^0.52.0",
"native-testing-library": "^1.5.0",
"native-testing-library": "^3.0.0",
"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 Expand Up @@ -79,12 +83,12 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/bcarroll22/jest-native.git"
"url": "git+https://github.com/testing-library/jest-native.git"
},
"author": "Brandon Carroll",
"license": "MIT",
"bugs": {
"url": "https://github.com/bcarroll22/jest-native/issues"
"url": "https://github.com/testing-library/jest-native/issues"
},
"homepage": "https://github.com/bcarroll22/jest-native#readme"
"homepage": "https://github.com/testing-library/jest-native#readme"
}
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
Loading

0 comments on commit 5813dc8

Please sign in to comment.