Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 584da55

Browse files
committed
update README
1 parent ea4aaf8 commit 584da55

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

README.md

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010

1111
## Known problems
1212

13-
- [ ] some DOM events are not working when running all tests at once [#4](https://github.com/bahmutov/cypress-react-unit-test/issues/4)
14-
- [x] cannot mock server XHR for injected components [#5](https://github.com/bahmutov/cypress-react-unit-test/issues/5)
15-
- [x] cannot spy on `window.alert` [#6](https://github.com/bahmutov/cypress-react-unit-test/issues/6)
16-
1713
## Install
1814

1915
Requires [Node](https://nodejs.org/en/) version 8 or above.
@@ -22,46 +18,38 @@ Requires [Node](https://nodejs.org/en/) version 8 or above.
2218
npm install --save-dev cypress cypress-react-unit-test
2319
```
2420

25-
If you need help configuring bundler, see [preprocessors info](https://gitpitch.com/cypress-io/testing-workshop-cypress?p=slides/16-preprocessors)
26-
2721
## Use
2822

29-
Include this plugin from `cypress/support/index.js`
23+
Include this plugin from your project's `cypress/support/index.js`
3024

3125
```js
32-
import 'cypress-react-unit-test'
26+
require('cypress-react-unit-test/dist/hooks')
3327
```
3428

35-
This adds a new command `cy.mount` that can mount a React component. It also overloads `cy.get` to accept in addition to selectors React component, returning it. See examples below.
29+
Then turn the experimental component support on in your `cypress.json`. You can also specify where component spec files are located. For exampled to have them located in `src` folder use:
30+
31+
```json
32+
{
33+
"experimentalComponentTesting": true,
34+
"componentFolder": "src"
35+
}
36+
```
3637

3738
## Example
3839

3940
```js
40-
// load Cypress TypeScript definitions for IntelliSense
41-
/// <reference types="cypress" />
42-
// import the component you want to test
43-
import { HelloState } from '../../src/hello-x.jsx'
4441
import React from 'react'
45-
describe('HelloState component', () => {
42+
import { mount } from 'cypress-react-unit-test'
43+
import { HelloWorld } from './hello-world.jsx'
44+
describe('HelloWorld component', () => {
4645
it('works', () => {
47-
// mount the component under test
48-
cy.mount(<HelloState />)
49-
// start testing!
50-
cy.contains('Hello Spider-man!')
51-
// mounted component can be selected via its name, function, or JSX
52-
// e.g. '@HelloState', HelloState, or <HelloState />
53-
cy.get(HelloState).invoke('setState', { name: 'React' })
54-
cy.get(HelloState)
55-
.its('state')
56-
.should('deep.equal', { name: 'React' })
57-
// check if GUI has rerendered
58-
cy.contains('Hello React!')
46+
mount(<HelloWorld />)
47+
// now use standard Cypress commands
48+
cy.contains('Hello World!').should('be.visible')
5949
})
6050
})
6151
```
6252

63-
![Unit testing React components](images/demo.png)
64-
6553
### styles
6654

6755
If you component imports its own style, the style should be applied during the Cypress test. But sometimes you need more power.
@@ -208,10 +196,21 @@ Look at the examples in [cypress/component](cypress/component) folder.
208196

209197
## Large examples
210198

211-
- [bahmutov/calculator](https://github.com/bahmutov/calculator) tests multiple components: calculator App, Button, Display.
212-
- [bahmutov/react-todo-with-hooks](https://github.com/bahmutov/react-todo-with-hooks) branch `added-tests`
213-
- [bahmutov/test-redux-examples](https://github.com/bahmutov/test-redux-examples) branch `mount2`
214-
- [bahmutov/test-react-hooks-animations](https://github.com/bahmutov/test-react-hooks-animations) react hooks with spring animations test
199+
<!-- prettier-ignore-start -->
200+
Repo | description
201+
--- | ---
202+
[try-cra-with-unit-test](https://github.com/bahmutov/try-cra-with-unit-test) | Hello world initialized with CRAv3
203+
[try-cra-app-typescript](https://github.com/bahmutov/try-cra-app-typescript) | Hello world initialized with CRAv3 `--typescript`
204+
[react-todo-with-hooks](https://github.com/bahmutov/react-todo-with-hooks) | Modern web application using hooks
205+
[test-redux-examples](https://github.com/bahmutov/test-redux-examples) | Example apps copies from official Redux repo and tested as components
206+
[test-react-hooks-animations](https://github.com/bahmutov/test-react-hooks-animations) | Testing React springs fun blob animation
207+
[test-mdx-example](https://github.com/bahmutov/test-mdx-example) | Example testing MDX components using Cypress
208+
[test-apollo](https://github.com/bahmutov/test-apollo) | Component testing an application that uses Apollo GraphQL library
209+
[test-xstate-react](https://github.com/bahmutov/test-xstate-react) | XState component testing using Cypress
210+
[test-react-router-v5](https://github.com/bahmutov/test-react-router-v5) | A few tests of React Router v5
211+
[test-material-ui](https://github.com/bahmutov/test-material-ui) | Testing Material UI components: date pickers, lists, autocomplete
212+
[test-d3-react-gauge](https://github.com/bahmutov/test-d3-react-gauge) | Testing React D3 gauges
213+
<!-- prettier-ignore-end -->
215214

216215
To find more examples, see GitHub topic [cypress-react-unit-test-example](https://github.com/topics/cypress-react-unit-test-example)
217216

0 commit comments

Comments
 (0)