This code unit tests the eloqua existing import module, which checks for the existence of custom data objects and import definitions within the Eloqua API. The tests use Sinon.js to mock API responses and assert the module's expected behavior.
npm run import -- "test eloqua existing import"var assert = require('assert');
var sinon = require('sinon');
var importer = require('../Core');
var request = importer.import("http request polyfill");
var existing = importer.import("eloqua existing import");
var { bulkImportTemplate } = importer.import("eloqua create template");
var {
getEloquaConfig,
getOauthToken,
} = importer.import("eloqua import blueprints");
var eloquaConfig = getEloquaConfig();
var eloquaToken = getOauthToken();
var sandbox = sinon.createSandbox();
describe('eloqua existing import', () => {
afterEach(() => {
sandbox.restore();
})
it('should check if the custom object is configured', () => {
const requestStub = sandbox.stub(request, "request")
.returns(Promise.resolve({ body: {items: [{name: 'AUT - NA Renewals'}] } }));
return existing.getCustomDataObject({}, eloquaConfig)
.then(r => {
assert(r);
assert(requestStub.calledOnce, 'request should only be called once');
})
})
it('should check if there is already an import definition', () => {
const requestStub = sandbox.stub(request, "request")
.returns(Promise.resolve({ body: {items: [{name: bulkImportTemplate(0).name, uri: '/imports/1234'}] } }));
return existing.getImportDefinitions('/imports/1234', eloquaToken, eloquaConfig)
.then(r => {
assert(r.uri);
assert(requestStub.calledOnce, 'request should only be called once');
})
})
});
const assert = require('assert');
const sinon = require('sinon');
const { createSandbox } = require('sinon');
const importer = require('../Core');
const requestMock = importer.import('http request polyfill');
const existingImport = importer.import('eloqua existing import');
const { getEloquaConfig, getOauthToken, bulkImportTemplate } = importer.import('eloqua import blueprints');
describe('Eloqua Existing Import', () => {
let sandbox;
beforeEach(() => {
sandbox = createSandbox();
});
afterEach(() => {
sandbox.restore();
});
it('should check if the custom object is configured', async () => {
// Mock the request response to return a custom object
const requestStub = sandbox.stub(requestMock,'request')
.returns(Promise.resolve({ body: { items: [{ name: 'AUT - NA Renewals' }] } }));
const result = await existingImport.getCustomDataObject({}, await getEloquaConfig());
assert(result);
assert(requestStub.calledOnce,'request should only be called once');
});
it('should check if there is already an import definition', async () => {
// Mock the request response to return an import definition
const requestStub = sandbox.stub(requestMock,'request')
.returns(Promise.resolve({ body: { items: [{ name: bulkImportTemplate(0).name, uri: '/imports/1234' }] } }));
const result = await existingImport.getImportDefinitions('/imports/1234', await getOauthToken(), await getEloquaConfig());
assert(result.uri);
assert(requestStub.calledOnce,'request should only be called once');
});
});This code defines unit tests for the eloqua existing import module, which likely handles interacting with the Eloqua API to check for existing custom data objects and import definitions.
Here's a breakdown:
Setup:
assert for assertions, sinon for mocking, and modules related to Eloqua API interaction.sinon.createSandbox() to isolate test environment.Test Suite:
describe('eloqua existing import', () => { ... }).Test Cases:
afterEach(() => { sandbox.restore(); }): Restores the sandbox after each test to avoid interference.it('should check if the custom object is configured', () => { ... }):
request function using sandbox.stub(request, "request") to return a predefined response containing a custom object named "AUT - NA Renewals".existing.getCustomDataObject({}, eloquaConfig) to test the function.request function was called only once.it('should check if there is already an import definition', () => { ... }):
request function to return a response containing an import definition with a specific name and URI.existing.getImportDefinitions('/imports/1234', eloquaToken, eloquaConfig) to test the function.uri property and that the request function was called only once.Conclusion:
These tests verify the functionality of the eloqua existing import module by mocking API interactions and asserting the expected behavior.