Skip to content

Commit e6088d1

Browse files
test: add a test for componentWillUnmount first TDD
1 parent 1f6f722 commit e6088d1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
} from '@testing-library/react'
99
import useLifecycleHelpers from './'
1010

11+
const mockCleanupCallback = jest.fn()
12+
beforeEach(() => {
13+
jest.clearAllMocks()
14+
})
1115
afterEach(cleanup)
1216

1317
const Component = props => {
@@ -36,6 +40,10 @@ const Component = props => {
3640
['counter']
3741
)
3842

43+
useComponentWillUnmount(() => {
44+
mockCleanupCallback()
45+
})
46+
3947
return (
4048
<div id='component'>
4149
<p>{componentState}</p>
@@ -76,4 +84,15 @@ describe('Test useLifecycleHelpers custom hook', () => {
7684

7785
getByText(/DepsUpdated/i)
7886
})
87+
88+
test('The component should be unmounted', () => {
89+
const dom = renderComponent()
90+
const getById = queryByAttribute.bind(null, 'id')
91+
const { unmount } = dom
92+
unmount()
93+
94+
const component = getById(dom.container, 'component')
95+
expect(component).toBe(null)
96+
expect(mockCleanupCallback.mock.calls.length).toBe(1)
97+
})
7998
})

0 commit comments

Comments
 (0)