UNPKG

@aller/blink

Version:

A library for tracking user behaviour.

182 lines 7.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var event_time_1 = require("../event-time"); describe('calculateEventTime', function () { it('should calculate the total', function () { var times = [ { type: 'start', time: new Date(0, 0, 0, 0, 0) }, { type: 'stop', time: new Date(0, 0, 0, 0, 1) }, ]; var inscreen = event_time_1.calculateEventTime({ times: times }); expect(inscreen).toEqual(60000); }); it('should merge duplicate events by picking the first one', function () { var times = [ { type: 'start', time: new Date(0, 0, 0, 0, 0) }, { type: 'stop', time: new Date(0, 0, 0, 0, 1) }, { type: 'stop', time: new Date(0, 0, 0, 0, 2) }, ]; var inscreen = event_time_1.calculateEventTime({ times: times }); expect(inscreen).toEqual(60000 * 1); }); it('should discard unrealistically high values', function () { var times = [ { type: 'start', time: new Date(0, 0, 0, 0, 0) }, { type: 'stop', time: new Date(2017, 0, 0, 0, 0) }, ]; var inscreen = event_time_1.calculateEventTime({ times: times }); expect(inscreen).toEqual(0); }); it('should calculate the total for multiple intervals', function () { var times = [ // One second { type: 'start', time: new Date(0, 0, 0, 0, 0) }, { type: 'stop', time: new Date(0, 0, 0, 0, 1) }, { type: 'start', time: new Date(0, 0, 0, 0, 2) }, { type: 'stop', time: new Date(0, 0, 0, 0, 3) }, ]; var inscreen = event_time_1.calculateEventTime({ times: times }); expect(inscreen).toEqual(60000 * 2); }); it('should set inscreen to zero if no recorded times', function () { var times = []; var inscreen = event_time_1.calculateEventTime({ times: times }); expect(inscreen).toEqual(0); }); it('should set inscreen to zero if only a stop time', function () { var times = [ { type: 'stop', time: new Date(0, 0, 0, 0, 1) }, ]; var inscreen = event_time_1.calculateEventTime({ times: times }); expect(inscreen).toEqual(0); }); it('should ignore the first event if it is a stop event', function () { var times = [ { type: 'stop', time: new Date(1000) }, { type: 'start', time: new Date(2000) }, ]; var inscreen = event_time_1.calculateEventTime({ times: times, now: new Date(4000), }); expect(inscreen).toEqual(2000); }); it('should use the current date if no end time to calculate total', function () { var times = [{ type: 'start', time: new Date(0) }]; var currentDate = new Date(1000); var inscreen = event_time_1.calculateEventTime({ times: times, now: currentDate }); expect(inscreen).toEqual(1000); }); it('should have a maximum idle time if no stop events registered', function () { var times = [{ type: 'start', time: new Date(1000) }]; var currentDate = new Date(40000); var inscreen = event_time_1.calculateEventTime({ times: times, now: currentDate, maxIdleTime: 10000, }); expect(inscreen).toEqual(10000); }); it('should consider consecutive start times as a pulse', function () { var times = [ { type: 'start', time: new Date(1000) }, { type: 'start', time: new Date(3000) }, ]; var currentDate = new Date(50000); var inscreen = event_time_1.calculateEventTime({ times: times, now: currentDate, maxIdleTime: 10000, }); expect(inscreen).toEqual(12000); }); it('should conevert a page hide to a stop time', function () { var times = [ { type: 'start', time: new Date(0, 0, 0, 0, 0) }, ]; var screenEvents = [ { type: 'hide', time: new Date(0, 0, 0, 0, 2) }, ]; var currentDate = new Date(0, 0, 0, 0, 5); var inscreen = event_time_1.calculateEventTime({ times: times, screenEvents: screenEvents, now: currentDate, }); expect(inscreen).toEqual(120000); }); it('should handle complex case', function () { var times = [ { type: 'start', time: new Date(0, 0, 0, 0, 0) }, { type: 'stop', time: new Date(0, 0, 0, 0, 2) }, { type: 'start', time: new Date(0, 0, 0, 0, 4) }, { type: 'stop', time: new Date(0, 0, 0, 0, 6) }, ]; var screenEvents = [ { type: 'show', time: new Date(0, 0, 0, 0, 1) }, { type: 'hide', time: new Date(0, 0, 0, 0, 3) }, { type: 'hide', time: new Date(0, 0, 0, 0, 5) }, { type: 'show', time: new Date(0, 0, 0, 0, 7) }, ]; var currentDate = new Date(0, 0, 0, 0, 20); var inscreen = event_time_1.calculateEventTime({ times: times, screenEvents: screenEvents, now: currentDate, }); expect(inscreen).toEqual(180000); }); it('should case where the showing and hiding of the page controls the inscreen time', function () { var times = [ { type: 'start', time: new Date(0, 0, 0, 0, 0) }, { type: 'stop', time: new Date(0, 0, 0, 0, 20) }, ]; var screenEvents = [ { type: 'show', time: new Date(0, 0, 0, 0, 1) }, { type: 'hide', time: new Date(0, 0, 0, 0, 3) }, { type: 'show', time: new Date(0, 0, 0, 0, 5) }, { type: 'hide', time: new Date(0, 0, 0, 0, 7) }, { type: 'show', time: new Date(0, 0, 0, 0, 14) }, { type: 'hide', time: new Date(0, 0, 0, 0, 16) }, ]; var currentDate = new Date(0, 0, 0, 0, 20); var inscreen = event_time_1.calculateEventTime({ times: times, screenEvents: screenEvents, now: currentDate, }); expect(inscreen).toEqual(420000); }); it('should support a completely illogical series of events', function () { var times = [ { type: 'stop', time: new Date(0, 0, 0, 0, 0) }, { type: 'start', time: new Date(0, 0, 0, 0, 2) }, { type: 'start', time: new Date(0, 0, 0, 0, 2) }, { type: 'start', time: new Date(0, 0, 0, 0, 2) }, { type: 'start', time: new Date(0, 0, 0, 0, 3) }, { type: 'stop', time: new Date(0, 0, 0, 0, 19) }, { type: 'stop', time: new Date(0, 0, 0, 0, 20) }, ]; var screenEvents = [ { type: 'show', time: new Date(0, 0, 0, 0, 1) }, { type: 'hide', time: new Date(0, 0, 0, 0, 4) }, { type: 'show', time: new Date(0, 0, 0, 0, 5) }, { type: 'show', time: new Date(0, 0, 0, 0, 5) }, { type: 'show', time: new Date(0, 0, 0, 0, 5) }, { type: 'show', time: new Date(0, 0, 0, 0, 5) }, { type: 'hide', time: new Date(0, 0, 0, 0, 7) }, { type: 'hide', time: new Date(0, 0, 0, 0, 8) }, { type: 'hide', time: new Date(0, 0, 0, 0, 9) }, { type: 'show', time: new Date(0, 0, 0, 0, 14) }, { type: 'hide', time: new Date(0, 0, 0, 0, 16) }, ]; var currentDate = new Date(0, 0, 0, 0, 20); var inscreen = event_time_1.calculateEventTime({ times: times, screenEvents: screenEvents, now: currentDate, }); expect(inscreen).toEqual(360000); }); }); //# sourceMappingURL=event-time.test.js.map