1+ const React = require ( 'react' ) ;
2+ const renderer = require ( 'react-test-renderer' ) ;
13const SharedState = require ( '../../lib/shared-state' ) ;
24
35test ( 'setState() is assign state and call setters.' , ( ) => {
@@ -17,9 +19,88 @@ test('setState() is assign state and call setters.', () => {
1719 expect ( shareState . setters [ 1 ] . set ) . toBeCalledWith ( { test : true } ) ;
1820} ) ;
1921
20- test ( 'Setter it was generated execute without path.' , ( ) => {
22+ test ( 'Setter call setState() without path.' , ( ) => {
2123 const shareState = new SharedState ( ) ;
2224 const setState = shareState . generateSetState ( ) ;
25+ jest . spyOn ( shareState , 'setState' ) ;
2326 setState ( { test : true } ) ;
2427 expect ( shareState . state ) . toMatchSnapshot ( ) ;
28+ expect ( shareState . setState ) . toBeCalledWith ( { test : true } ) ;
29+ } ) ;
30+
31+ test ( 'Setter assign a partial state and call setters with path.' , ( ) => {
32+ const shareState = new SharedState ( ) ;
33+ const setState = shareState . generateSetState ( 'test' ) ;
34+ shareState . setters = [
35+ {
36+ path : 'test' ,
37+ set : jest . fn ( )
38+ } ,
39+ {
40+ set : jest . fn ( )
41+ }
42+ ] ;
43+ setState ( true ) ;
44+ expect ( shareState . state ) . toMatchSnapshot ( ) ;
45+ expect ( shareState . setters [ 0 ] . set ) . toBeCalledWith ( true ) ;
46+ expect ( shareState . setters [ 1 ] . set ) . toBeCalledWith ( { test : true } ) ;
47+ } ) ;
48+
49+ test ( 'Call useState() to get the state and the setter.' , ( ) => {
50+ const shareState = new SharedState ( ) ;
51+ jest . spyOn ( shareState , 'useState' ) ;
52+ shareState . setState ( { test : true } ) ;
53+ const component = ( ) => {
54+ const [ state , setState ] = shareState . useState ( ) ;
55+ expect ( state ) . toMatchSnapshot ( ) ;
56+ if ( state . test ) {
57+ setState ( { test : false } ) ;
58+ }
59+
60+ return React . createElement ( 'div' , null ) ;
61+ } ;
62+
63+ renderer . create ( React . createElement ( component , null ) ) ;
64+ expect ( shareState . useState ) . toBeCalledTimes ( 2 ) ;
65+ } ) ;
66+
67+ test ( 'Call useState() with a initial state to get the state.' , ( ) => {
68+ const shareState = new SharedState ( ) ;
69+ jest . spyOn ( shareState , 'useState' ) ;
70+ const component = ( ) => {
71+ const [ state ] = shareState . useState ( null , { test : true } ) ;
72+ expect ( state ) . toMatchSnapshot ( ) ;
73+ return React . createElement ( 'div' , null ) ;
74+ } ;
75+
76+ renderer . create ( React . createElement ( component , null ) ) ;
77+ expect ( shareState . useState ) . toBeCalled ( ) ;
78+ } ) ;
79+
80+ test ( 'Call useState() with a path and a initial partial state to get the state.' , ( ) => {
81+ const shareState = new SharedState ( ) ;
82+ jest . spyOn ( shareState , 'useState' ) ;
83+ const component = ( ) => {
84+ const [ state ] = shareState . useState ( 'test' , true ) ;
85+ expect ( state ) . toMatchSnapshot ( ) ;
86+ return React . createElement ( 'div' , null ) ;
87+ } ;
88+
89+ renderer . create ( React . createElement ( component , null ) ) ;
90+ expect ( shareState . useState ) . toBeCalled ( ) ;
91+ } ) ;
92+
93+ test ( 'Call useState() twice to make sure it did not assign setter again.' , ( ) => {
94+ const shareState = new SharedState ( ) ;
95+ jest . spyOn ( shareState , 'useState' ) ;
96+ shareState . setState ( { test : true } ) ;
97+ const component = ( ) => {
98+ const [ state ] = shareState . useState ( ) ;
99+ expect ( state ) . toMatchSnapshot ( ) ;
100+ return React . createElement ( 'div' , null ) ;
101+ } ;
102+
103+ renderer . create ( React . createElement ( component , null ) ) ;
104+ renderer . create ( React . createElement ( component , null ) ) ;
105+ expect ( shareState . useState ) . toBeCalled ( ) ;
25106} ) ;
0 commit comments