"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const nock_1 = __importDefault(require("nock"));
const actions_core_1 = require("@segment/actions-core");
const index_1 = __importDefault(require("../index"));
const testDestination = (0, actions_core_1.createTestIntegration)(index_1.default);
const timestamp = '2022-05-12T15:21:15.449Z';
const settings = {
    subdomain: 'test123',
    client_id: 'test123',
    client_secret: 'test123',
    account_id: 'test123'
};
const event = (0, actions_core_1.createTestEvent)({
    timestamp: timestamp,
    userId: 'eric-15',
    type: 'track',
    properties: {
        contactKey: 'ericForman15',
        eventDefinitionKey: '123',
        eventData: {
            Email: 'eric.forman@test.com',
            First_Name: 'Eric',
            Last_Name: 'Forman'
        }
    }
});
const requestUrl = `https://${settings.subdomain}.rest.marketingcloudapis.com/interaction/v1/events`;
describe('Salesforce Marketing Cloud', () => {
    describe('Send API Event Action', () => {
        it('Send API Event', async () => {
            (0, nock_1.default)(requestUrl).post('').reply(200, {});
            const responses = await testDestination.testAction('apiEvent', {
                event,
                settings,
                mapping: {
                    contactKey: { '@path': '$.properties.contactKey' },
                    eventDefinitionKey: { '@path': '$.properties.eventDefinitionKey' },
                    data: { '@path': '$.properties.eventData' }
                }
            });
            expect(responses[0].status).toBe(200);
            expect(responses[0].options.body).toMatchInlineSnapshot(`"{\\"contactKey\\":\\"ericForman15\\",\\"eventDefinitionKey\\":\\"123\\",\\"data\\":{\\"Email\\":\\"eric.forman@test.com\\",\\"First_Name\\":\\"Eric\\",\\"Last_Name\\":\\"Forman\\"}}"`);
        });
        it('Send API Event with Default', async () => {
            (0, nock_1.default)(requestUrl).post('').reply(200, {});
            const responses = await testDestination.testAction('apiEvent', {
                event,
                settings,
                useDefaultMappings: true,
                mapping: {
                    eventDefinitionKey: { '@path': '$.properties.eventDefinitionKey' },
                    data: { '@path': '$.properties.eventData' }
                }
            });
            expect(responses[0].status).toBe(200);
            expect(responses[0].options.body).toMatchInlineSnapshot(`"{\\"eventDefinitionKey\\":\\"123\\",\\"contactKey\\":\\"eric-15\\",\\"data\\":{\\"Email\\":\\"eric.forman@test.com\\",\\"First_Name\\":\\"Eric\\",\\"Last_Name\\":\\"Forman\\"}}"`);
        });
        it('Send API Event without Data', async () => {
            (0, nock_1.default)(requestUrl).post('').reply(200, {});
            const responses = await testDestination.testAction('apiEvent', {
                event,
                settings,
                useDefaultMappings: true,
                mapping: {
                    eventDefinitionKey: { '@path': '$.properties.eventDefinitionKey' }
                }
            });
            expect(responses[0].status).toBe(200);
            expect(responses[0].options.body).toMatchInlineSnapshot(`"{\\"eventDefinitionKey\\":\\"123\\",\\"contactKey\\":\\"eric-15\\"}"`);
        });
        it('Fail to send api event due to missing required fields -> contactKey & eventDefinitionKey', async () => {
            (0, nock_1.default)(requestUrl).post('').reply(400, {});
            await expect(testDestination.testAction('apiEvent', {
                event,
                settings
            })).rejects.toThrowError(`The root value is missing the required field 'eventDefinitionKey'. The root value is missing the required field 'contactKey'.`);
        });
    });
});
//# sourceMappingURL=apiEvent.test.js.map