zuora to eloqua | zuora account service | bulk upload eloqua | Search

This code unit tests the accounts module's functionality to retrieve Zuora account information by mocking API calls and verifying that the correct query is sent to the Zuora API.

Run example

npm run import -- "test zuora account service"

test zuora account service

var sinon = require('sinon');
var assert = require('assert');
var importer = require('../Core');
var request = importer.import("request polyfill");
var accounts = importer.import("zuora account service");

var sandbox = sinon.createSandbox();
var zuoraConfig = {
    "rest_api_user":"devteam@fakepage.com",
    "rest_api_password":"pass",
    "rest_api_url": "http://localhost:18888"
};

describe('zuora account service', () => {
    afterEach(() => {
        sandbox.restore();
    })
    
    it('should call zuora query', () => {
        const dummyEmail = 'zuora-test@swiftipage.com'
        
        const queryStub = sandbox.stub(request, 'request').returns(Promise.resolve({ body: {records: [{}]} }))
        
        return accounts.getZuoraAccounts({
            items: [{
                 'EmailAddress': dummyEmail
            }]
        }, zuoraConfig).then(r => {
            assert(r);
            const queryCall = queryStub.getCall(0);
            assert(queryCall.args[0].json.queryString.includes(dummyEmail));
        });
    })
    
})

What the code could have been:

const sinon = require('sinon');
const { assert } = require('chai'); // Use chai for assertions
const importer = require('../Core');
const request = importer.import('request polyfill');
const accounts = importer.import('zuora account service');

describe('zuora account service', () => {
    let sandbox; // Declare sandbox variable to avoid global scope

    beforeEach(() => {
        sandbox = sinon.createSandbox(); // Create sandbox in beforeEach
    });

    afterEach(() => {
        sandbox.restore(); // Restore sandbox in afterEach
    });

    it('should call zuora query', async () => {
        // Define constants at the top for better readability
        const zuoraConfig = {
            "rest_api_user": "devteam@fakepage.com",
            "rest_api_password": "pass",
            "rest_api_url": "http://localhost:18888"
        };

        const dummyEmail = 'zuora-test@swiftipage.com';

        // Use sandbox.stub to return a promise for better testing
        const queryStub = sandbox.stub(request,'request').returns(
            Promise.resolve({ body: { records: [{ emailAddress: dummyEmail }] } })
        );

        // Call the function and then assert the results
        const result = await accounts.getZuoraAccounts({
            items: [{
                'EmailAddress': dummyEmail
            }]
        }, zuoraConfig);

        assert.isOk(result); // Use assert.isOk instead of just assert

        const queryCall = queryStub.getCall(0);
        assert.includes(queryCall.args[0].json.queryString, dummyEmail); // Use assert.includes
    });
});

This code defines unit tests for the accounts module, which interacts with the Zuora API to retrieve account information.

Here's a breakdown:

  1. Setup:

  2. Test Suite:

  3. Test Case: