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

Commit 4101945

Browse files
author
Thomas Treffry
authored
Merge pull request #71 from nearform/remove-flush-effects
Replace experimental flushEffects with act from react-dom/test-utils
2 parents 056c04a + f841306 commit 4101945

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

test/unit/hooks/geolocation.unit.test.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { flushEffects } from 'react-testing-library'
21
import { testHook, cleanup } from 'react-proxy-hook'
2+
import { act } from 'react-dom/test-utils'
3+
34
import { useGeolocation } from '../../../src'
45

56
afterEach(cleanup)
@@ -18,9 +19,9 @@ describe('useGeolocation', () => {
1819
})
1920

2021
it('calls getCurrentPosition with options', () => {
21-
testHook(() => useGeolocation({ foo: 'bar' }))
22-
23-
flushEffects()
22+
act(() => {
23+
testHook(() => useGeolocation({ foo: 'bar' }))
24+
})
2425

2526
expect(navigator.geolocation.getCurrentPosition).toHaveBeenCalledWith(
2627
expect.any(Function),
@@ -37,9 +38,9 @@ describe('useGeolocation', () => {
3738
.mockImplementationOnce((onSuccess) => onSuccess('position'))
3839

3940
let position, error
40-
testHook(() => ({ position, error } = useGeolocation()))
41-
42-
flushEffects()
41+
act(() => {
42+
testHook(() => ({ position, error } = useGeolocation()))
43+
})
4344

4445
expect(position).toBe('position')
4546
expect(error).toBe(null)
@@ -53,17 +54,17 @@ describe('useGeolocation', () => {
5354
.mockImplementationOnce((_, onError) => onError(mockError))
5455

5556
let error
56-
testHook(() => ({ error } = useGeolocation()))
57-
58-
flushEffects()
57+
act(() => {
58+
testHook(() => ({ error } = useGeolocation()))
59+
})
5960

6061
expect(error).toBe(mockError)
6162
})
6263

6364
it('calls watchPosition with options', () => {
64-
testHook(() => useGeolocation({ foo: 'bar' }))
65-
66-
flushEffects()
65+
act(() => {
66+
testHook(() => useGeolocation({ foo: 'bar' }))
67+
})
6768

6869
expect(navigator.geolocation.watchPosition).toHaveBeenCalledWith(
6970
expect.any(Function),
@@ -80,9 +81,9 @@ describe('useGeolocation', () => {
8081
.mockImplementationOnce((onSuccess) => onSuccess('position'))
8182

8283
let position, error
83-
testHook(() => ({ position, error } = useGeolocation()))
84-
85-
flushEffects()
84+
act(() => {
85+
testHook(() => ({ position, error } = useGeolocation()))
86+
})
8687

8788
expect(position).toBe('position')
8889
expect(error).toBe(null)
@@ -96,9 +97,9 @@ describe('useGeolocation', () => {
9697
.mockImplementationOnce((_, onError) => onError(mockError))
9798

9899
let error
99-
testHook(() => ({ error } = useGeolocation()))
100-
101-
flushEffects()
100+
act(() => {
101+
testHook(() => ({ error } = useGeolocation()))
102+
})
102103

103104
expect(error).toBe(mockError)
104105
})

test/unit/hooks/mouse-position.unit.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent, flushEffects } from 'react-testing-library'
1+
import { fireEvent } from 'react-testing-library'
22
import { testHook, cleanup } from 'react-proxy-hook'
33
import { act } from 'react-dom/test-utils'
44

@@ -18,9 +18,10 @@ describe('useMousePosition', () => {
1818

1919
it('updates state on "mousemove"', () => {
2020
let x, y
21-
testHook(() => ({ x, y } = useMousePosition()))
2221

23-
flushEffects()
22+
act(() => {
23+
testHook(() => ({ x, y } = useMousePosition()))
24+
})
2425

2526
act(() => {
2627
fireEvent.mouseMove(document.body, { clientX: 100, clientY: 100 })

test/unit/hooks/orientation.unit.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent, flushEffects } from 'react-testing-library'
1+
import { fireEvent } from 'react-testing-library'
22
import { testHook, cleanup } from 'react-proxy-hook'
33
import { act } from 'react-dom/test-utils'
44

@@ -20,9 +20,10 @@ describe('useOrientation', () => {
2020
it('updates state on "orientationchange"', () => {
2121
let angle, type
2222
window.screen.orientation = { angle: 0, type: 'portrait-primary' }
23-
testHook(() => ({ angle, type } = useOrientation()))
2423

25-
flushEffects()
24+
act(() => {
25+
testHook(() => ({ angle, type } = useOrientation()))
26+
})
2627

2728
window.screen.orientation = { angle: 90, type: 'landscape-primary' }
2829

test/unit/hooks/page-visibility.unit.test.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent, flushEffects } from 'react-testing-library'
1+
import { fireEvent } from 'react-testing-library'
22
import { testHook, cleanup } from 'react-proxy-hook'
33
import { act } from 'react-dom/test-utils'
44

@@ -48,9 +48,10 @@ describe('usePageVisibility', () => {
4848
it('updates state on the "visibilitychange" event', () => {
4949
let visible
5050
document.hidden = false
51-
testHook(() => (visible = usePageVisibility()))
5251

53-
flushEffects()
52+
act(() => {
53+
testHook(() => (visible = usePageVisibility()))
54+
})
5455

5556
document.hidden = true
5657

@@ -109,9 +110,10 @@ describe('usePageVisibility', () => {
109110
it('updates state on the "msvisibilitychange" event', () => {
110111
let visible
111112
document.msHidden = false
112-
testHook(() => (visible = usePageVisibility()))
113113

114-
flushEffects()
114+
act(() => {
115+
testHook(() => (visible = usePageVisibility()))
116+
})
115117

116118
document.msHidden = true
117119
act(() => {
@@ -169,9 +171,10 @@ describe('usePageVisibility', () => {
169171
it('updates state on the "webkitvisibilitychange" event', () => {
170172
let visible
171173
document.webkitHidden = false
172-
testHook(() => (visible = usePageVisibility()))
173174

174-
flushEffects()
175+
act(() => {
176+
testHook(() => (visible = usePageVisibility()))
177+
})
175178

176179
document.webkitHidden = true
177180
act(() => {

0 commit comments

Comments
 (0)