-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
b8fec2b
commit 93b40af
Showing
5 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
..._SFRA/cartridge/adyen/scripts/partialPayments/__tests__/cancelPartialPaymentOrder.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
...ges/int_adyen_SFRA/cartridge/adyen/scripts/partialPayments/__tests__/checkBalance.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
...s/int_adyen_SFRA/cartridge/adyen/scripts/partialPayments/__tests__/fetchGiftCards.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/__tests__/giftCardsHelper.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); | ||
}); | ||
}); |