@causalfoundry/js-sdk
Version:
Causal Foundry WEB SDK (JS/TS)
81 lines (63 loc) • 2.3 kB
text/typescript
import CfSender from './CfSender'
import { AppAction, ContentBlock, NavigationTypes } from '../modules/Navigation/typings';
import CfNetwork from "../drivers/network/CfNetwork";
jest.useFakeTimers();
jest.spyOn(global, 'setInterval');
const senderOptions = {
flushInterval: 10000,
flushMaxRetries: 10,
baseUrl: 'www.base.url',
ingestPath: '/ingestPath',
allowAnonymousUsers: false,
catalogPath: '/catalog',
nudgeFetchPath: '/nudgePath',
cacheEventsInLocalstorage: true,
activateNudgeMechanism: true,
cacheEventsKey: 'cfLog',
debug: true
}
let mockCfNetwork;
let notificationSpy;
describe('CfSender - setInterval', () => {
beforeEach(() => {
mockCfNetwork = {
send: jest.fn(() => Promise.resolve()),
get: jest.fn()
}
notificationSpy = jest.spyOn(mockCfNetwork, 'send')
})
it('Should send all events receive from last flushing - flush interval', async () => {
const cfSender = new CfSender(mockCfNetwork, senderOptions)
const event = {
block: ContentBlock.Core,
u_id: 'u_id',
d_id: 'device_id',
os: 'linux',
sdk: '',
ip: '0.0.0.0', // TODO: use ip-address package to check format a.b.c.d (or define our own one)
ol: true,
ts: "2022-01-12T07:36:28Z",
up: 90,
dn: 100,
type: NavigationTypes.App,
props: {
action: AppAction.Open,
start_time : 123
}
};
cfSender.add("userId", "deviceId", { ...event, ts: "2022-01-12T07:36:28Z" })
cfSender.add("userId", "deviceId", { ...event, ts: "2022-01-12T07:36:29Z" })
cfSender.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" }),
])
}
)
})
})