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

Commit 632a84a

Browse files
authored
feat: make unmount function automatically enqueue (#319)
The command `unmount()` is enqueued into the Cypress chain automatically. The old way of calling it should still work though. ```js import { mount, unmount } from 'cypress-react-unit-test' it('unmounts component', () => { mount(<Component />) // interact with the component using Cypress commands // old unmount, still works! cy.then(unmount) // new unmount call (preferred) unmount() }) ```
1 parent b36fed8 commit 632a84a

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

cypress/component/advanced/renderless/mouse-spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ describe('Renderless component', () => {
1919
.trigger('mousemove')
2020
.then(() => {
2121
expect(onMoved).to.have.been.calledWith(true)
22-
unmount()
2322
})
2423

24+
unmount()
25+
2526
cy.get('@log')
2627
.its('callCount')
2728
.should('equal', 4)

cypress/component/basic/unmount/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ If you need to test what the component is doing when it is being unmounted, use
55
```js
66
import { mount, unmount } from 'cypress-react-unit-test'
77
it('calls unmount prop', () => {
8+
// async command
89
mount(...)
910
// cy commands
10-
// now let's unmount
11-
cy.then(unmount)
11+
12+
// now let's unmount (async command)
13+
unmount()
1214

1315
// confirm the component has been unmounted
1416
// and performed everything needed in its

cypress/component/basic/unmount/comp-spec.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ it('calls callbacks on mount and unmount', () => {
1212
expect(onMount).to.have.been.calledOnce
1313
expect(onUnmount).to.have.not.been.called
1414
})
15-
cy.contains('Component with')
16-
.should('be.visible')
17-
.then(unmount)
18-
.then(() => {
19-
expect(onUnmount).to.have.been.calledOnce
20-
})
15+
cy.contains('Component with').should('be.visible')
16+
unmount().then(() => {
17+
expect(onUnmount).to.have.been.calledOnce
18+
})
2119
cy.contains('Component with').should('not.exist')
2220
})

cypress/component/basic/unmount/unmount-spec.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@ describe('Comp with componentWillUnmount', () => {
1818
mount(<Comp onUnmount={cy.stub().as('onUnmount')} />)
1919
cy.contains('My component')
2020

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-
})
21+
// after we have confirmed the component exists let's remove it
22+
// unmount() command is automatically enqueued
23+
unmount()
24+
25+
// the component is gone from the DOM
26+
cy.contains('My component').should('not.exist')
27+
// the component has called the prop on unmount
28+
cy.get('@onUnmount').should('have.been.calledOnce')
29+
})
30+
31+
it('can be called using then', () => {
32+
mount(<Comp onUnmount={cy.stub().as('onUnmount')} />)
33+
cy.contains('My component')
34+
35+
// still works, should probably be removed in v5
36+
cy.then(unmount)
2737

2838
// the component is gone from the DOM
2939
cy.contains('My component').should('not.exist')

lib/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,30 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
139139
}
140140

141141
/**
142-
* Removes the mounted component
142+
* Removes the mounted component. Notice this command automatically
143+
* queues up the `unmount` into Cypress chain, thus you don't need `.then`
144+
* to call it.
143145
* @see https://github.com/bahmutov/cypress-react-unit-test/tree/main/cypress/component/basic/unmount
144146
* @example
145147
```
146148
import { mount, unmount } from 'cypress-react-unit-test'
147149
it('works', () => {
148150
mount(...)
151+
// interact with the component using Cypress commands
149152
// whenever you want to unmount
150-
cy.then(unmount)
153+
unmount()
151154
})
152155
```
153156
*/
154157
export const unmount = () => {
155158
checkMountModeEnabled()
156159

157-
cy.log('unmounting...')
158-
const selector = '#' + rootId
159-
return cy.get(selector, { log: false }).then($el => {
160-
unmountComponentAtNode($el[0])
160+
return cy.then(() => {
161+
cy.log('unmounting...')
162+
const selector = '#' + rootId
163+
return cy.get(selector, { log: false }).then($el => {
164+
unmountComponentAtNode($el[0])
165+
})
161166
})
162167
}
163168

0 commit comments

Comments
 (0)