@@ -7,24 +7,23 @@ describe('Session', () => {
77 it ( 'initializes with the proper defaults' , ( ) => {
88 const session = new Session ( ) . toJSON ( ) ;
99
10- const sessionStartTime = session . timestamp ;
11-
10+ // Grab current year to check if we are converting from sec -> ms correctly
11+ const currentYear = new Date ( timestampInSeconds ( ) * 1000 ) . toISOString ( ) . slice ( 0 , 4 ) ;
1212 expect ( session ) . toEqual ( {
1313 attrs : { } ,
1414 duration : 0 ,
1515 errors : 0 ,
1616 init : true ,
1717 sid : expect . any ( String ) ,
18- started : expect . any ( String ) ,
18+ started : expect . stringMatching ( currentYear ) ,
1919 status : SessionStatus . Ok ,
20- timestamp : expect . any ( String ) ,
20+ timestamp : expect . stringMatching ( currentYear ) ,
2121 } ) ;
2222
2323 expect ( session . sid ) . toHaveLength ( 32 ) ;
2424
2525 // started and timestamp should be the same on creation
26- expect ( session . started ) . toEqual ( sessionStartTime ) ;
27- expect ( session . timestamp ) . toEqual ( sessionStartTime ) ;
26+ expect ( session . started ) . toEqual ( session . timestamp ) ;
2827 } ) ;
2928
3029 describe ( 'update' , ( ) => {
@@ -41,13 +40,16 @@ describe('Session', () => {
4140 [ 'sets an did' , { did : 'specialID123' } , { did : 'specialID123' } ] ,
4241 [ 'overwrites user did with custom did' , { did : 'custom-did' , user : { id : 'user-id' } } , { did : 'custom-did' } ] ,
4342 [ 'sets a started time' , { started : time } , { started : new Date ( time * 1000 ) . toISOString ( ) } ] ,
44- [ 'does not set a duration for browser env' , { isBrowser : true } , { duration : undefined } ] ,
43+ [ 'does not set a duration for browser env' , { ignoreDuration : true } , { duration : undefined } ] ,
4544 [ 'sets a duration' , { duration : 12000 } , { duration : 12000 } ] ,
46- [ 'does not use custom duration for browser env' , { duration : 12000 , isBrowser : true } , { duration : undefined } ] ,
45+ [
46+ 'does not use custom duration for browser env' ,
47+ { duration : 12000 , ignoreDuration : true } ,
48+ { duration : undefined } ,
49+ ] ,
4750 [
4851 'does not set a negative duration' ,
4952 { timestamp : 10 , started : 100 } ,
50- // TODO(abhi): What should the behaviour here be?
5153 { duration : 0 , timestamp : expect . any ( String ) , started : expect . any ( String ) } ,
5254 ] ,
5355 [
@@ -88,30 +90,26 @@ describe('Session', () => {
8890 describe ( 'close' , ( ) => {
8991 it ( 'exits a normal session' , ( ) => {
9092 const session = new Session ( ) ;
91- const mockUpdate = jest . spyOn ( session , 'update' ) ;
92- expect ( mockUpdate ) . toHaveBeenCalledTimes ( 0 ) ;
93+ expect ( session . status ) . toEqual ( SessionStatus . Ok ) ;
9394 session . close ( ) ;
94- expect ( mockUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
95- expect ( mockUpdate ) . toHaveBeenLastCalledWith ( { status : SessionStatus . Exited } ) ;
95+ expect ( session . status ) . toEqual ( SessionStatus . Exited ) ;
9696 } ) ;
9797
9898 it ( 'updates session status when give status' , ( ) => {
9999 const session = new Session ( ) ;
100- const mockUpdate = jest . spyOn ( session , 'update' ) ;
100+ expect ( session . status ) . toEqual ( SessionStatus . Ok ) ;
101101
102- session . close ( SessionStatus . Crashed ) ;
103- expect ( mockUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
104- expect ( mockUpdate ) . toHaveBeenLastCalledWith ( { status : SessionStatus . Crashed } ) ;
102+ session . close ( SessionStatus . Abnormal ) ;
103+ expect ( session . status ) . toEqual ( SessionStatus . Abnormal ) ;
105104 } ) ;
106105
107- it ( 'only changes status ok' , ( ) => {
106+ it ( 'only changes status ok to exited ' , ( ) => {
108107 const session = new Session ( ) ;
109- session . status = SessionStatus . Abnormal ;
110- const mockUpdate = jest . spyOn ( session , 'update' ) ;
108+ session . update ( { status : SessionStatus . Crashed } ) ;
109+ expect ( session . status ) . toEqual ( SessionStatus . Crashed ) ;
111110
112111 session . close ( ) ;
113- expect ( mockUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
114- expect ( mockUpdate ) . toHaveBeenLastCalledWith ( ) ;
112+ expect ( session . status ) . toEqual ( SessionStatus . Crashed ) ;
115113 } ) ;
116114 } ) ;
117115} ) ;
0 commit comments