Skip to content

Commit

Permalink
fix(Jimo): prevent losing ref of client (#2392)
Browse files Browse the repository at this point in the history
* fix(Jimo): prevent losing ref of client

* fix(Jimo): tests
  • Loading branch information
imsamdez authored Sep 17, 2024
1 parent acd3145 commit d82f90a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ const action: BrowserActionDefinition<Settings, JimoClient, Payload> = {
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}`))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -69,12 +78,15 @@ describe('Jimo - Send User Data', () => {
} as Payload
})

expect(segmentJimo.client.push).toHaveBeenCalled()
expect(segmentJimo.client.push).toHaveBeenCalledWith(['set', 'user:email', ['[email protected]']])
expect(segmentJimo.client().push).toHaveBeenCalled()
expect(segmentJimo.client().push).toHaveBeenCalledWith(['set', 'user:email', ['[email protected]']])
})
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({
Expand All @@ -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',
[
Expand All @@ -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({
Expand All @@ -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',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,21 @@ const action: BrowserActionDefinition<Settings, JimoClient, Payload> = {
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',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export interface JimoSDK {

export interface JimoClient {
initialized: boolean
client: JimoSDK | any[]
client: () => JimoSDK | any[]
}

0 comments on commit d82f90a

Please sign in to comment.