Skip to content
Closed
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,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for actions-altertable destination: event action - all fields 1`] = `
Object {
"distinct_id": "UM[e12E",
"environment": "UM[e12E",
"event": "UM[e12E",
"properties": Object {
"$lib": "altertable-segment",
"testType": "UM[e12E",
},
"timestamp": "2021-02-01T00:00:00.000Z",
}
`;

exports[`Testing snapshot for actions-altertable destination: event action - required fields 1`] = `
Object {
"distinct_id": "UM[e12E",
"environment": "UM[e12E",
"event": "UM[e12E",
"properties": Object {
"testType": "UM[e12E",
},
"timestamp": "2021-02-01T00:00:00.000Z",
}
`;

exports[`Testing snapshot for actions-altertable destination: identify action - all fields 1`] = `
Object {
"distinct_id": "&03lz0I0LT8Hl)",
"environment": "&03lz0I0LT8Hl)",
"timestamp": "2021-02-01T00:00:00.000Z",
"traits": Object {
"$lib": "altertable-segment",
"testType": "&03lz0I0LT8Hl)",
},
}
`;

exports[`Testing snapshot for actions-altertable destination: identify action - required fields 1`] = `
Object {
"distinct_id": "&03lz0I0LT8Hl)",
"environment": "&03lz0I0LT8Hl)",
"timestamp": "2021-02-01T00:00:00.000Z",
"traits": Object {
"testType": "&03lz0I0LT8Hl)",
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 destinationSlug = 'actions-altertable'

describe(`Testing snapshot for ${destinationSlug} destination:`, () => {
for (const actionSlug in destination.actions) {
it(`${actionSlug} action - required fields`, async () => {
const seedName = `${destinationSlug}#${actionSlug}`
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,
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(`${actionSlug} action - all fields`, async () => {
const seedName = `${destinationSlug}#${actionSlug}`
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()
}
})
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for Altertable's event destination action: all fields 1`] = `
Object {
"distinct_id": "CHXbRcxq]U",
"environment": "CHXbRcxq]U",
"event": "CHXbRcxq]U",
"properties": Object {
"$lib": "altertable-segment",
"testType": "CHXbRcxq]U",
},
"timestamp": "2021-02-01T00:00:00.000Z",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import nock from 'nock'
import { createTestEvent, createTestIntegration, SegmentEvent } from '@segment/actions-core'
import Destination from '../../index'

const testDestination = createTestIntegration(Destination)

describe('Altertable.event', () => {
const endpoint = 'https://api.altertable.ai'
const apiKey = 'test-api-key'
const environment = 'test-environment'

beforeEach(() => nock.cleanAll())

it('should send event to Altertable', async () => {
const event = createTestEvent({
properties: {
testProperty: 'test-value'
},
context: {
device: {
id: 'test-device-id'
}
}
})

nock(endpoint).post('/track').reply(200, {})

const responses = await testDestination.testAction('event', {
event,
useDefaultMappings: true,
settings: {
apiKey: apiKey,
endpoint: endpoint,
environment: environment
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
const payload = JSON.parse(responses[0].options.body as string)
expect(payload).toMatchObject({
environment: environment,
properties: expect.objectContaining({
testProperty: 'test-value'
}),
distinct_id: event.userId,
anonymous_id: event.anonymousId,
timestamp: event.timestamp,
device_id: event.context?.device?.id
})
})

it('should throw error if required fields are missing', async () => {
const event = createTestEvent({
properties: {
testProperty: 'test-value'
}
})

await expect(
testDestination.testAction('event', {
event,
mapping: {
// Only provide properties, missing required event, userId, and timestamp
properties: {
'@path': '$.properties'
}
},
settings: {
apiKey: apiKey,
endpoint: endpoint,
environment: environment
}
})
).rejects.toThrow()
})

it('should correctly map Segment context properties to Altertable format', async () => {
const event: SegmentEvent = {
type: 'track',
properties: {
customProp: 'custom-value'
},
context: {
ip: '192.168.1.1',
page: {
url: 'https://example.com/page',
referrer: 'https://google.com'
},
os: {
name: 'iOS'
},
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0)',
screen: {
width: 375,
height: 667
},
campaign: {
name: 'summer-sale',
source: 'google',
medium: 'cpc',
term: 'shoes',
content: 'ad-variant-a'
},
library: {
name: 'analytics.js',
version: '3.0.0'
},
device: {
id: 'device-123'
}
}
}

nock(endpoint).post('/track').reply(200, {})

const responses = await testDestination.testAction('event', {
event,
useDefaultMappings: true,
settings: {
apiKey: apiKey,
endpoint: endpoint,
environment: environment
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
const payload = JSON.parse(responses[0].options.body as string)

// Verify context mappings
expect(payload.properties).toMatchObject({
// Direct context mappings
$ip: '192.168.1.1',
$url: 'https://example.com/page',
$referer: 'https://google.com',
$os: 'iOS',
$user_agent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0)',

// Screen dimensions -> viewport
$viewport: '375x667',

// Campaign mappings
$utm_campaign: 'summer-sale',
$utm_source: 'google',
$utm_medium: 'cpc',
$utm_term: 'shoes',
$utm_content: 'ad-variant-a',

// Library mappings
$lib: 'analytics.js',
$lib_version: '3.0.0',

// Event properties should still be present
customProp: 'custom-value'
})

// Verify device_id is extracted correctly
expect(payload.device_id).toBe('device-123')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 = 'event'
const destinationSlug = 'Altertable'
const seedName = `${destinationSlug}#${actionSlug}`

describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination action:`, () => {
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