From 4a2c8d1d781d4b6b33fd8cfe706c7f3d7c94b507 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 4 Sep 2023 16:24:37 +0200 Subject: [PATCH] refactor: refactor eligibility helper for test. --- .../scripts/helpers/almaEligibilityHelper.js | 24 ++++++-- .../helpers/almaEligibilityHelperMocks.js | 28 +++++++++ .../helpers/almaEligibilityHelperTest.js | 58 +++++++++++++++++++ 3 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 test/mocks/helpers/almaEligibilityHelperMocks.js create mode 100644 test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js index 8b397cb6..f1f640cf 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js @@ -29,17 +29,16 @@ 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( @@ -47,16 +46,29 @@ function getEligibility(plansForEligibility, locale, currentBasket) { 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 }; diff --git a/test/mocks/helpers/almaEligibilityHelperMocks.js b/test/mocks/helpers/almaEligibilityHelperMocks.js new file mode 100644 index 00000000..292c8131 --- /dev/null +++ b/test/mocks/helpers/almaEligibilityHelperMocks.js @@ -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 +}; diff --git a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js new file mode 100644 index 00000000..9c6a25e5 --- /dev/null +++ b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js @@ -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 + ); + }); +}); +