All files / test/analytics AutocollectorTest.ts

100% Statements 52/52
50% Branches 2/4
100% Functions 9/9
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 911x 1x 1x 1x     1x     1x 5x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x       1x     1x 1x       1x     1x 1x       1x     1x 1x     1x   1x 1x   1x     1x 1x 1x 1x 1x       1x     1x 1x 1x 1x     1x 1x     1x 1x 1x 1x 1x       1x  
import { TestClass } from '../TestClass';
import * as sinon from 'sinon';
import Autocollector from '../../src/analytics/Autocollector';
import Analytics from '../../src/Analytics';
import { IView } from '../../src/Index';
 
export class AutocollectorTest extends TestClass {
    private _autoCollector: Autocollector;
 
    public testInitialize() {
        this._autoCollector = new Autocollector();
    }
 
    public registerTests() {
        this.testCase({
            name: 'Verify screen resoltion',
            test: () => {
                const doc = document;
                const win = window;
                const result = this._autoCollector._getScreenResolution();
                QUnit.assert.equal(result.h, screen.height);
                QUnit.assert.equal(result.w, screen.width);
                QUnit.assert.equal(result.cd, screen.colorDepth);
                QUnit.assert.equal(result.vW,  Math.max(doc.documentElement.clientWidth, win.innerWidth || 0));
                QUnit.assert.equal(result.vH, Math.max(doc.documentElement.clientHeight, win.innerHeight || 0));
                QUnit.assert.equal(result.sR, screen.height + 'X' + screen.width );
            }
        });
 
        this.testCase({
            name: 'Verify getReferrer',
            test: () => {
                const result = this._autoCollector._getReferrer(document);
                QUnit.assert.equal(result.uri, document.referrer);
            }
        });
 
        this.testCase({
            name: 'Verify getPageNameFromPath',
            test: () => {
                const result = this._autoCollector._getPageNameFromPath('/test/page/first.html');
                QUnit.assert.equal(result, 'first.html');
            }
        });
 
        this.testCase({
            name: 'Verify autocollecting views',
            test: () => {
                const analyticsStub: sinon.SinonStubbedInstance<Analytics> | any  = sinon.createStubInstance<Analytics>(Analytics);
                this._autoCollector._autocollect(analyticsStub, {
                    view: false
                });                
                QUnit.assert.notOk((analyticsStub as sinon.SinonStubbedInstance<Analytics>).trackView.calledOnce);
                let viewSignal: IView;
                (analyticsStub as sinon.SinonStubbedInstance<Analytics>).trackView.callsFake((arg) => {
                    viewSignal = arg as IView;
                });
                this._autoCollector._autocollect(analyticsStub, {
                    view: true
                });
                QUnit.assert.ok((analyticsStub as sinon.SinonStubbedInstance<Analytics>).trackView.calledOnce);
                QUnit.assert.equal(viewSignal.uri, location.href);
                QUnit.assert.equal(viewSignal.title, document.title.substring(0, 150));
                QUnit.assert.ok(viewSignal.referrer);
                QUnit.assert.ok(viewSignal.name);
            }
        });
 
        this.testCase({
            name: 'Verify autocollecting clicks adds eventlisteners',
            test: () => {
                const addSpy = sinon.spy(window, 'addEventListener');
                const removeSpy = sinon.spy(window, 'removeEventListener');
                const analyticsStub: sinon.SinonStubbedInstance<Analytics> | any  = sinon.createStubInstance<Analytics>(Analytics);    
                this._autoCollector._autocollect(analyticsStub, {
                    click: false
                });
                QUnit.assert.notOk(addSpy.calledOnce);
                this._autoCollector._autocollect(analyticsStub, {
                    click: true
                });
                QUnit.assert.ok(addSpy.calledTwice);
                this._autoCollector._removeAllListeners();
                QUnit.assert.ok(removeSpy.calledTwice);
                removeSpy.restore();
                addSpy.restore();               
            }
        });
    }
}