UNPKG

@benshi.ai/js-sdk

Version:

Benshi SDK

193 lines (162 loc) 5.95 kB
/** * @jest-environment jsdom */ import BsCore from "../../../core/BsCore" import { CurrencyCode } from "../../../core/commonTypes" import { ICatalogRepository } from "../../../core/repositories/catalog/CatalogRepository" import { ICurrencyRepository } from "../../../core/repositories/currency/CurrencyRepository" import ECommerce from "../../ECommerce" import { DeliveryAction, DrugProperties, ItemAction, ItemType, StockStatus } from "../../ECommerce/typings" import { AudioVideoType, ContentBlock, MediaData, UserProperties } from "../../Navigation/typings" let windowSpy const device_id = 'asdf' const mockImpressionManager = { on: () => { } }; let mockCatalogRepository: ICatalogRepository; describe('Identification tests', () => { beforeAll(() => { const mockCurrencyRepository: ICurrencyRepository = { convertCurrencyToUSD: (currency: CurrencyCode) => new Promise(r => r({ date: '2022-10-02', usd: 2 })) } mockCatalogRepository = { injectDrug: (userId: string, properties: DrugProperties) => new Promise(r => r()), injectUser: (userId: string, properties: UserProperties) => new Promise(r => r()), injectMedia: (id: string, type: AudioVideoType, properties: MediaData | {}) => { } } ECommerce.init(mockCurrencyRepository, mockCatalogRepository) }) beforeEach(() => { windowSpy = jest.spyOn(window, "window", "get"); }) afterEach(() => { // to remove the singleton instance (BsCore as any).instance = null }) it('Should use the same ID for device and user when it has not been set', async () => { const mockSender = { add() { return false } } const senderSpy = jest.spyOn(mockSender, 'add') const bsCore = BsCore.createInstance( mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, device_id) const event = { action: ItemAction.View, item: { id: '111', type: ItemType.Drug, quantity: 2, price: 100, currency: CurrencyCode.FKP, stock_status: StockStatus.InStock, promo_id: 'asdf' }, search_id: "1" } await ECommerce.logItemEvent(event, {}) expect(senderSpy).toBeCalledWith( expect.stringMatching(device_id), expect.stringMatching(device_id), expect.anything(), expect.anything()) }) it('Should use the custom user_id used specifically in this call', async () => { const mockSender = { add() { return false } } const senderSpy = jest.spyOn(mockSender, 'add') const bsCore = BsCore.createInstance( mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, device_id) const event = { id: '1111', action: DeliveryAction.Delivered, order_id: 'order_Id_111', } await ECommerce.logDeliveryEvent(event, 'new_user_id') expect(senderSpy).toBeCalledWith( expect.stringMatching('new_user_id'), expect.stringMatching(device_id), expect.anything(), expect.anything()) }) it('Should use the custom user_id used specifically in this call', async () => { const mockSender = { add() { return false } } const senderSpy = jest.spyOn(mockSender, 'add') const bsCore = BsCore.createInstance( mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, device_id) const event = { id: '1111', action: DeliveryAction.Delivered, order_id: 'order_Id_111', } await ECommerce.logDeliveryEvent(event, 'new_user_id') expect(senderSpy).toBeCalledWith( expect.stringMatching('new_user_id'), expect.stringMatching(device_id), expect.anything(), expect.anything()) }) it('Should use the custom user_id used specifically in this call even if the user is logged', async () => { const mockSender = { add() { return false } } const senderSpy = jest.spyOn(mockSender, 'add') const bsCore = BsCore.createInstance( mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, device_id) bsCore.login('real_user_id') const event = { id: '1111', action: DeliveryAction.Delivered, order_id: 'order_Id_111', } await ECommerce.logDeliveryEvent(event, 'new_user_id') expect(senderSpy).toBeCalledWith( expect.stringMatching('new_user_id'), expect.stringMatching(device_id), expect.anything(), expect.anything()) }) it('Should use user_id from logging ', async () => { const mockSender = { add() { return false } } const senderSpy = jest.spyOn(mockSender, 'add') const bsCore = BsCore.createInstance( mockSender as any, mockImpressionManager as any, false, ContentBlock.Core, device_id) bsCore.login('real_user_id') const event = { id: '1111', action: DeliveryAction.Delivered, order_id: 'order_Id_111', } await ECommerce.logDeliveryEvent(event) expect(senderSpy).toBeCalledWith( expect.stringMatching('real_user_id'), expect.stringMatching(device_id), expect.anything(), expect.anything()) }) })