Skip to content

Commit

Permalink
refactor: refactor eligibility helper for test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Gomis committed Sep 4, 2023
1 parent 054bb8b commit 4a2c8d1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,46 @@ function callEligibility(param) {


/**
* Get eligibility for a given set of plan, a locale and a basket
* Get eligibility params for a given set of plan, a locale and a basket
* @param {Object} plansForEligibility Alma plans
* @param {string} locale e.g. 'fr_FR'
* @param {dw.order.Basket} currentBasket current basket
* @returns {array} of eligibility plan Object
*/
function getEligibility(plansForEligibility, locale, currentBasket) {
function getParams(plansForEligibility, locale, currentBasket) {
if (currentBasket === null) {
return [];
}

var purchaseAmount = Math.round(currentBasket.totalGrossPrice.multiply(100).value);
var billingAddress = formatAddress(currentBasket.getBillingAddress(), currentBasket.getCustomerEmail());
var shippingAddress = formatAddress(
currentBasket.getDefaultShipment().shippingAddress,
currentBasket.getCustomerEmail()
);

var param = {
var params = {
purchase_amount: purchaseAmount,
queries: plansForEligibility,
locale: locale,
billing_address: billingAddress,
shipping_address: shippingAddress
};
return callEligibility(param);
return params;
}

/**
* Get eligibility for a given set of plan, a locale and a basket
* @param {Object} plansForEligibility Alma plans
* @param {string} locale e.g. 'fr_FR'
* @param {dw.order.Basket} currentBasket current basket
* @returns {array} of eligibility plan Object
*/
function getEligibility(plansForEligibility, locale, currentBasket) {
var params = getParams(plansForEligibility, locale, currentBasket);
return callEligibility(params);
}

module.exports = {
getEligibility: getEligibility
getEligibility: getEligibility,
getParams: getParams
};
28 changes: 28 additions & 0 deletions test/mocks/helpers/almaEligibilityHelperMocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

var proxyquire = require('proxyquire').noCallThru().noPreserveCache();
var sinon = require('sinon');
var almaAddressHelper = require('../../mocks/helpers/almaAddressHelper');

var logger = {
getLogger: function () {
return {
info: sinon.stub()
};
}
};


function proxyModel() {
return proxyquire(
'../../../cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper',
{
'dw/system/Logger': logger,
'*/cartridge/scripts/helpers/almaAddressHelper': almaAddressHelper
}
);
}
module.exports = {
almaEligibilityHelperMocks: proxyModel(),
logger: logger
};
58 changes: 58 additions & 0 deletions test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

// almaEligibilityHelper.js unit tests

var assert = require('chai').assert;
var expect = require('chai').expect;
var almaEligibilityHelperMocks = require('../../../../mocks/helpers/almaEligibilityHelperMocks').almaEligibilityHelperMocks;
var basketMock = require('../../../../mocks/dw/order/BasketMgr');

var baseReturn =
{
purchase_amount: 25000,
queries: [],
locale: 'fr_FR',
billing_address: {
title: 'address.jobTitle',
first_name: 'address.lastName',
last_name: 'address.firstName',
company: 'address.companyName',
line1: 'address.address1',
line2: 'address.address2',
postal_code: 'address.postalCode',
city: 'address.city',
country: 'address.countryCode.value',
state_province: 'address.stateCode',
phone: 'address.phone'
},
shipping_address: {
title: 'address.jobTitle',
first_name: 'address.lastName',
last_name: 'address.firstName',
company: 'address.companyName',
line1: 'address.address1',
line2: 'address.address2',
postal_code: 'address.postalCode',
city: 'address.city',
country: 'address.countryCode.value',
state_province: 'address.stateCode',
phone: 'address.phone'
}
};

var baseBasket = basketMock.getCurrentBasket();
describe('Construct eligibility payload', function () {
it('return a empty array for a null current bask', function () {
var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', null);
// eslint-disable-next-line no-unused-expressions
expect(params).to.be.an('array').that.is.empty;
});
it('Return full eligibility payload for a basket', function () {
var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', baseBasket);
assert.deepEqual(
params,
baseReturn
);
});
});

0 comments on commit 4a2c8d1

Please sign in to comment.