UNPKG

@benshi.ai/js-sdk

Version:

Benshi SDK

78 lines (58 loc) 2.2 kB
/** * @jest-environment jsdom */ const fs = require('fs') const yaml = require('js-yaml'); var beautify = require("json-beautify"); import Ajv from 'ajv' import BsCore from "../../../core/BsCore" import Payments from ".." import { CurrencyCode } from "../../../core/commonTypes"; import { DeferredPaymentType, } from "../typings"; import { PaymentsTypes } from "../typings"; import { ICurrencyRepository } from "../../../core/repositories/currency/CurrencyRepository"; import { ContentBlock } from "../../Navigation/typings"; let mockSender; let mockImpressionManager = {} describe('Payments - API', () => { let schemaValidate; beforeAll(() => { const schemaPath = `${__dirname}/../../../../scripts/standardized-schema.json` const jsonSchema = fs.readFileSync(schemaPath, 'utf8') const ajv = new Ajv() schemaValidate = ajv.compile(JSON.parse(jsonSchema)) 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', async () => { const senderSpy = jest.spyOn(mockSender, 'add') const bsCore = 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 } await Payments.logDeferredPaymentEvent(event) const valid = schemaValidate(senderSpy.mock.calls[0][2]) if (!valid) { console.log(beautify(senderSpy.mock.calls[0][2], null, 2, 80)) console.log(schemaValidate.errors) } expect(valid).toEqual(true) }) })