Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zihejia committed Oct 27, 2022
1 parent a39250b commit 2013e44
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@ describe('Mixpanel.identifyUser', () => {
const event = createTestEvent({
timestamp, traits: {
firstName: 'Joe',
lastName: 'Doe',
lastName: 'Doe'
}
})

const event2 = createTestEvent({
timestamp, traits: {
firstName: 'Joe'
}
})

const event3 = createTestEvent({
timestamp, traits: {
lastName: 'Doe'
}
})

Expand All @@ -98,14 +110,62 @@ describe('Mixpanel.identifyUser', () => {
data: JSON.stringify({
$token: MIXPANEL_PROJECT_TOKEN,
$distinct_id: 'user1234',
$ip: '8.8.8.8',
$set: {
$first_name: 'Joe',
$last_name: 'Doe',
$name: 'Joe Doe',
$name: 'Joe Doe'
}
})
})
)
nock('https://api.mixpanel.com').post('/engage').reply(200, {})
nock('https://api.mixpanel.com').post('/track').reply(200, {})
const responses2 = await testDestination.testAction('identifyUser', {
event: event2,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.US
}
})
expect(responses2[1].options.body).toMatchObject(
new URLSearchParams({
data: JSON.stringify({
$token: MIXPANEL_PROJECT_TOKEN,
$distinct_id: 'user1234',
$ip: '8.8.8.8',
$set: {
$first_name: 'Joe'
}
})
})
)
nock('https://api.mixpanel.com').post('/engage').reply(200, {})
nock('https://api.mixpanel.com').post('/track').reply(200, {})
const responses3 = await testDestination.testAction('identifyUser', {
event: event3,
useDefaultMappings: true,
settings: {
projectToken: MIXPANEL_PROJECT_TOKEN,
apiSecret: MIXPANEL_API_SECRET,
apiRegion: ApiRegions.US
}
})
expect(responses3[1].options.body).toMatchObject(
new URLSearchParams({
data: JSON.stringify({
$token: MIXPANEL_PROJECT_TOKEN,
$distinct_id: 'user1234',
$ip: '8.8.8.8',
$set: {
$last_name: 'Doe'
}
})
})
)

})

it('should use EU server URL', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ActionDefinition, IntegrationError, omit } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'

import { getApiServerUrl, getName } from '../utils'
import { getApiServerUrl, getConcatenatedName } from '../utils'

const action: ActionDefinition<Settings, Payload> = {
title: 'Identify User',
Expand Down Expand Up @@ -73,18 +73,19 @@ const action: ActionDefinition<Settings, Payload> = {
}

if (payload.traits && Object.keys(payload.traits).length > 0) {
const concatenatedName = getConcatenatedName(
payload.traits.firstName,
payload.traits.lastName,
payload.traits.name
)
const traits = {
...omit(payload.traits, ['created', 'email', 'firstName', 'lastName', 'name', 'username', 'phone']),
// to fit the Mixpanel expectations, transform the special traits to Mixpanel reserved property
$created: payload.traits.created,
$email: payload.traits.email,
$first_name: payload.traits.firstName,
$last_name: payload.traits.lastName,
$name: getName(
payload.traits.firstName,
payload.traits.lastName,
payload.traits.name
),
$name: concatenatedName,
$username: payload.traits.username,
$phone: payload.traits.phone
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export enum ApiRegions {
EU = 'EU 🇪🇺'
}

export function getName(firstName: unknown, lastName: unknown, name: unknown): unknown {
export function getConcatenatedName(firstName: unknown, lastName: unknown, name: unknown): unknown {
return (
name ?? (firstName && lastName ? `${ firstName } ${ lastName }` : undefined)
)
Expand Down

0 comments on commit 2013e44

Please sign in to comment.