Skip to content

Commit 3391ad4

Browse files
committed
Convert Session to factory function
1 parent 81b7d82 commit 3391ad4

File tree

4 files changed

+126
-169
lines changed

4 files changed

+126
-169
lines changed

src/analyticsProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Assignment from './assignment';
1+
import type Assignment from './assignment';
22

33
export interface AnalyticsProvider {
44
trackAssignment(visitorId: string, assignment: Assignment, callback: (value: boolean) => void): void;

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import Session from './session';
1+
import { createSession } from './session';
22
export type { AnalyticsProvider } from './analyticsProvider';
33

4-
const TestTrack = new Session().getPublicAPI();
4+
const TestTrack = createSession();
5+
56
const notifyListener = function () {
67
window.dispatchEvent(
78
new CustomEvent('tt:lib:loaded', {

src/session.test.ts

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Assignment from './assignment';
22
import { persistAssignmentOverride } from './assignmentOverride';
33
import Cookies from 'js-cookie';
4-
import Session from './session';
4+
import { createSession, type Session } from './session';
55
import Visitor from './visitor';
66
import type { AnalyticsProvider } from './analyticsProvider';
77
import type { RawConfig } from './config';
@@ -31,7 +31,7 @@ vi.mock('uuid');
3131

3232
const mockPersistAssignmentOverride = vi.mocked(persistAssignmentOverride);
3333

34-
describe('Session', () => {
34+
describe('createSession', () => {
3535
beforeAll(() => {
3636
window.TT = btoa(JSON.stringify(rawConfig));
3737
});
@@ -43,7 +43,7 @@ describe('Session', () => {
4343

4444
describe('Cookie behavior', () => {
4545
it('reads the visitor id from a cookie and sets it back in the cookie', async () => {
46-
await new Session().getPublicAPI().initialize({});
46+
await createSession().initialize({});
4747
expect(Cookies.get).toHaveBeenCalledTimes(1);
4848
expect(Cookies.get).toHaveBeenCalledWith('custom_cookie_name');
4949
expect(Cookies.set).toHaveBeenCalledTimes(1);
@@ -60,7 +60,7 @@ describe('Session', () => {
6060
// @ts-expect-error uuid mock return type
6161
vi.mocked(uuid).mockReturnValue('generated_visitor_id');
6262

63-
await new Session().getPublicAPI().initialize({});
63+
await createSession().initialize({});
6464
expect(Cookies.get).toHaveBeenCalledTimes(1);
6565
expect(Cookies.get).toHaveBeenCalledWith('custom_cookie_name');
6666
expect(Cookies.set).toHaveBeenCalledTimes(1);
@@ -77,15 +77,15 @@ describe('Session', () => {
7777

7878
beforeEach(async () => {
7979
window.TT = btoa(JSON.stringify(rawConfig));
80-
session = new Session();
80+
session = createSession();
8181
await session.initialize({});
8282
vi.mocked(Cookies.set).mockClear();
8383
});
8484

8585
describe('#initialize()', () => {
8686
it('calls notifyUnsyncedAssignments when a visitor is loaded', async () => {
8787
const notifySpy = vi.spyOn(Visitor.prototype, 'notifyUnsyncedAssignments');
88-
await new Session().getPublicAPI().initialize({});
88+
await createSession().initialize({});
8989
expect(notifySpy).toHaveBeenCalledTimes(1);
9090
});
9191

@@ -98,7 +98,7 @@ describe('Session', () => {
9898

9999
const setAnalyticsSpy = vi.spyOn(Visitor.prototype, 'setAnalytics');
100100

101-
await new Session().getPublicAPI().initialize({ analytics });
101+
await createSession().initialize({ analytics });
102102
expect(setAnalyticsSpy).toHaveBeenCalledTimes(1);
103103
expect(setAnalyticsSpy).toHaveBeenCalledWith(analytics);
104104
});
@@ -107,7 +107,7 @@ describe('Session', () => {
107107
const errorLogger = function () {};
108108
const setErrorLoggerSpy = vi.spyOn(Visitor.prototype, 'setErrorLogger');
109109

110-
await new Session().getPublicAPI().initialize({ errorLogger: errorLogger });
110+
await createSession().initialize({ errorLogger: errorLogger });
111111
expect(setErrorLoggerSpy).toHaveBeenCalledTimes(1);
112112
expect(setErrorLoggerSpy).toHaveBeenCalledWith(errorLogger);
113113
});
@@ -119,7 +119,7 @@ describe('Session', () => {
119119
// @ts-expect-error uuid returns different types depending on arguments
120120
vi.mocked(uuid).mockReturnValue('new_visitor_id');
121121

122-
const visitor = await new Session().getPublicAPI().initialize({
122+
const visitor = await createSession().initialize({
123123
config: {
124124
url: 'http://localhost:3000',
125125
cookieDomain: 'localhost',
@@ -156,7 +156,7 @@ describe('Session', () => {
156156
});
157157

158158
it('calls analytics.identify with the resolved visitor id', async () => {
159-
const testSession = new Session();
159+
const testSession = createSession();
160160
const visitor = await testSession.initialize({});
161161
const identifySpy = vi.spyOn(visitor.analytics, 'identify');
162162

@@ -191,7 +191,7 @@ describe('Session', () => {
191191
});
192192

193193
it('calls analytics.alias with the resolved visitor id', async () => {
194-
const testSession = new Session();
194+
const testSession = createSession();
195195
const visitor = await testSession.initialize({});
196196
const aliasSpy = vi.spyOn(visitor.analytics, 'alias');
197197

@@ -232,65 +232,50 @@ describe('Session', () => {
232232
});
233233
});
234234

235-
describe('#getPublicAPI()', () => {
236-
let publicApi: ReturnType<Session['getPublicAPI']>;
237-
238-
beforeEach(() => {
239-
session = new Session();
240-
session.initialize({});
241-
publicApi = session.getPublicAPI();
242-
});
243-
244-
it('returns an object with a limited set of methods', () => {
245-
expect(publicApi).toEqual(
246-
expect.objectContaining({
247-
vary: expect.any(Function),
248-
ab: expect.any(Function),
249-
logIn: expect.any(Function),
250-
signUp: expect.any(Function),
251-
initialize: expect.any(Function)
252-
})
253-
);
254-
255-
expect(publicApi._crx).toEqual(
256-
expect.objectContaining({
257-
loadInfo: expect.any(Function),
258-
persistAssignment: expect.any(Function)
259-
})
260-
);
235+
it('returns an object with a limited set of methods', () => {
236+
expect(session).toEqual({
237+
vary: expect.any(Function),
238+
ab: expect.any(Function),
239+
logIn: expect.any(Function),
240+
signUp: expect.any(Function),
241+
initialize: expect.any(Function),
242+
_crx: {
243+
loadInfo: expect.any(Function),
244+
persistAssignment: expect.any(Function)
245+
}
261246
});
247+
});
262248

263-
describe('_crx', () => {
264-
describe('#persistAssignment()', () => {
265-
it('calls persistAssignmentOverride with the correct parameters', async () => {
266-
mockPersistAssignmentOverride.mockResolvedValue();
267-
268-
await publicApi._crx.persistAssignment('split', 'variant', 'the_username', 'the_password');
269-
expect(mockPersistAssignmentOverride).toHaveBeenCalledTimes(1);
270-
expect(mockPersistAssignmentOverride).toHaveBeenCalledWith({
271-
visitor: expect.any(Visitor),
272-
username: 'the_username',
273-
password: 'the_password',
274-
assignment: new Assignment({
275-
splitName: 'split',
276-
variant: 'variant',
277-
context: 'chrome_extension',
278-
isUnsynced: true
279-
})
280-
});
249+
describe('_crx', () => {
250+
describe('#persistAssignment()', () => {
251+
it('calls persistAssignmentOverride with the correct parameters', async () => {
252+
mockPersistAssignmentOverride.mockResolvedValue();
253+
254+
await session._crx.persistAssignment('split', 'variant', 'the_username', 'the_password');
255+
expect(mockPersistAssignmentOverride).toHaveBeenCalledTimes(1);
256+
expect(mockPersistAssignmentOverride).toHaveBeenCalledWith({
257+
visitor: expect.any(Visitor),
258+
username: 'the_username',
259+
password: 'the_password',
260+
assignment: new Assignment({
261+
splitName: 'split',
262+
variant: 'variant',
263+
context: 'chrome_extension',
264+
isUnsynced: true
265+
})
281266
});
282267
});
268+
});
283269

284-
describe('#loadInfo()', () => {
285-
it('returns a promise that resolves with the split registry, assignment registry and visitor id', async () => {
286-
const info = await publicApi._crx.loadInfo();
287-
expect(info.visitorId).toEqual('existing_visitor_id');
288-
expect(info.splitRegistry).toEqual({
289-
jabba: { cgi: 50, puppet: 50 },
290-
wine: { red: 50, white: 25, rose: 25 }
291-
});
292-
expect(info.assignmentRegistry).toEqual({ jabba: 'puppet', wine: 'rose' });
270+
describe('#loadInfo()', () => {
271+
it('returns a promise that resolves with the split registry, assignment registry and visitor id', async () => {
272+
const info = await session._crx.loadInfo();
273+
expect(info.visitorId).toEqual('existing_visitor_id');
274+
expect(info.splitRegistry).toEqual({
275+
jabba: { cgi: 50, puppet: 50 },
276+
wine: { red: 50, white: 25, rose: 25 }
293277
});
278+
expect(info.assignmentRegistry).toEqual({ jabba: 'puppet', wine: 'rose' });
294279
});
295280
});
296281
});

0 commit comments

Comments
 (0)