Skip to content

[Attentive] - New Actions #3033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const API_URL = 'https://api.attentivemobile.com'
export const API_VERSION = 'v1'
export const MARKETING = 'MARKETING' as const
export const TRANSACTIONAL = 'TRANSACTIONAL' as const
export const SUBSCRIPTION_TYPES = [MARKETING, TRANSACTIONAL] as const
export const SUBSCRIPTION_TYPE_CHOICES = [
{ label: MARKETING, value: MARKETING },
{ label: TRANSACTIONAL, value: TRANSACTIONAL }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for Attentive's customEvents destination action: all fields 1`] = `
Object {
"externalEventId": "*8ANaCHkFXR*[r23fd",
"occurredAt": "*8ANaCHkFXR*[r23fd",
"properties": Object {
"testType": "*8ANaCHkFXR*[r23fd",
},
"type": "*8ANaCHkFXR*[r23fd",
"user": Object {
"email": "[email protected]",
"externalIdentifiers": Object {
"clientUserId": "*8ANaCHkFXR*[r23fd",
},
"phone": "*8ANaCHkFXR*[r23fd",
},
}
`;

exports[`Testing snapshot for Attentive's customEvents destination action: required fields 1`] = `
Object {
"type": "*8ANaCHkFXR*[r23fd",
"user": Object {
"externalIdentifiers": Object {
"customIdentifiers": Object {
"userId": "testuserid",
},
},
},
}
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nock from 'nock'
import { createTestEvent, createTestIntegration, SegmentEvent, PayloadValidationError } from '@segment/actions-core'

Check failure on line 2 in packages/destination-actions/src/destinations/attentive/customEvents/__tests__/index.test.ts

View workflow job for this annotation

GitHub Actions / Lint (22.x)

'PayloadValidationError' is defined but never used. Allowed unused vars must match /^_/u
import Definition from '../../index'
import { Settings } from '../../generated-types'

Expand All @@ -11,20 +11,20 @@
}

const validPayload = {
timestamp: timestamp,
event: 'Event Type 1',
timestamp,
event: 'Product Clicked', // <- Used as "type" in Attentive
messageId: '123e4567-e89b-12d3-a456-426614174000',
type: 'track',
userId: '123e4567-e89b-12d3-a456-426614174000',
userId: 'user-123',
context: {
traits: {
phone: '+3538675765689',
email: '[email protected]'
}
},
properties: {
tracking_url: 'https://tracking-url.com',
product_name: 'Product X'
product_name: 'Product X',
tracking_url: 'https://tracking-url.com'
}
} as Partial<SegmentEvent>

Expand All @@ -41,62 +41,89 @@
}

const expectedPayload = {
type: 'Event Type 1',
properties: {
tracking_url: 'https://tracking-url.com',
product_name: 'Product X'
},
externalEventId: '123e4567-e89b-12d3-a456-426614174000',
occurredAt: '2024-01-08T13:52:50.212Z',
type: 'Product Clicked',
user: {
phone: '+3538675765689',
email: '[email protected]',
externalIdentifiers: {
clientUserId: '123e4567-e89b-12d3-a456-426614174000'
clientUserId: 'user-123'
}
}
},
properties: {
product_name: 'Product X',
tracking_url: 'https://tracking-url.com'
},
externalEventId: '123e4567-e89b-12d3-a456-426614174000',
occurredAt: timestamp
}

beforeEach((done) => {
beforeEach(() => {
testDestination = createTestIntegration(Definition)
nock.cleanAll()
done()
})

describe('Attentive.customEvents', () => {
it('should send a custom event to Attentive', async () => {
const event = createTestEvent(validPayload)

nock('https://api.attentivemobile.com').post('/v1/events/custom', expectedPayload).reply(200, {})
nock('https://api.attentivemobile.com', {
reqheaders: {
authorization: 'Bearer test-api-key',
'content-type': 'application/json'
}
})
.post('/v1/events/custom', expectedPayload)
.reply(200, {})

const responses = await testDestination.testAction('customEvents', {
event,
settings,
useDefaultMappings: true,
mapping
mapping,
useDefaultMappings: false
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
})

it('should throw error if no identifiers provided', async () => {
it('throws error if no userIdentifiers provided', async () => {
const badPayload = {
...validPayload,
context: {
traits: {}
},
userId: undefined
}

const event = createTestEvent(badPayload)

await expect(
testDestination.testAction('customEvents', {
event,
settings,
mapping,
useDefaultMappings: false
})
).rejects.toThrowError("At least one user identifier is required.")
})

it('throws error if properties contain arrays', async () => {
const badPayload = {
...validPayload
...validPayload,
properties: {
someArray: [1, 2, 3]
}
}
delete badPayload?.context?.traits?.phone
delete badPayload?.context?.traits?.email
badPayload.userId = undefined

const event = createTestEvent(badPayload)

await expect(
testDestination.testAction('customEvents', {
event,
settings,
useDefaultMappings: true,
mapping
mapping,
useDefaultMappings: false
})
).rejects.toThrowError(new PayloadValidationError('At least one user identifier is required.'))
).rejects.toThrowError("Properties cannot contain arrays.")
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import { generateTestData } from '../../../../lib/test-data'
import destination from '../../index'
import nock from 'nock'

const testDestination = createTestIntegration(destination)
const actionSlug = 'customEvents'
const destinationSlug = 'Attentive'
const seedName = `${destinationSlug}#${actionSlug}`

describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination action:`, () => {
it('required fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, true)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping:
{
...event.properties,
userIdentifiers: {
userId: "testuserid"
}
},
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}

expect(request.headers).toMatchSnapshot()
})

it('all fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading