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'
210import { useHook , cleanup } from 'src'
311
412describe ( '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