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

Commit 0fca723

Browse files
committed
jsdoc unmount function
1 parent 7201f6a commit 0fca723

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# unmount
2+
3+
If you need to test what the component is doing when it is being unmounted, use `unmount` function.
4+
5+
```js
6+
import { mount, unmount } from 'cypress-react-unit-test'
7+
it('calls unmount prop', () => {
8+
mount(...)
9+
// cy commands
10+
// now let's unmount
11+
cy.then(unmount)
12+
13+
// confirm the component has been unmounted
14+
// and performed everything needed in its
15+
// componentWillUnmount method
16+
})
17+
```
18+
19+
See [unmount-spec.js](unmount-spec.js) and [comp-spec.js](comp-spec.js)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference types="cypress" />
2+
import React, { Component } from 'react'
3+
import { mount, unmount } from 'cypress-react-unit-test'
4+
5+
class Comp extends Component {
6+
componentWillUnmount() {
7+
// simply calls the prop
8+
this.props.onUnmount()
9+
}
10+
11+
render() {
12+
return <div>My component</div>
13+
}
14+
}
15+
16+
describe('Comp with componentWillUnmount', () => {
17+
it('calls the prop', () => {
18+
mount(<Comp onUnmount={cy.stub().as('onUnmount')} />)
19+
cy.contains('My component')
20+
21+
// after we have confirmed the component exists
22+
// we can remove it asynchronously
23+
cy.then(() => {
24+
// now unmount the mounted component
25+
unmount()
26+
})
27+
28+
// the component is gone from the DOM
29+
cy.contains('My component').should('not.exist')
30+
// the component has called the prop on unmount
31+
cy.get('@onUnmount').should('have.been.calledOnce')
32+
})
33+
})

lib/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
130130

131131
/**
132132
* Removes any mounted component
133+
* @see https://github.com/bahmutov/cypress-react-unit-test/tree/master/cypress/component/basic/unmount
134+
* @example
135+
```
136+
import { mount, unmount } from 'cypress-react-unit-test'
137+
it('works', () => {
138+
mount(...)
139+
// whenever you want to unmount
140+
cy.then(unmount)
141+
})
142+
```
133143
*/
134144
export const unmount = () => {
135145
checkMountModeEnabled()

0 commit comments

Comments
 (0)