mediawiki-extensions-Popups/resources/ext.popups/experiment.js
Sam Smith 19349c0108 Add BOOT action
Changes:
* Make grunt:qunit run all QUnit tests in those modules whose names
  being with "ext.popups".
* Add ext.popups/index.js, which initialises the mw.popups namespace.
* Add ext.popups/userSettings.js, which contains the code that deals
  with interacting with the User Agent's storage.
* Add ext.popups/experiment.js, which contains the code that that
  decides whether or not the user is in the experiment condition.
* Add tests for both units, converting existing tests where appropriate.
* Remove the associated code from ext.popups.core/ext.popups.core.js.
* Finally, dispatch the BOOT action against the store in
  ext.popups/boot.js.

Change-Id: I697207677304bd49c7cfe1d37bb0a4af7810f387
2016-11-09 10:40:59 +00:00

50 lines
1.5 KiB
JavaScript

( function ( mw ) {
/**
* Given the global state of the application, creates a function which gets
* whether or not the user should have Link Previews enabled, i.e. whether
* they are in the experiment condition.
*
* The user is in the experiment condition if:
* * They've enabled Popups by click "Enable previews" in the footer menu.
* * They've enabled Popups as a beta feature.
* * They aren't in the control bucket of the experiment.
*
* @param {mw.Map} config
* @param {mw.user} user The `mw.user` singleton instance
* @param {Object} userSettings An object returned by
* `ext.popups.createUserSettings`, from which the user's token will be
* retrieved
*
* @return {Function}
*/
mw.popups.createExperiment = function ( config, user, userSettings ) {
return function () {
var experimentConfig = config.get( 'wgPopupsExperimentConfig' );
if ( userSettings.hasIsEnabled() ) {
return userSettings.getIsEnabled();
}
if ( config.get( 'wgPopupsExperiment', false ) === false ) {
return false;
}
if ( user.isAnon() ) {
if ( !experimentConfig ) {
return false;
}
// FIXME: mw.experiments should expose the CONTROL_BUCKET constant, e.g.
// `mw.experiments.CONTROL_BUCKET`.
return mw.experiments.getBucket( experimentConfig, userSettings.getToken() ) !== 'control';
} else {
// Logged in users are in condition depending on the beta feature flag
// instead of bucketing
return config.get( 'wgPopupsExperimentIsBetaFeatureEnabled', false );
}
};
};
}( mediaWiki ) );