Skip to content

Commit 6bf8228

Browse files
committed
Added useCallback and useMemo tests
1 parent 7923d70 commit 6bf8228

File tree

5 files changed

+106
-34
lines changed

5 files changed

+106
-34
lines changed

src/flush.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import invariant from 'invariant'
2+
3+
export default (context) => () => {
4+
invariant(context.flush, 'You must use the hook before effects can be flushed')
5+
6+
context.flush()
7+
8+
return context.result
9+
}

src/flushEffects.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/use.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default (hook, context) => (props) => {
1616

1717
context.update({
1818
rerender: (newProps) => rerender(context.resolveComponent(<HookHarness {...newProps} />)),
19-
flushEffects: () => context.rerender(props)
19+
flush: () => context.rerender(props)
2020
})
2121

2222
return context.result

src/useHookAdvanced.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import use from './use'
22
import withContextProvider from './withContextProvider'
3+
import flush from './flush'
34
import update from './update'
4-
import flushEffects from './flushEffects'
55

66
export default (hook, context) => {
77
return {
88
use: use(hook, context),
99
withContextProvider: withContextProvider(hook, context),
10-
update: update(context),
11-
flushEffects: flushEffects(context)
10+
flush: flush(context),
11+
update: update(context)
1212
}
1313
}

test/integration.test.js

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
1-
import React, { useState, useContext, createContext, useEffect } from 'react'
1+
import {
2+
useState,
3+
useContext,
4+
createContext,
5+
useEffect,
6+
useReducer,
7+
useCallback,
8+
useMemo
9+
} from 'react'
210
import { useHook, cleanup } from 'src'
311

412
describe('useHook tests', () => {
513
afterEach(cleanup)
614

7-
test('should handle useState hooks', () => {
8-
const { use, update } = useHook(() => useState('foo'))
15+
test('should handle useState hook', () => {
16+
const { use, flush } = useHook(() => useState('foo'))
917

1018
const [value1, setValue] = use()
1119

1220
expect(value1).toBe('foo')
1321

1422
setValue('bar')
1523

16-
const [value2] = update()
24+
const [value2] = flush()
1725

1826
expect(value2).toBe('bar')
1927
})
2028

21-
test('should handle useContext hooks', () => {
22-
const TestContext = createContext('foo')
23-
24-
const { use } = useHook(() => useContext(TestContext)).withContextProvider(
25-
TestContext.Provider,
26-
{ value: 'bar' }
27-
)
28-
29-
const value = use()
30-
31-
expect(value).toBe('bar')
32-
})
33-
34-
test('should handle useEffect hooks', () => {
29+
test('should handle useEffect hook', () => {
3530
const sideEffect = { [1]: false, [2]: false }
3631

37-
const { use, flushEffects, update } = useHook(({ id }) =>
32+
const { use, flush, update } = useHook(({ id }) =>
3833
useEffect(
3934
() => {
4035
sideEffect[id] = true
@@ -51,7 +46,7 @@ describe('useHook tests', () => {
5146
expect(sideEffect[1]).toBe(false)
5247
expect(sideEffect[2]).toBe(false)
5348

54-
flushEffects()
49+
flush()
5550

5651
expect(sideEffect[1]).toBe(true)
5752
expect(sideEffect[2]).toBe(false)
@@ -61,9 +56,86 @@ describe('useHook tests', () => {
6156
expect(sideEffect[1]).toBe(true)
6257
expect(sideEffect[2]).toBe(false)
6358

64-
flushEffects()
59+
flush()
6560

6661
expect(sideEffect[1]).toBe(false)
6762
expect(sideEffect[2]).toBe(true)
6863
})
64+
65+
test('should handle useContext hook', () => {
66+
const TestContext = createContext('foo')
67+
68+
const { use } = useHook(() => useContext(TestContext)).withContextProvider(
69+
TestContext.Provider,
70+
{ value: 'bar' }
71+
)
72+
73+
const value = use()
74+
75+
expect(value).toBe('bar')
76+
})
77+
78+
test('should handle useReducer hook', () => {
79+
const reducer = (state, action) => (action.type === 'inc' ? state + 1 : state)
80+
const { use, flush } = useHook(() => useReducer(reducer, 0))
81+
82+
const [initialState, dispatch] = use()
83+
84+
expect(initialState).toBe(0)
85+
86+
dispatch({ type: 'inc' })
87+
88+
const [state] = flush()
89+
90+
expect(state).toBe(1)
91+
})
92+
93+
test('should handle useCallback hook', () => {
94+
const { use, flush, update } = useHook(({ value }) => {
95+
const callback = () => ({ value })
96+
return useCallback(callback, [value])
97+
})
98+
99+
const callback1 = use({ value: 1 })
100+
101+
const calbackValue1 = callback1()
102+
103+
expect(calbackValue1).toEqual({ value: 1 })
104+
105+
const callback2 = flush()
106+
107+
const calbackValue2 = callback2()
108+
109+
expect(calbackValue2).toEqual({ value: 1 })
110+
111+
expect(callback2).toBe(callback1)
112+
113+
const callback3 = update({ value: 2 })
114+
115+
const calbackValue3 = callback3()
116+
117+
expect(calbackValue3).toEqual({ value: 2 })
118+
119+
expect(callback3).not.toBe(callback1)
120+
})
121+
122+
test('should handle useMemo hook', () => {
123+
const { use, flush, update } = useHook(({ value }) => useMemo(() => ({ value }), [value]))
124+
125+
const value1 = use({ value: 1 })
126+
127+
expect(value1).toEqual({ value: 1 })
128+
129+
const value2 = flush()
130+
131+
expect(value2).toEqual({ value: 1 })
132+
133+
expect(value2).toBe(value1)
134+
135+
const value3 = update({ value: 2 })
136+
137+
expect(value3).toEqual({ value: 2 })
138+
139+
expect(value3).not.toBe(value1)
140+
})
69141
})

0 commit comments

Comments
 (0)