@@ -60,6 +60,100 @@ describe('API functions', () => {
6060 } ) ;
6161 } ) ;
6262
63+ describe ( 'checkAuth' , ( ) => {
64+ let nextState ;
65+ let replace ;
66+ let next ;
67+ beforeEach ( ( ) => {
68+ nextState = { location : { pathname : 'test' } } ;
69+ replace = jest . fn ( ) ;
70+ next = jest . fn ( ) ;
71+ } ) ;
72+
73+ describe ( 'with logged user' , ( ) => {
74+ beforeEach ( ( ) => {
75+ __setUser ( user ) ;
76+ __setSession ( session ) ;
77+ } ) ;
78+
79+ test ( 'does call next function' , ( done ) => {
80+ const next = jest . fn ( ( ) => {
81+ expect ( next ) . toHaveBeenCalled ( ) ;
82+ done ( ) ;
83+ } ) ;
84+ sessionService . checkAuth ( nextState , replace , next ) ;
85+ } ) ;
86+
87+ describe ( 'with option refreshOnCheckAuth enable' , ( ) => {
88+ test ( 'change authenticated flag to true and save the user' , ( done ) => {
89+ sessionService . setOptions ( store , true ) ;
90+ // wait for change the redux store
91+ const unsubscribe = store . subscribe ( ( ) => {
92+ const state = store . getState ( ) ;
93+ expect ( state . authenticated ) . toEqual ( true ) ;
94+ // wait to change the user
95+ const { user } = state ;
96+ if ( ! ( Object . keys ( user ) . length === 0 && user . constructor === Object ) ) {
97+ expect ( state . user ) . toMatchObject ( user ) ;
98+ unsubscribe ( ) ;
99+ done ( ) ;
100+ }
101+ } ) ;
102+
103+ sessionService . checkAuth ( nextState , replace , next ) ;
104+ } ) ;
105+ } ) ;
106+ } ) ;
107+
108+ describe ( 'without logged user' , ( ) => {
109+ beforeEach ( ( ) => {
110+ __setUser ( undefined ) ;
111+ __setSession ( undefined ) ;
112+ sessionService . setOptions ( store , false , 'redirectionPath' ) ;
113+ } ) ;
114+
115+ test ( 'does call replace function' , ( done ) => {
116+ const replace = jest . fn ( ( ) => {
117+ expect ( replace ) . toHaveBeenCalled ( ) ;
118+ done ( ) ;
119+ } ) ;
120+ sessionService . checkAuth ( nextState , replace , next ) ;
121+ } ) ;
122+
123+ test ( 'does redirect to the redirectPath' , ( done ) => {
124+ const expectedArg = {
125+ pathname : 'redirectionPath' ,
126+ state : { nextPathname : nextState . location . pathname }
127+ } ;
128+ const replace = jest . fn ( ( ) => {
129+ expect ( replace ) . toHaveBeenCalledWith ( expectedArg ) ;
130+ done ( ) ;
131+ } ) ;
132+ sessionService . checkAuth ( nextState , replace , next ) ;
133+ } ) ;
134+
135+ describe ( 'with option refreshOnCheckAuth enable' , ( ) => {
136+ test ( 'change authenticated flag to false and the user to empty object' , ( done ) => {
137+ sessionService . setOptions ( store , true ) ;
138+ // wait for change the redux store
139+ const unsubscribe = store . subscribe ( ( ) => {
140+ const state = store . getState ( ) ;
141+ expect ( state . authenticated ) . toEqual ( false ) ;
142+ // wait to empty the user
143+ const { user } = state ;
144+ if ( ( Object . keys ( user ) . length === 0 && user . constructor === Object ) ) {
145+ expect ( state . user ) . toEqual ( { } ) ;
146+ unsubscribe ( ) ;
147+ done ( ) ;
148+ }
149+ } ) ;
150+
151+ sessionService . checkAuth ( nextState , replace , next ) ;
152+ } ) ;
153+ } ) ;
154+ } ) ;
155+ } ) ;
156+
63157 describe ( 'saveSession' , ( ) => {
64158 describe ( 'localforage returns success' , ( ) => {
65159 test ( 'change authenticated flag to true value' , ( done ) => {
0 commit comments