From d82f90a768d02c13dbf20ccb6e08bf9f2fd3aa54 Mon Sep 17 00:00:00 2001 From: Sam <22425976+imsamdez@users.noreply.github.com> Date: Tue, 17 Sep 2024 12:59:53 +0200 Subject: [PATCH] fix(Jimo): prevent losing ref of client (#2392) * fix(Jimo): prevent losing ref of client * fix(Jimo): tests --- .../destinations/jimo/src/init-script.ts | 4 +- .../sendTrackEvent/__tests__/index.test.ts | 9 ++-- .../jimo/src/sendTrackEvent/index.ts | 13 +++--- .../src/sendUserData/__tests__/index.test.ts | 45 ++++++++++++------- .../jimo/src/sendUserData/index.ts | 12 +++-- .../destinations/jimo/src/types.ts | 2 +- 6 files changed, 53 insertions(+), 32 deletions(-) diff --git a/packages/browser-destinations/destinations/jimo/src/init-script.ts b/packages/browser-destinations/destinations/jimo/src/init-script.ts index 356f294cf9..9b247af4a2 100644 --- a/packages/browser-destinations/destinations/jimo/src/init-script.ts +++ b/packages/browser-destinations/destinations/jimo/src/init-script.ts @@ -11,7 +11,9 @@ export function initScript(settings: Settings) { window.jimo = [] window.segmentJimo = { initialized: !manualInit, - client: window.jimo + client: function () { + return window.jimo + } } window['JIMO_MANUAL_INIT'] = manualInit window['JIMO_PROJECT_ID'] = settings.projectId diff --git a/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/__tests__/index.test.ts b/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/__tests__/index.test.ts index d249c63793..4b2243da97 100644 --- a/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/__tests__/index.test.ts +++ b/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/__tests__/index.test.ts @@ -5,8 +5,11 @@ import { Payload } from '../generated-types' describe('Jimo - Send Track Event', () => { test('do:segmentio:track is called', async () => { + const mockedPush = jest.fn() const client = { - client: { push: jest.fn() } + client() { + return { push: mockedPush } + } } as any as JimoClient const context = new Context({ @@ -29,8 +32,8 @@ describe('Jimo - Send Track Event', () => { } as Payload }) - expect(client.client.push).toHaveBeenCalled() - expect(client.client.push).toHaveBeenCalledWith([ + expect(client.client().push).toHaveBeenCalled() + expect(client.client().push).toHaveBeenCalledWith([ 'do', 'segmentio:track', [ diff --git a/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/index.ts b/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/index.ts index ebf04e49b8..5c518b76eb 100644 --- a/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/index.ts +++ b/packages/browser-destinations/destinations/jimo/src/sendTrackEvent/index.ts @@ -68,11 +68,14 @@ const action: BrowserActionDefinition = { const { event_name, userId, anonymousId, timestamp, messageId, properties } = payload const receivedAt = timestamp - jimo.client.push([ - 'do', - 'segmentio:track', - [{ event: event_name, userId, anonymousId, messageId, timestamp, receivedAt, properties }] - ]) + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + jimo + .client() + .push([ + 'do', + 'segmentio:track', + [{ event: event_name, userId, anonymousId, messageId, timestamp, receivedAt, properties }] + ]) window.dispatchEvent(new CustomEvent(`jimo-segmentio-track:${event_name}`)) } } diff --git a/packages/browser-destinations/destinations/jimo/src/sendUserData/__tests__/index.test.ts b/packages/browser-destinations/destinations/jimo/src/sendUserData/__tests__/index.test.ts index 4bdf5f90c3..b0e7278cde 100644 --- a/packages/browser-destinations/destinations/jimo/src/sendUserData/__tests__/index.test.ts +++ b/packages/browser-destinations/destinations/jimo/src/sendUserData/__tests__/index.test.ts @@ -6,8 +6,11 @@ import { Payload } from '../generated-types' describe('Jimo - Send User Data', () => { test('user id', async () => { + const mockedPush = jest.fn() const segmentJimo = { - client: { push: jest.fn() } + client() { + return { push: mockedPush } + } } as any as JimoClient const context = new Context({ @@ -23,12 +26,15 @@ describe('Jimo - Send User Data', () => { } as Payload }) - expect(segmentJimo.client.push).toHaveBeenCalled() - expect(segmentJimo.client.push).toHaveBeenCalledWith(['do', 'identify', ['u1', expect.any(Function)]]) + expect(segmentJimo.client().push).toHaveBeenCalled() + expect(segmentJimo.client().push).toHaveBeenCalledWith(['do', 'identify', ['u1', expect.any(Function)]]) }) test('user id then email and attributes', async () => { + const mockedPush = jest.fn() const segmentJimo = { - client: { push: jest.fn() } + client() { + return { push: mockedPush } + } } as any as JimoClient const context = new Context({ @@ -48,12 +54,15 @@ describe('Jimo - Send User Data', () => { } as Payload }) - expect(segmentJimo.client.push).toHaveBeenCalled() - expect(segmentJimo.client.push).toHaveBeenCalledWith(['do', 'identify', ['u1', expect.any(Function)]]) + expect(segmentJimo.client().push).toHaveBeenCalled() + expect(segmentJimo.client().push).toHaveBeenCalledWith(['do', 'identify', ['u1', expect.any(Function)]]) }) test('user email', async () => { + const mockedPush = jest.fn() const segmentJimo = { - client: { push: jest.fn() } + client() { + return { push: mockedPush } + } } as any as JimoClient const context = new Context({ @@ -69,12 +78,15 @@ describe('Jimo - Send User Data', () => { } as Payload }) - expect(segmentJimo.client.push).toHaveBeenCalled() - expect(segmentJimo.client.push).toHaveBeenCalledWith(['set', 'user:email', ['foo@bar.com']]) + expect(segmentJimo.client().push).toHaveBeenCalled() + expect(segmentJimo.client().push).toHaveBeenCalledWith(['set', 'user:email', ['foo@bar.com']]) }) test('user traits', async () => { + const mockedPush = jest.fn() const segmentJimo = { - client: { push: jest.fn() } + client() { + return { push: mockedPush } + } } as any as JimoClient const context = new Context({ @@ -94,8 +106,8 @@ describe('Jimo - Send User Data', () => { } as Payload }) - expect(segmentJimo.client.push).toHaveBeenCalled() - expect(segmentJimo.client.push).toHaveBeenCalledWith([ + expect(segmentJimo.client().push).toHaveBeenCalled() + expect(segmentJimo.client().push).toHaveBeenCalledWith([ 'set', 'user:attributes', [ @@ -110,8 +122,11 @@ describe('Jimo - Send User Data', () => { ]) }) test('user traits with experience refetching', async () => { + const mockedPush = jest.fn() const segmentJimo = { - client: { push: jest.fn() } + client() { + return { push: mockedPush } + } } as any as JimoClient const context = new Context({ @@ -131,8 +146,8 @@ describe('Jimo - Send User Data', () => { } as Payload }) - expect(segmentJimo.client.push).toHaveBeenCalled() - expect(segmentJimo.client.push).toHaveBeenCalledWith([ + expect(segmentJimo.client().push).toHaveBeenCalled() + expect(segmentJimo.client().push).toHaveBeenCalledWith([ 'set', 'user:attributes', [ diff --git a/packages/browser-destinations/destinations/jimo/src/sendUserData/index.ts b/packages/browser-destinations/destinations/jimo/src/sendUserData/index.ts index 4e1827e520..3c9760990e 100644 --- a/packages/browser-destinations/destinations/jimo/src/sendUserData/index.ts +++ b/packages/browser-destinations/destinations/jimo/src/sendUserData/index.ts @@ -46,23 +46,21 @@ const action: BrowserActionDefinition = { const pushEmail = () => { if (payload.email == null) return // eslint-disable-next-line @typescript-eslint/no-unsafe-call - jimo.client.push(['set', 'user:email', [payload.email]]) + jimo.client().push(['set', 'user:email', [payload.email]]) } const pushTraits = () => { if (payload.traits == null) return // eslint-disable-next-line @typescript-eslint/no-unsafe-call - jimo.client.push([ - 'set', - 'user:attributes', - [payload.traits, settings.refetchExperiencesOnTraitsUpdate ?? false, true] - ]) + jimo + .client() + .push(['set', 'user:attributes', [payload.traits, settings.refetchExperiencesOnTraitsUpdate ?? false, true]]) } // If a userId is passed, we need to make sure email and attributes changes only happen in the // after the identify flow is done, that's why we pass it as a callback of the identify method if (payload.userId != null) { // eslint-disable-next-line @typescript-eslint/no-unsafe-call - jimo.client.push([ + jimo.client().push([ 'do', 'identify', [ diff --git a/packages/browser-destinations/destinations/jimo/src/types.ts b/packages/browser-destinations/destinations/jimo/src/types.ts index cf27a6f8c9..9ced6b0e37 100644 --- a/packages/browser-destinations/destinations/jimo/src/types.ts +++ b/packages/browser-destinations/destinations/jimo/src/types.ts @@ -4,5 +4,5 @@ export interface JimoSDK { export interface JimoClient { initialized: boolean - client: JimoSDK | any[] + client: () => JimoSDK | any[] }