mediawiki-extensions-Popups/tests/node-qunit/getPageviewTracker.test.js
jdlrobson e6202b6ce4 VirtualPageViews bypass EventLogging for logging virtual events
We are using EventLogging to track page views not user behaviour.
This is an exception to the rule and requires special handling.

Bug: T190188
Change-Id: If096ccaf0ac884d57744ed57e2f26b51446de2d7
2018-03-29 16:53:20 +00:00

60 lines
1.9 KiB
JavaScript

/* global Promise */
import getPageviewTracker, { getSendBeacon } from '../../src/getPageviewTracker';
QUnit.module( 'ext.popups#getPageviewTracker', {
beforeEach: function () {
this.makeBeaconUrl = this.sandbox.stub();
this.prepare = this.sandbox.stub();
this.trackerGetter = () => ( { makeBeaconUrl: this.makeBeaconUrl,
prepare: this.prepare } );
this.loader = () => Promise.resolve();
}
} );
const enabledConfig = {
get: () => true
};
QUnit.test( 'getPageviewTracker', function ( assert ) {
const loader = this.sandbox.stub();
const sendBeacon = this.sandbox.stub();
const data = { foo: 1 };
const tracker = getPageviewTracker( enabledConfig,
loader, this.trackerGetter, sendBeacon );
loader.resolves();
return tracker( 'event.VirtualPageView', data ).then( () => {
assert.ok( loader.calledOnce, 'loader called once' );
assert.ok( loader.calledWith( [ 'ext.eventLogging', 'schema.VirtualPageView' ] ),
'appropriate code is loaded' );
assert.ok( this.prepare.calledWith( 'VirtualPageView', data ),
'mw.eventLog.prepare called appropriately' );
assert.ok( this.makeBeaconUrl.calledOnce,
'makeBeacon called with result of prepare' );
assert.ok( sendBeacon.calledOnce,
'sendBeacon called with url from makeBeaconUrl' );
} );
} );
QUnit.test( 'getSendBeacon', function ( assert ) {
let success = false;
const navtr = {
successful: true,
sendBeacon: function () {
// This local variable helps test context. Don't refactor.
success = this.successful;
}
};
const sendBeacon = getSendBeacon( navtr );
sendBeacon();
assert.ok( success, 'native sendBeacon is used when available and run in appropriate context' );
} );
QUnit.test( 'getSendBeacon (fallback)', function ( assert ) {
const spy = this.sandbox.spy( document, 'createElement' );
const sendBeacon = getSendBeacon( {} );
sendBeacon();
assert.ok( spy.calledOnce, 'an img element is used as fallback' );
} );