Skip to content

Commit 92ac0df

Browse files
committed
Add additional test for hooks
1 parent b207aee commit 92ac0df

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
]
3333
},
3434
"lint-staged": {
35-
"**/*.{js}": [
35+
"**/*.js": [
3636
"flow focus-check",
3737
"prettier --write",
3838
"git add"

src/__tests__/suspense.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import React, { Fragment, Component, createElement, createContext, useContext, useState } from 'react'
1+
import React, {
2+
Fragment,
3+
Component,
4+
createElement,
5+
createContext,
6+
useContext,
7+
useState,
8+
useMemo,
9+
useRef
10+
} from 'react'
211

312
import renderPrepass from '..'
413

@@ -44,6 +53,36 @@ describe('renderPrepass', () => {
4453
})
4554
})
4655

56+
it('preserves state correctly across suspensions', () => {
57+
const getValue = jest.fn()
58+
.mockImplementationOnce(() => { throw Promise.resolve() })
59+
.mockImplementation(() => 'test')
60+
61+
const Inner = jest.fn(props => {
62+
expect(props.value).toBe('test')
63+
expect(props.state).toBe('test')
64+
})
65+
66+
const Outer = jest.fn(() => {
67+
const [state, setState] = useState('default')
68+
69+
const memoed = useMemo(() => state, [state])
70+
const ref = useRef('initial')
71+
expect(memoed).toBe(state)
72+
expect(ref.current).toBe('initial')
73+
74+
const value = getValue()
75+
setState(value)
76+
77+
return <Inner value={value} state={state} />
78+
})
79+
80+
return renderPrepass(<Outer />).then(() => {
81+
expect(Outer).toHaveBeenCalledTimes(3 * 3 * 3 /* welp */)
82+
expect(Inner).toHaveBeenCalledTimes(2)
83+
})
84+
})
85+
4786
it('ignores thrown non-promises', () => {
4887
const Outer = () => { throw new Error('test') }
4988
const render$ = renderPrepass(<Outer />)

0 commit comments

Comments
 (0)