@benshi.ai/js-sdk
Version:
Benshi SDK
258 lines (186 loc) • 7.64 kB
text/typescript
/**
* @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 CatalogRepository, { ICatalogRepository } from "../../../core/repositories/catalog/CatalogRepository";
import ECommerce from "../../ECommerce";
import { DeliveryAction, DrugProperties, ECommerceTypes } from "../../ECommerce/typings";
import Navigation from ".."
import {
AppAction,
AudioVideoType,
ContentBlock,
IdentifyAction,
ImageType,
MediaActionForImage,
MediaActionForVideo,
MediaData,
NavigationTypes,
NudgeAction,
NudgeResponseType,
TrackActions,
TrackTypes,
UserInfo,
UserProperties,
} from "../typings";
let windowSpy
let mockSender;
let mockCatalogRepository: ICatalogRepository;
class MockImpressionsDetector {
start() { }
stop() { }
restart() { }
}
describe('Navigation - 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))
})
beforeEach(() => {
mockSender = {
add() { return false }
}
windowSpy = jest.spyOn(window, "window", "get");
})
afterEach(() => {
// to remove the singleton instance
(BsCore as any).instance = null
})
it('Should deliver an "App" event', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = {
action: AppAction.Background,
start_time: 0
}
Navigation.logAppEvent(event)
const valid = schemaValidate(senderSpy.mock.calls[0][2])
expect(valid).toEqual(true)
})
it('Should deliver a "Media" event', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = {
type: AudioVideoType.Video,
id: 'id-media-event',
action: MediaActionForVideo.Play,
time: 11111
}
Navigation.logMediaEvent(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)
})
it('Should deliver a "Track" event', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = {
action: TrackActions.ViewItem,
item_id: "111",
}
Navigation.logTrackEvent(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)
})
it('Should deliver a "Media" event with e-elearning type', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = {
contentBlock: ContentBlock.ELearning,
type: AudioVideoType.Audio,
id: 'id-media-event',
action: MediaActionForVideo.Play,
time: 111
}
Navigation.logMediaEvent(event)
console.log(beautify(senderSpy.mock.calls[0][2], null, 2, 80))
const valid = schemaValidate(senderSpy.mock.calls[0][2])
if (!valid) console.log(schemaValidate.errors)
expect(valid).toEqual(true)
})
it('Should deliver a "Page" event', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = { path: '/mypath', title: 'page-title' }
Navigation.logPageEvent(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)
})
it('Should deliver a "Login" event', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const props: UserInfo = {
action: IdentifyAction.Login,
user_props: {}
}
Navigation.logIdentifyEvent(props)
const valid = schemaValidate(senderSpy.mock.calls[0][2])
expect(valid).toEqual(true)
})
it('Should deliver a "Delivery" event with the content block set to ECommerce - no call to setCurrentBlock', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = {
id: "11",
action: DeliveryAction.Delivered,
order_id: "1",
}
ECommerce.logDeliveryEvent(event)
const valid = schemaValidate(senderSpy.mock.calls[0][2])
expect(valid).toEqual(true)
})
it('Should deliver a "Delivery" event with the contentBlock set to ECommerce - call to setCurrentBlock', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = {
id: "1",
action: DeliveryAction.Delivered,
order_id: "11",
}
Navigation.setCurrentBlock(ContentBlock.ELearning)
ECommerce.logDeliveryEvent(event)
const valid = schemaValidate(senderSpy.mock.calls[0][2])
expect(valid).toEqual(true)
})
it('Should deliver a "PushNotification" event', () => {
const DEFAULT_DEVICE_ID = 'web_sdk_device_id'
const senderSpy = jest.spyOn(mockSender, 'add')
const bsCore = BsCore.createInstance(mockSender as any, MockImpressionsDetector, false, ContentBlock.Core, DEFAULT_DEVICE_ID)
const event = {
nudge_id: 111,
type: NudgeResponseType.Push,
response: {
action: NudgeAction.Discard
}
}
Navigation.logPushNotificationEvent(event)
const valid = schemaValidate(senderSpy.mock.calls[0][2])
expect(valid).toEqual(true)
})
})