11import Assignment from './assignment' ;
22import { persistAssignmentOverride } from './assignmentOverride' ;
33import Cookies from 'js-cookie' ;
4- import Session from './session' ;
4+ import { createSession , type Session } from './session' ;
55import Visitor from './visitor' ;
66import type { AnalyticsProvider } from './analyticsProvider' ;
77import type { RawConfig } from './config' ;
@@ -21,17 +21,13 @@ const rawConfig: RawConfig = {
2121 }
2222} ;
2323
24- vi . mock ( './assignmentOverride' , ( ) => {
25- return {
26- persistAssignmentOverride : vi . fn ( )
27- } ;
28- } ) ;
24+ vi . mock ( './assignmentOverride' ) ;
2925vi . mock ( 'js-cookie' ) ;
3026vi . mock ( 'uuid' ) ;
3127
3228const mockPersistAssignmentOverride = vi . mocked ( persistAssignmentOverride ) ;
3329
34- describe ( 'Session ' , ( ) => {
30+ describe ( 'createSession ' , ( ) => {
3531 beforeAll ( ( ) => {
3632 window . TT = btoa ( JSON . stringify ( rawConfig ) ) ;
3733 } ) ;
@@ -43,7 +39,7 @@ describe('Session', () => {
4339
4440 describe ( 'Cookie behavior' , ( ) => {
4541 it ( 'reads the visitor id from a cookie and sets it back in the cookie' , async ( ) => {
46- await new Session ( ) . getPublicAPI ( ) . initialize ( { } ) ;
42+ await createSession ( ) . initialize ( { } ) ;
4743 expect ( Cookies . get ) . toHaveBeenCalledTimes ( 1 ) ;
4844 expect ( Cookies . get ) . toHaveBeenCalledWith ( 'custom_cookie_name' ) ;
4945 expect ( Cookies . set ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -60,7 +56,7 @@ describe('Session', () => {
6056 // @ts -expect-error uuid mock return type
6157 vi . mocked ( uuid ) . mockReturnValue ( 'generated_visitor_id' ) ;
6258
63- await new Session ( ) . getPublicAPI ( ) . initialize ( { } ) ;
59+ await createSession ( ) . initialize ( { } ) ;
6460 expect ( Cookies . get ) . toHaveBeenCalledTimes ( 1 ) ;
6561 expect ( Cookies . get ) . toHaveBeenCalledWith ( 'custom_cookie_name' ) ;
6662 expect ( Cookies . set ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -77,15 +73,15 @@ describe('Session', () => {
7773
7874 beforeEach ( async ( ) => {
7975 window . TT = btoa ( JSON . stringify ( rawConfig ) ) ;
80- session = new Session ( ) ;
76+ session = createSession ( ) ;
8177 await session . initialize ( { } ) ;
8278 vi . mocked ( Cookies . set ) . mockClear ( ) ;
8379 } ) ;
8480
8581 describe ( '#initialize()' , ( ) => {
8682 it ( 'calls notifyUnsyncedAssignments when a visitor is loaded' , async ( ) => {
8783 const notifySpy = vi . spyOn ( Visitor . prototype , 'notifyUnsyncedAssignments' ) ;
88- await new Session ( ) . getPublicAPI ( ) . initialize ( { } ) ;
84+ await createSession ( ) . initialize ( { } ) ;
8985 expect ( notifySpy ) . toHaveBeenCalledTimes ( 1 ) ;
9086 } ) ;
9187
@@ -98,7 +94,7 @@ describe('Session', () => {
9894
9995 const setAnalyticsSpy = vi . spyOn ( Visitor . prototype , 'setAnalytics' ) ;
10096
101- await new Session ( ) . getPublicAPI ( ) . initialize ( { analytics } ) ;
97+ await createSession ( ) . initialize ( { analytics } ) ;
10298 expect ( setAnalyticsSpy ) . toHaveBeenCalledTimes ( 1 ) ;
10399 expect ( setAnalyticsSpy ) . toHaveBeenCalledWith ( analytics ) ;
104100 } ) ;
@@ -107,7 +103,7 @@ describe('Session', () => {
107103 const errorLogger = function ( ) { } ;
108104 const setErrorLoggerSpy = vi . spyOn ( Visitor . prototype , 'setErrorLogger' ) ;
109105
110- await new Session ( ) . getPublicAPI ( ) . initialize ( { errorLogger : errorLogger } ) ;
106+ await createSession ( ) . initialize ( { errorLogger : errorLogger } ) ;
111107 expect ( setErrorLoggerSpy ) . toHaveBeenCalledTimes ( 1 ) ;
112108 expect ( setErrorLoggerSpy ) . toHaveBeenCalledWith ( errorLogger ) ;
113109 } ) ;
@@ -138,7 +134,7 @@ describe('Session', () => {
138134 } ) ;
139135
140136 it ( 'calls analytics.identify with the resolved visitor id' , async ( ) => {
141- const testSession = new Session ( ) ;
137+ const testSession = createSession ( ) ;
142138 const visitor = await testSession . initialize ( { } ) ;
143139 const identifySpy = vi . spyOn ( visitor . analytics , 'identify' ) ;
144140
@@ -173,7 +169,7 @@ describe('Session', () => {
173169 } ) ;
174170
175171 it ( 'calls analytics.alias with the resolved visitor id' , async ( ) => {
176- const testSession = new Session ( ) ;
172+ const testSession = createSession ( ) ;
177173 const visitor = await testSession . initialize ( { } ) ;
178174 const aliasSpy = vi . spyOn ( visitor . analytics , 'alias' ) ;
179175
@@ -214,65 +210,50 @@ describe('Session', () => {
214210 } ) ;
215211 } ) ;
216212
217- describe ( '#getPublicAPI()' , ( ) => {
218- let publicApi : ReturnType < Session [ 'getPublicAPI' ] > ;
219-
220- beforeEach ( ( ) => {
221- session = new Session ( ) ;
222- session . initialize ( { } ) ;
223- publicApi = session . getPublicAPI ( ) ;
224- } ) ;
225-
226- it ( 'returns an object with a limited set of methods' , ( ) => {
227- expect ( publicApi ) . toEqual (
228- expect . objectContaining ( {
229- vary : expect . any ( Function ) ,
230- ab : expect . any ( Function ) ,
231- logIn : expect . any ( Function ) ,
232- signUp : expect . any ( Function ) ,
233- initialize : expect . any ( Function )
234- } )
235- ) ;
236-
237- expect ( publicApi . _crx ) . toEqual (
238- expect . objectContaining ( {
239- loadInfo : expect . any ( Function ) ,
240- persistAssignment : expect . any ( Function )
241- } )
242- ) ;
213+ it ( 'returns an object with a limited set of methods' , ( ) => {
214+ expect ( session ) . toEqual ( {
215+ vary : expect . any ( Function ) ,
216+ ab : expect . any ( Function ) ,
217+ logIn : expect . any ( Function ) ,
218+ signUp : expect . any ( Function ) ,
219+ initialize : expect . any ( Function ) ,
220+ _crx : {
221+ loadInfo : expect . any ( Function ) ,
222+ persistAssignment : expect . any ( Function )
223+ }
243224 } ) ;
225+ } ) ;
244226
245- describe ( '_crx' , ( ) => {
246- describe ( '#persistAssignment()' , ( ) => {
247- it ( 'calls persistAssignmentOverride with the correct parameters' , async ( ) => {
248- mockPersistAssignmentOverride . mockResolvedValue ( ) ;
249-
250- await publicApi . _crx . persistAssignment ( 'split' , 'variant' , 'the_username' , 'the_password' ) ;
251- expect ( mockPersistAssignmentOverride ) . toHaveBeenCalledTimes ( 1 ) ;
252- expect ( mockPersistAssignmentOverride ) . toHaveBeenCalledWith ( {
253- visitor : expect . any ( Visitor ) ,
254- username : 'the_username' ,
255- password : 'the_password' ,
256- assignment : new Assignment ( {
257- splitName : 'split' ,
258- variant : 'variant' ,
259- context : 'chrome_extension' ,
260- isUnsynced : true
261- } )
262- } ) ;
227+ describe ( '_crx' , ( ) => {
228+ describe ( '#persistAssignment()' , ( ) => {
229+ it ( 'calls persistAssignmentOverride with the correct parameters' , async ( ) => {
230+ mockPersistAssignmentOverride . mockResolvedValue ( ) ;
231+
232+ await session . _crx . persistAssignment ( 'split' , 'variant' , 'the_username' , 'the_password' ) ;
233+ expect ( mockPersistAssignmentOverride ) . toHaveBeenCalledTimes ( 1 ) ;
234+ expect ( mockPersistAssignmentOverride ) . toHaveBeenCalledWith ( {
235+ visitor : expect . any ( Visitor ) ,
236+ username : 'the_username' ,
237+ password : 'the_password' ,
238+ assignment : new Assignment ( {
239+ splitName : 'split' ,
240+ variant : 'variant' ,
241+ context : 'chrome_extension' ,
242+ isUnsynced : true
243+ } )
263244 } ) ;
264245 } ) ;
246+ } ) ;
265247
266- describe ( '#loadInfo()' , ( ) => {
267- it ( 'returns a promise that resolves with the split registry, assignment registry and visitor id' , async ( ) => {
268- const info = await publicApi . _crx . loadInfo ( ) ;
269- expect ( info . visitorId ) . toEqual ( 'existing_visitor_id' ) ;
270- expect ( info . splitRegistry ) . toEqual ( {
271- jabba : { cgi : 50 , puppet : 50 } ,
272- wine : { red : 50 , white : 25 , rose : 25 }
273- } ) ;
274- expect ( info . assignmentRegistry ) . toEqual ( { jabba : 'puppet' , wine : 'rose' } ) ;
248+ describe ( '#loadInfo()' , ( ) => {
249+ it ( 'returns a promise that resolves with the split registry, assignment registry and visitor id' , async ( ) => {
250+ const info = await session . _crx . loadInfo ( ) ;
251+ expect ( info . visitorId ) . toEqual ( 'existing_visitor_id' ) ;
252+ expect ( info . splitRegistry ) . toEqual ( {
253+ jabba : { cgi : 50 , puppet : 50 } ,
254+ wine : { red : 50 , white : 25 , rose : 25 }
275255 } ) ;
256+ expect ( info . assignmentRegistry ) . toEqual ( { jabba : 'puppet' , wine : 'rose' } ) ;
276257 } ) ;
277258 } ) ;
278259 } ) ;
0 commit comments