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

Commit

Permalink
jsdoc unmount function
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jun 12, 2020
1 parent 7201f6a commit 0fca723
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cypress/component/basic/unmount/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# unmount

If you need to test what the component is doing when it is being unmounted, use `unmount` function.

```js
import { mount, unmount } from 'cypress-react-unit-test'
it('calls unmount prop', () => {
mount(...)
// cy commands
// now let's unmount
cy.then(unmount)

// confirm the component has been unmounted
// and performed everything needed in its
// componentWillUnmount method
})
```

See [unmount-spec.js](unmount-spec.js) and [comp-spec.js](comp-spec.js)
33 changes: 33 additions & 0 deletions cypress/component/basic/unmount/unmount-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// <reference types="cypress" />
import React, { Component } from 'react'
import { mount, unmount } from 'cypress-react-unit-test'

class Comp extends Component {
componentWillUnmount() {
// simply calls the prop
this.props.onUnmount()
}

render() {
return <div>My component</div>
}
}

describe('Comp with componentWillUnmount', () => {
it('calls the prop', () => {
mount(<Comp onUnmount={cy.stub().as('onUnmount')} />)
cy.contains('My component')

// after we have confirmed the component exists
// we can remove it asynchronously
cy.then(() => {
// now unmount the mounted component
unmount()
})

// the component is gone from the DOM
cy.contains('My component').should('not.exist')
// the component has called the prop on unmount
cy.get('@onUnmount').should('have.been.calledOnce')
})
})
10 changes: 10 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {

/**
* Removes any mounted component
* @see https://github.com/bahmutov/cypress-react-unit-test/tree/master/cypress/component/basic/unmount
* @example
```
import { mount, unmount } from 'cypress-react-unit-test'
it('works', () => {
mount(...)
// whenever you want to unmount
cy.then(unmount)
})
```
*/
export const unmount = () => {
checkMountModeEnabled()
Expand Down

0 comments on commit 0fca723

Please sign in to comment.