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

Commit f99b41a

Browse files
committed
Adds rerender on testHook return
1 parent 3d92dcc commit f99b41a

File tree

6 files changed

+74
-11
lines changed

6 files changed

+74
-11
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ What happened:
5959

6060
<!-- Please provide the full error message/screenshots/anything -->
6161

62-
Reproduction repository: https://github.com/alexkrolick/dom-testing-library-template
62+
Reproduction repository:
63+
https://github.com/alexkrolick/dom-testing-library-template
6364

6465
<!--
6566
If possible, please create a repository that reproduces the issue with the

examples/__tests__/mock.react-transition-group.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ afterEach(cleanup)
3131

3232
jest.mock('react-transition-group', () => {
3333
const FakeTransition = jest.fn(({children}) => children)
34-
const FakeCSSTransition = jest.fn(
35-
props =>
36-
props.in ? <FakeTransition>{props.children}</FakeTransition> : null,
34+
const FakeCSSTransition = jest.fn(props =>
35+
props.in ? <FakeTransition>{props.children}</FakeTransition> : null,
3736
)
3837
return {CSSTransition: FakeCSSTransition, Transition: FakeTransition}
3938
})

examples/__tests__/react-hooks.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import {testHook, act, cleanup} from 'react-testing-library'
88

9-
import { useCounter, useDocumentTitle } from '../react-hooks'
9+
import {useCounter, useDocumentTitle, useCall} from '../react-hooks'
1010

1111
afterEach(cleanup)
1212

@@ -59,6 +59,7 @@ describe('useCounter', () => {
5959
})
6060
})
6161

62+
// using unmount function to check useEffect behavior when unmounting
6263
describe('useDocumentTitle', () => {
6364
test('sets a title', () => {
6465
document.title = 'original title'
@@ -71,11 +72,48 @@ describe('useDocumentTitle', () => {
7172

7273
test('returns to original title when component is unmounted', () => {
7374
document.title = 'original title'
74-
const { unmount } = testHook(() => {
75+
const {unmount} = testHook(() => {
7576
useDocumentTitle('modified title')
7677
})
7778

7879
unmount()
7980
expect(document.title).toBe('original title')
8081
})
81-
})
82+
})
83+
84+
// using rerender function to test calling useEffect multiple times
85+
describe('useCall', () => {
86+
test('calls once on render', () => {
87+
const spy = jest.fn()
88+
testHook(() => {
89+
useCall(spy, [])
90+
})
91+
expect(spy).toHaveBeenCalledTimes(1)
92+
})
93+
94+
test('calls again if deps change', () => {
95+
let deps = [false]
96+
const spy = jest.fn()
97+
const {rerender} = testHook(() => {
98+
useCall(spy, deps)
99+
})
100+
expect(spy).toHaveBeenCalledTimes(1)
101+
102+
deps = [true]
103+
rerender()
104+
expect(spy).toHaveBeenCalledTimes(2)
105+
})
106+
107+
test('does not call again if deps are the same', () => {
108+
let deps = [false]
109+
const spy = jest.fn()
110+
const {rerender} = testHook(() => {
111+
useCall(spy, deps)
112+
})
113+
expect(spy).toHaveBeenCalledTimes(1)
114+
115+
deps = [false]
116+
rerender()
117+
expect(spy).toHaveBeenCalledTimes(1)
118+
})
119+
})

examples/react-hooks.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ export function useDocumentTitle(title) {
1717
}
1818
}, [title])
1919
}
20+
21+
export function useCall(callback, deps) {
22+
useEffect(() => {
23+
callback()
24+
}, deps)
25+
}

src/__tests__/test-hook.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ test('testHook calls the callback', () => {
1212
test('confirm we can safely call a React Hook from within the callback', () => {
1313
testHook(() => useState())
1414
})
15-
test('returns a function to unmount the component', () => {
15+
test('returns a function to unmount component', () => {
1616
let isMounted
17-
const { unmount } = testHook(() => {
17+
const {unmount} = testHook(() => {
1818
useEffect(() => {
1919
isMounted = true
2020
return () => {
@@ -26,3 +26,15 @@ test('returns a function to unmount the component', () => {
2626
unmount()
2727
expect(isMounted).toBe(false)
2828
})
29+
test('returns a function to rerender component', () => {
30+
let renderCount = 0
31+
const {rerender} = testHook(() => {
32+
useEffect(() => {
33+
renderCount++
34+
})
35+
})
36+
37+
expect(renderCount).toBe(1)
38+
rerender()
39+
expect(renderCount).toBe(2)
40+
})

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ function TestHook({callback}) {
6767
}
6868

6969
function testHook(callback) {
70-
const { unmount } = render(<TestHook callback={callback} />)
71-
return { unmount }
70+
const {unmount, rerender: rerenderComponent} = render(
71+
<TestHook callback={callback} />,
72+
)
73+
return {
74+
unmount,
75+
rerender: () => {
76+
rerenderComponent(<TestHook callback={callback} />)
77+
},
78+
}
7279
}
7380

7481
function cleanup() {

0 commit comments

Comments
 (0)