mediawiki-extensions-Popups/tests/node-qunit/isEnabled.test.js
Baha 5d4cc8d15a Allow bucketing anons
* Logged in users bypass bucketing. They keep working as before.
* When Page Previews is configured as a beta feature, logged out users
  won't see the feature.
* If an anonymous user has enabled/disabled the feature using
  the settings cog then they will see or not see the feature
  depending on the value of their setting.
* The other anonymous users are bucketed. By default 90% of these
  users see the feature, the other 10% don't. These numbers can be
  controlled by the config variable `wgPopupsAnonsEnabledSamplingRate`.

Bug: T157700
Change-Id: I5307587b10f4849c4e82d3b064ff759121c2de67
2017-03-01 10:45:32 +00:00

106 lines
3 KiB
JavaScript

/* global Map: false */
var stubs = require( './stubs' ),
isEnabled = require( '../../src/isEnabled' );
function createStubUserSettings( isEnabled ) {
return {
hasIsEnabled: function () {
return isEnabled !== undefined;
},
getIsEnabled: function () {
return Boolean( isEnabled );
}
};
}
function createStubExperiments( isSampled ) {
return {
getBucket: function () {
return isSampled ? 'A' : 'control';
}
};
}
QUnit.module( 'ext.popups#isEnabled (logged out)', {
setup: function () {
this.user = stubs.createStubUser( /* isAnon = */ true );
}
} );
QUnit.test( 'is should handle logged out users', function ( assert ) {
var user = stubs.createStubUser( /* isAnon = */ true ),
cases,
i, testCase,
userSettings,
experiments,
config = new Map();
cases = [
[ undefined, true, true, 'When the user hasn\'t enabled or disabled' +
' the feature and the user is in the sample.' ],
[ undefined, false, false, 'When the user hasn\'t enabled or disabled' +
' the feature and the user is not in the sample.' ],
[ false, true, false, 'When the user has disabled the feature' +
' and the user is in the sample.'],
[ false, false, false, 'When the user has disabled the feature' +
' and the user is not in the sample.'],
[ true, true, true, 'When the user has enabled the feature' +
' and the user is in the sample.' ],
[ true, false, true, 'When the user has enabled the feature' +
' and the user is not in the sample.' ]
];
for ( i = 0; i < cases.length; i++ ) {
testCase = cases[ i ];
userSettings = createStubUserSettings( testCase[ 0 ] );
experiments = createStubExperiments( testCase[ 1 ] );
assert.equal(
isEnabled( user, userSettings, config, experiments ),
testCase[ 2 ],
testCase[ 3 ]
);
}
// ---
config.set( 'wgPopupsBetaFeature', true );
experiments = createStubExperiments( true );
assert.notOk(
isEnabled( user, userSettings, config, experiments ),
'When Page Previews is enabled as a beta feature, then it\'s not' +
' enabled for logged out users when they are in the sample.'
);
experiments = createStubExperiments( false );
assert.notOk(
isEnabled( user, userSettings, config, experiments ),
'When Page Previews is enabled as a beta feature, then it\'s not' +
' enabled for logged out users when they are not in the sample.'
);
} );
QUnit.test( 'it should handle logged in users', function ( assert ) {
var user = stubs.createStubUser( /* isAnon = */ false ),
userSettings = createStubUserSettings( false ),
experiments = createStubExperiments( true ),
config = new Map();
config.set( 'wgPopupsShouldSendModuleToUser', true );
assert.ok(
isEnabled( user, userSettings, config, experiments ),
'If the user is logged in and Page Previews has booted, then it\'s enabled.'
);
experiments = createStubExperiments( false );
assert.ok(
isEnabled( user, userSettings, config, experiments ),
'Anon sampling does not have an affect on logged in users.' +
'If the user is logged in and Page Previews has booted, then it\'s enabled.'
);
} );