From 0fca723a69c1a8e8cc2e0a2179790bdd9526165f Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Fri, 12 Jun 2020 14:15:46 -0400 Subject: [PATCH] jsdoc unmount function --- cypress/component/basic/unmount/README.md | 19 +++++++++++ .../component/basic/unmount/unmount-spec.js | 33 +++++++++++++++++++ lib/index.ts | 10 ++++++ 3 files changed, 62 insertions(+) create mode 100644 cypress/component/basic/unmount/README.md create mode 100644 cypress/component/basic/unmount/unmount-spec.js diff --git a/cypress/component/basic/unmount/README.md b/cypress/component/basic/unmount/README.md new file mode 100644 index 00000000..b5ce0d4a --- /dev/null +++ b/cypress/component/basic/unmount/README.md @@ -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) diff --git a/cypress/component/basic/unmount/unmount-spec.js b/cypress/component/basic/unmount/unmount-spec.js new file mode 100644 index 00000000..404529dc --- /dev/null +++ b/cypress/component/basic/unmount/unmount-spec.js @@ -0,0 +1,33 @@ +/// +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
My component
+ } +} + +describe('Comp with componentWillUnmount', () => { + it('calls the prop', () => { + mount() + 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') + }) +}) diff --git a/lib/index.ts b/lib/index.ts index 6a323b31..ac99ae5d 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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()