@benshi.ai/js-sdk
Version:
Benshi SDK
70 lines (58 loc) • 2.21 kB
text/typescript
import BsSender from './BsSender'
import {AppAction, ContentBlock, NavigationTypes} from '../modules/Navigation/typings';
jest.useFakeTimers();
jest.spyOn(global, 'setInterval');
const senderOptions = {
flushInterval: 10000,
flushMaxRetries: 10,
baseUrl: 'www.base.url',
ingestPath: '/ingestPath',
currencyPath: '/currencyPath',
catalogPath: '/catalog',
nudgeFetchPath: '/nudgePath',
cacheEventsInLocalstorage: true,
activateNudgeMechanism: true,
cacheEventsKey: 'bslog',
debug: true
}
describe('BsSender - setInterval', () => {
it('Should send all events receive from last flushing - flush interval', async () => {
const mockBsNetwork = {
send: jest.fn(() => Promise.resolve()),
get: jest.fn()
}
const notificationSpy = jest.spyOn(mockBsNetwork, 'send')
const bsSender = new BsSender(mockBsNetwork, senderOptions)
const event = {
block: ContentBlock.Core,
ol: true,
ts: "2022-01-12T07:36:28Z",
type: NavigationTypes.App,
props: {
action: AppAction.Open,
start_time: 122
}
};
bsSender.add("userId", "deviceId", {...event, ts: "2022-01-12T07:36:28Z"})
bsSender.add("userId", "deviceId", {...event, ts: "2022-01-12T07:36:29Z"})
bsSender.add("userId", "deviceId", {...event, ts: "2022-01-12T07:36:30Z"})
jest.runOnlyPendingTimers()
expect(notificationSpy).toHaveBeenCalledWith(
expect.anything(),
{
data: expect.arrayContaining([
expect.objectContaining({ts: "2022-01-12T07:36:28Z"}),
expect.objectContaining({ts: "2022-01-12T07:36:29Z"}),
expect.objectContaining({ts: "2022-01-12T07:36:30Z"}),
]),
app_info: expect.anything(),
d_info: expect.anything(),
dn: expect.anything(),
up: expect.anything(),
sdk: expect.anything(),
u_id: expect.anything(),
s_id: expect.anything(),
}
)
})
})