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.
npm run import -- "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));
});
})
})
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:
Setup:
sinon
for mocking, assert
for assertions, importer
for loading other modules, and request polyfill
for making HTTP requests.sinon.createSandbox()
to isolate the test environment.zuoraConfig
object with placeholder credentials.Test Suite:
describe('zuora account service', () => { ... })
to define a test suite.Test Case:
afterEach(() => { sandbox.restore(); })
: Restores the sandbox after each test to avoid interference.it('should call zuora query', () => { ... })
:
request.request
function using sandbox.stub
to return a predefined response containing dummy account data.accounts.getZuoraAccounts
with a sample notifyRequest
object containing an email address and the zuoraConfig
.request
function was called with a query string containing the provided email address.