@benshi.ai/js-sdk
Version:
Benshi SDK
124 lines (100 loc) • 3.74 kB
text/typescript
/**
* @jest-environment jsdom
*/
import BsCore from "../../../core/BsCore"
import Payments from ".."
import { CurrencyCode } from "../../../core/commonTypes";
import {
DeferredPaymentType, PaymentsMethodType,
} from "../typings";
import { PaymentsTypes } from "../typings";
import { ICurrencyRepository } from "../../../core/repositories/currency/CurrencyRepository";
import { ContentBlock } from "../../Navigation/typings";
let mockSender;
let mockImpressionManager = {}
describe('Payments', () => {
beforeAll(() => {
mockSender = {
add() { return false }
}
const mockCurrencyRepository: ICurrencyRepository = {
convertCurrencyToUSD: (currency: CurrencyCode) => new Promise(r => r({ date: '2022-10-02', usd: 2 }))
}
Payments.init(mockCurrencyRepository)
})
afterEach(() => {
// to remove the singleton instance
(BsCore as any).instance = null
})
it('Should deliver an "DeferredPayment" event with payment mechanism', async () => {
const senderSpy = jest.spyOn(mockSender, 'add')
BsCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core,'device-id')
const event = {
id: '134',
order_id: '1234',
action: DeferredPaymentType.PaymentProcessed,
account_balance: -5,
payment_amount: 100,
currency: CurrencyCode.EUR,
type: PaymentsMethodType.Credit,
is_successful: true
}
await Payments.logDeferredPaymentEvent(event)
expect(senderSpy).toBeCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining(
{
type: PaymentsTypes.DeferredPayment,
props: { ...event, usd_rate: 2 }
}),
expect.anything())
})
it('Should deliver an "DeferredPayment" event with default payment mechanism', async () => {
const senderSpy = jest.spyOn(mockSender, 'add')
BsCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core,'device-id')
const event = {
id: '134',
order_id: '1234',
action: DeferredPaymentType.PaymentProcessed,
account_balance: -5,
payment_amount: 100,
currency: CurrencyCode.EUR,
is_successful: true
}
const publicEvent = {
...event,
type: PaymentsMethodType.BankTransfer
}
await Payments.logDeferredPaymentEvent(event)
expect(senderSpy).toBeCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining(
{
type: PaymentsTypes.DeferredPayment,
props: { ...publicEvent, usd_rate: 2 }
}),
expect.anything())
})
it('Should deliver an "PaymentMethod"', async () => {
const senderSpy = jest.spyOn(mockSender, 'add')
BsCore.createInstance(mockSender as any, mockImpressionManager as any, false, ContentBlock.Core,'device-id')
const event = {
order_id: "123",
type: PaymentsMethodType.BankCard,
payment_amount: 123,
currency: CurrencyCode.AED
}
await Payments.logPaymentMethodEvent(event)
expect(senderSpy).toBeCalledWith(
expect.anything(),
expect.anything(),
expect.objectContaining(
{
type: PaymentsTypes.PaymentMethod,
props: { ...event, usd_rate: 2 }
}),
expect.anything())
})
})