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
@@ -1,18 +1,98 @@
import nock from 'nock'
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import { createTestIntegration } from '@segment/actions-core'
import Definition from '../index'

const testDestination = createTestIntegration(Definition)

describe('Taguchi', () => {
beforeEach(() => {
nock.cleanAll()
})

describe('testAuthentication', () => {
it('should validate authentication inputs', async () => {
nock('https://your.destination.endpoint').get('*').reply(200, {})
it('should validate authentication with correct credentials', async () => {
// Mock the test endpoint (URL with /prod replaced by /test)
nock('https://api.taguchi.com.au')
.post('/test/subscriber')
.reply(200, [
{
code: 200,
name: 'Success',
description: 'Authentication successful'
}
])

// This should match your authentication.fields
const authData = {}
const authData = {
apiKey: 'test-api-key',
integrationURL: 'https://api.taguchi.com.au/prod',
organizationId: '123'
}

await expect(testDestination.testAuthentication(authData)).resolves.not.toThrowError()
})

it('should handle authentication failure', async () => {
// Mock authentication failure
nock('https://api.taguchi.com.au').post('/test/subscriber').reply(401, {
code: 401,
name: 'Unauthorized',
description: 'Invalid API key'
})

const authData = {
apiKey: 'invalid-api-key',
integrationURL: 'https://api.taguchi.com.au/prod',
organizationId: '123'
}

await expect(testDestination.testAuthentication(authData)).rejects.toThrowError()
})

it('should handle invalid integration URL', async () => {
// Mock network error for invalid URL
nock('https://invalid.endpoint.com')
.post('/test/subscriber')
.replyWithError('getaddrinfo ENOTFOUND invalid.endpoint.com')

const authData = {
apiKey: 'test-api-key',
integrationURL: 'https://invalid.endpoint.com/prod',
organizationId: '123'
}

await expect(testDestination.testAuthentication(authData)).rejects.toThrowError()
})

it('should replace /prod with /test in URL', async () => {
// Verify that the test endpoint is called correctly
const scope = nock('https://api.taguchi.com.au')
.post('/test/subscriber', (body) => {
// Verify the request body structure
expect(Array.isArray(body)).toBe(true)
expect(body[0]).toHaveProperty('profile')
expect(body[0].profile).toHaveProperty('organizationId', 123)
expect(body[0].profile).toHaveProperty('ref', 'test-connection')
expect(body[0].profile).toHaveProperty('firstname', 'Test')
return true
})
.reply(200, [
{
code: 200,
name: 'Success',
description: 'Test connection successful'
}
])

const authData = {
apiKey: 'test-api-key',
integrationURL: 'https://api.taguchi.com.au/prod',
organizationId: '123'
}

await testDestination.testAuthentication(authData)

// Verify that the correct endpoint was called
expect(scope.isDone()).toBe(true)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,77 +1,202 @@
import nock from 'nock'
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-taguchi'

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)
describe('Taguchi Destination Snapshots', () => {
beforeEach(() => {
nock.cleanAll()
})

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)
describe('syncAudience action', () => {
it('should match snapshot with required fields', async () => {
nock('https://api.taguchi.com.au')
.post('/subscriber')
.reply(200, [{ code: 200, name: 'Success', description: 'Subscriber processed' }])

const event = createTestEvent({
properties: eventData
type: 'identify',
userId: 'test-user-123',
traits: {
email: '[email protected]',
first_name: 'John',
last_name: 'Doe'
}
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
const { data } = await testDestination.testAction('syncAudience', {
event,
settings: {
apiKey: 'test-api-key',
integrationURL: 'https://api.taguchi.com.au',
organizationId: '123'
},
mapping: {
identifiers: {
ref: event.userId,
email: event.traits?.email
},
traits: {
firstname: event.traits?.first_name,
lastname: event.traits?.last_name
},
timestamp: event.timestamp
}
})

const request = responses[0].request
const rawBody = await request.text()
expect(data).toMatchSnapshot()
})

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}
it('should match snapshot with all fields', async () => {
nock('https://api.taguchi.com.au')
.post('/subscriber')
.reply(200, [{ code: 200, name: 'Success', description: 'Subscriber processed' }])

expect(request.headers).toMatchSnapshot()
})
const event = createTestEvent({
type: 'identify',
userId: 'test-user-123',
traits: {
email: '[email protected]',
first_name: 'John',
last_name: 'Doe',
title: 'Mr',
birthday: '1990-01-01T00:00:00.000Z',
street: '123 Main St',
city: 'Test City',
state: 'Test State',
country: 'Test Country',
postal_code: '12345',
phone: '555-1234',
gender: 'Male'
}
})

const { data } = await testDestination.testAction('syncAudience', {
event,
settings: {
apiKey: 'test-api-key',
integrationURL: 'https://api.taguchi.com.au',
organizationId: '123'
},
mapping: {
identifiers: {
ref: event.userId,
email: event.traits?.email
},
traits: {
title: event.traits?.title,
firstname: event.traits?.first_name,
lastname: event.traits?.last_name,
dob: event.traits?.birthday,
address: event.traits?.street,
suburb: event.traits?.city,
state: event.traits?.state,
country: event.traits?.country,
postcode: event.traits?.postal_code,
phone: event.traits?.phone,
gender: event.traits?.gender
},
subscribeLists: ['123', '456'],
unsubscribeLists: ['789'],
timestamp: event.timestamp
}
})

it(`${actionSlug} action - all fields`, async () => {
const seedName = `${destinationSlug}#${actionSlug}`
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)
expect(data).toMatchSnapshot()
})
})

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)
describe('syncEvent action', () => {
it('should match snapshot with required fields', async () => {
nock('https://api.taguchi.com.au')
.post('/subscriber')
.reply(200, [{ code: 200, name: 'Success', description: 'Event processed' }])

const event = createTestEvent({
properties: eventData
type: 'track',
event: 'Order Completed',
userId: 'test-user-123',
properties: {
total: 123.5,
products: [{ sku: '1290W', price: 123.5 }],
email: '[email protected]'
}
})

const { data } = await testDestination.testAction('syncEvent', {
event,
settings: {
apiKey: 'test-api-key',
integrationURL: 'https://api.taguchi.com.au',
organizationId: '123'
},
mapping: {
target: {
ref: event.userId,
email: event.properties?.email
},
eventType: 'p',
eventData: {
total: event.properties?.total,
products: event.properties?.products
}
}
})

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

it('should match snapshot with all fields', async () => {
nock('https://api.taguchi.com.au')
.post('/subscriber')
.reply(200, [{ code: 200, name: 'Success', description: 'Event processed' }])

const event = createTestEvent({
type: 'track',
event: 'Order Completed',
userId: 'test-user-123',
properties: {
total: 123.5,
products: [
{
sku: '1290W',
price: 123.5,
quantity: 1,
name: 'Test Product',
category: 'Electronics'
}
],
currency: 'USD',
order_id: 'order-123',
email: '[email protected]'
}
})

const request = responses[0].request
const rawBody = await request.text()
const { data } = await testDestination.testAction('syncEvent', {
event,
settings: {
apiKey: 'test-api-key',
integrationURL: 'https://api.taguchi.com.au',
organizationId: '123'
},
mapping: {
target: {
ref: event.userId,
email: event.properties?.email
},
isTest: false,
eventType: 'p',
eventData: {
total: event.properties?.total,
products: event.properties?.products,
currency: event.properties?.currency,
order_id: event.properties?.order_id
}
}
})

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