mediawiki-extensions-Popups/tests/node-qunit/instrumentation/eventLogging.test.js
Sam Smith ba3c0b7f76 Hygiene: i13n: Return false over not sampling
Previously, if the browser didn't support the Beacon API, then
instrumentation/eventLogging#isEnabled would bucket the user with a
sampling rate of 0, which is equivalent to returning false. This change
simply does the latter.

Additional changes:
* Update the documented module names of the instrumentation/eventLogging
  and statsv modules.

Bug: T168847
Change-Id: I7ae5c10da42ca614b5b1a6619f9555e5665344cf
2017-07-26 17:06:58 +00:00

94 lines
2.1 KiB
JavaScript

var isEnabled = require( '../../../src/instrumentation/eventLogging' ).isEnabled,
stubs = require( '../stubs' );
QUnit.module( 'ext.popups/instrumentation/eventLogging', {
beforeEach: function () {
this.config = stubs.createStubMap();
this.config.set( 'wgPopupsSchemaSamplingRate', 1 );
this.window = {
navigator: {
sendBeacon: function () {}
}
};
this.experiments = {
weightedBoolean: this.sandbox.stub()
};
this.user = stubs.createStubUser();
// Helper function that DRYs up the tests below.
this.isEnabled = function () {
return isEnabled(
this.user,
this.config,
this.experiments,
this.window
);
};
}
} );
QUnit.test( 'it should use wgPopupsSchemaSamplingRate as the sampling rate', function ( assert ) {
this.isEnabled();
assert.ok( this.experiments.weightedBoolean.calledOnce );
assert.deepEqual(
this.experiments.weightedBoolean.getCall( 0 ).args,
[
'ext.Popups.instrumentation.eventLogging',
this.config.get( 'wgPopupsSchemaSamplingRate' ),
this.user.sessionId()
]
);
// ---
this.config.delete( 'wgPopupsSchemaSamplingRate' );
this.isEnabled();
assert.strictEqual(
this.experiments.weightedBoolean.getCall( 1 ).args[ 1 ],
0,
'The bucketing rate should be 0 by default.'
);
} );
QUnit.test( 'it should return false when sendBeacon isn\'t supported', function ( assert ) {
var window = {};
assert.notOk( isEnabled( this.user, this.config, this.experiments, window ) );
// ---
window.navigator = {
sendBeacon: 'NOT A FUNCTION'
};
assert.notOk( isEnabled( this.user, this.config, this.experiments, window ) );
} );
QUnit.test( 'it should return the weighted boolean', function ( assert ) {
this.experiments.weightedBoolean.returns( true );
assert.ok( this.isEnabled() );
// ---
this.experiments.weightedBoolean.returns( false );
assert.notOk( this.isEnabled() );
} );
QUnit.test( 'it should respect the debug flag', function ( assert ) {
this.config.set( 'wgPopupsSchemaSamplingRate', 0 );
this.config.set( 'debug', false );
assert.notOk( this.isEnabled() );
this.config.set( 'debug', true );
assert.ok( this.isEnabled() );
} );