Skip to content

Commit

Permalink
Giftcards unit tests (#1208)
Browse files Browse the repository at this point in the history
* feat: added unit test for check balance

* feat: test for cancel partial payment order

* feat: test for fetch giftcards

* feat: giftcards helper tests

* feat: added jest mocks
  • Loading branch information
amihajlovski authored Nov 8, 2024
1 parent b8fec2b commit 93b40af
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jest/__mocks__/dw/value/Money.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function Money(isAvailable) {
getCurrencyCode() {
return 'USD';
},
divide() {
return new Money(isAvailable);
},
subtract() {
return new Money(isAvailable);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable global-require */
let cancelPartialPaymentOrder;
let res;
let req = {
form: {
data: {
paymentMethod: 'givex'
}
}
}

jest.mock(
'*/cartridge/adyen/scripts/payments/adyenCheckout',
() => ({
doCancelPartialPaymentOrderCall: jest.fn(() => ({ resultCode: 'Received' })),
}),
{ virtual: true },
);

beforeEach(() => {
const { adyen } = require('../../../../controllers/middlewares/index');
cancelPartialPaymentOrder = adyen.cancelPartialPaymentOrder;
jest.clearAllMocks();
res = { redirect: jest.fn(), json: jest.fn() };
});

afterEach(() => {
jest.resetModules();
});

describe('cancelPartialPaymentOrder', () => {
it('should remove a gift card', async () => {
cancelPartialPaymentOrder(req, res, jest.fn());
expect(session.privacy.giftCardResponse).toBeFalsy();
expect(session.privacy.partialPaymentData).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable global-require */
let checkBalance;
let res;
let req = {
form: {
data: {
paymentMethod: 'givex'
}
}
}

beforeEach(() => {
const { adyen } = require('../../../../controllers/middlewares/index');
checkBalance = adyen.checkBalance;
jest.clearAllMocks();
res = { redirect: jest.fn(), json: jest.fn() };
});

afterEach(() => {
jest.resetModules();
});

describe('check balance', () => {
it('should send successful response', async () => {
checkBalance(req, res, jest.fn());
expect(res.json).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable global-require */
const BasketMgr = require('dw/order/BasketMgr');

let fetchGiftCards;
let res;
let req;
let adyenGiftCards = [{
giftCard: {
amount: {
value: 100,
currency: 'USD',
}
}
}]

jest.mock(
'*/cartridge/adyen/utils/adyenHelper',
() => ({
getDivisorForCurrency: jest.fn(),
}),
{ virtual: true },
);

jest.mock('dw/value/Money', () => {
return jest.fn().mockImplementation((value, currency) => {
return {
getValue: jest.fn(() => value),
getCurrency: jest.fn(() => currency),
divide: jest.fn().mockReturnThis(),
toFormattedString: jest.fn()
};
});
});

beforeEach(() => {
const { adyen } = require('../../../../controllers/middlewares/index');
fetchGiftCards = adyen.fetchGiftCards;
jest.clearAllMocks();
res = { redirect: jest.fn(), json: jest.fn() };
});

afterEach(() => {
jest.resetModules();
});

describe('fetchGiftCards', () => {
it('should return gift cards', () => {
const currentBasket = {
custom: {
adyenGiftCards: JSON.stringify(adyenGiftCards)
}
};
BasketMgr.getCurrentBasket.mockReturnValueOnce(currentBasket);
fetchGiftCards(req, res, jest.fn());
expect(res.json).toHaveBeenCalledWith({
giftCards: adyenGiftCards
});
});

it('should return empty array for gift cards', () => {
const currentBasket = {
custom: {
adyenGiftCards: []
}
};
BasketMgr.getCurrentBasket.mockReturnValueOnce(currentBasket);
fetchGiftCards(req, res, jest.fn());
expect(res.json).toHaveBeenCalledWith({
giftCards: [],
totalDiscountedAmount: null
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const AdyenHelper = require('*/cartridge/adyen/utils/adyenHelper');
const constants = require('*/cartridge/adyen/config/constants');
const giftCardsHelper = require('../giftCardsHelper');

jest.mock('dw/system/Transaction', () => ({
wrap: jest.fn()
}));

jest.mock('dw/order/PaymentMgr', () => ({
getPaymentMethod: jest.fn().mockReturnValue({
paymentProcessor: 'mockProcessor',
}),
}));

jest.mock('dw/value/Money', () => {
return jest.fn().mockImplementation((value, currency) => ({
divide: jest.fn().mockReturnValue({ value: value / 100, currency })
}));
});

jest.mock('*/cartridge/adyen/utils/adyenHelper', () => ({
setPaymentTransactionType: jest.fn(),
}));

describe('giftCardsHelper', () => {
describe('createGiftCardPaymentInstrument', () => {
const mockOrder = {
createPaymentInstrument: jest.fn().mockReturnValue({
paymentTransaction: { custom: {} },
custom: {},
}),
};

const parsedGiftCardObj = {
giftCard: {
amount: { value: 1000, currency: 'USD' },
pspReference: 'testPspReference',
name: 'Gift Card Name',
brand: 'Gift Card Brand',
},
};

beforeEach(() => {
jest.clearAllMocks();
});

it('should create a payment instrument with correct properties', () => {
const divideBy = 100;
giftCardsHelper.createGiftCardPaymentInstrument(parsedGiftCardObj, divideBy, mockOrder);
const createdInstrument = mockOrder.createPaymentInstrument.mock.results[0].value;
expect(createdInstrument.paymentTransaction.paymentProcessor).toStrictEqual({ID: "mockedPaymentProcessor"});
expect(createdInstrument.paymentTransaction.transactionID).toBe(parsedGiftCardObj.giftCard.pspReference);
expect(createdInstrument.custom.adyenPaymentMethod).toBe(parsedGiftCardObj.giftCard.name);
expect(createdInstrument.custom[`${constants.OMS_NAMESPACE}__Adyen_Payment_Method`]).toBe(parsedGiftCardObj.giftCard.name);
expect(createdInstrument.custom.Adyen_Payment_Method_Variant).toBe(parsedGiftCardObj.giftCard.brand);
expect(createdInstrument.custom[`${constants.OMS_NAMESPACE}__Adyen_Payment_Method_Variant`]).toBe(parsedGiftCardObj.giftCard.brand);
expect(createdInstrument.paymentTransaction.custom.Adyen_log).toBe(JSON.stringify(parsedGiftCardObj));
expect(createdInstrument.paymentTransaction.custom.Adyen_pspReference).toBe(parsedGiftCardObj.giftCard.pspReference);
expect(AdyenHelper.setPaymentTransactionType).toHaveBeenCalledWith(
createdInstrument,
parsedGiftCardObj.giftCard
);
});
});
});

0 comments on commit 93b40af

Please sign in to comment.