@aller/blink
Version:
A library for tracking user behaviour.
174 lines (155 loc) • 4.47 kB
text/typescript
import createBlink from '../main';
import { VERSION } from '../config/config';
import jest from 'jest-mock';
describe('Custom type intregration test', () => {
it('should send a single custom event', () => {
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// First send of a pageInit event, to set the general state
blink.pageInit({
url: 'http://some.site',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
site: 'www.kk.no',
});
blink.custom({
customDomain: 'fake-domain',
customType: 'fake-type',
customContent: 'fake-content',
customValue: 12,
time: new Date(3),
});
expect(mockSend.mock.calls[0][0]).toEqual([
{
id: 'fake-domainfake-type',
type: 'custom',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
site: 'www.kk.no',
time: new Date(3),
version: VERSION,
customDomain: 'fake-domain',
customType: 'fake-type',
customContent: 'fake-content',
customValue: 12,
},
]);
});
it('should send a single custom event even with time missing', () => {
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// First send of a pageInit event, to set the general state
blink.pageInit({
url: 'http://some.site',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
site: 'www.kk.no',
customUserAgent: 'fake-ua',
});
blink.custom({
customDomain: 'fake-domain',
customType: 'fake-type',
customContent: 'fake-content',
customValue: 12,
});
const expected = {
id: 'fake-domainfake-type',
type: 'custom',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
site: 'www.kk.no',
version: VERSION,
customUserAgent: 'fake-ua',
customDomain: 'fake-domain',
customType: 'fake-type',
customContent: 'fake-content',
customValue: 12,
time: new Date(1),
};
const expectedFields = Object.keys(expected).sort();
const fields = Object.keys(mockSend.mock.calls[0][0][0]).sort();
expect(fields).toEqual(expectedFields);
});
it('should send two separate events for two separate pages', () => {
enum PAGES {
FIRST = 'FIRST_PAGE',
SECOND = 'SECOND_PAGE',
}
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// FIRST PAGE INIT
blink.pageInit({
url: 'http://some.site',
pageView: `${PAGES.FIRST}-pageview`,
pageId: PAGES.FIRST,
referrer: 'www.sol.no',
site: 'www.kk.no',
});
// SECOND PAGE INIT
blink.pageInit({
url: 'http://other.site',
pageView: `${PAGES.SECOND}-pageview`,
pageId: PAGES.SECOND,
referrer: 'www.sol.no',
site: 'www.kk.no',
});
// FIRST PAGE CUSTOM EVENT
blink.custom({
customDomain: 'fake-domain',
customType: 'fake-type',
customContent: 'fake-content',
customValue: 12,
time: new Date(1000),
pageId: PAGES.FIRST,
});
// SECOND PAGE CUSTOM EVENT
blink.custom({
customDomain: 'second-domain',
customType: 'second-type',
customContent: 'second-content',
customValue: 42,
time: new Date(2000),
pageId: PAGES.SECOND,
});
expect(mockSend.mock.calls.length).toBe(2);
// Check first event
const firstEvent = mockSend.mock.calls[0][0][0];
expect(firstEvent).toEqual({
customContent: 'fake-content',
customDomain: 'fake-domain',
customType: 'fake-type',
customValue: 12,
pageView: 'FIRST_PAGE-pageview',
referrer: 'www.sol.no',
site: 'www.kk.no',
type: 'custom',
version: VERSION,
time: new Date(1000),
id: 'fake-domainfake-type',
});
// Check second event
const secondEvent = mockSend.mock.calls[1][0][0];
expect(secondEvent).toEqual({
customContent: 'second-content',
customDomain: 'second-domain',
customType: 'second-type',
customValue: 42,
id: 'second-domainsecond-type',
pageView: 'SECOND_PAGE-pageview',
referrer: 'www.sol.no',
site: 'www.kk.no',
time: new Date(2000),
type: 'custom',
version: VERSION,
});
});
});