mediawiki-extensions-Popups/tests/node-qunit/isPagePreviewsEnabled.test.js
Thiemo Kreuz c281bb9302 Clean up code enabling individual popup types
We still have 2 different mechanisms in place, maybe even 3:
* We simplify the CSS selector when we know a popup type is
  disabled, and it's impossible an anonymous user can enable it
  at run-time.
* We create that "initiallyEnabled" map that allows anonymous
  users to toggle the individual popup types at run-time.
* This map is also used to check if the footer link should be
  shown.
* There is also a wgPopupsReferencePreviews global that acts as
  a "kill switch". However, this is not a pure feature flag,
  but incorporates the user setting for registered users. This
  is currently partly redundant (checking
  `mw.user.options.get( 'popupsreferencepreviews' )` does the
  same) and can be removed later when the feature flag is not
  needed any more.

The footer link currently acts odd because anonymous users are
unable to enable ReferencePreviews, but get the footer link.

This patch introduces a 3-state model:
* `true` acts as before.
* `false` means a popup type is disabled, but anonymous users
  can enable it (i.e. this is the opt-out behavior for anonymous
  users).
* `null` means a popup type is not available at run-time, for
  nobody. Anonymous users can't do anything about this.
  Registered users must leave the page and change a setting.

Bug: T277640
Change-Id: Id8d1396c09cf0f706034a66f9cd3c880a8b33df8
2021-04-21 19:38:25 +02:00

72 lines
1.8 KiB
JavaScript

import * as stubs from './stubs';
import isPagePreviewsEnabled from '../../src/isPagePreviewsEnabled';
function createStubUserSettings( expectEnabled ) {
return {
isPagePreviewsEnabled() {
return expectEnabled !== false;
}
};
}
QUnit.module( 'ext.popups#isPagePreviewsEnabled', {
beforeEach() {
mw.user = { options: { get: () => '1' } };
},
afterEach() {
mw.user = null;
}
} );
QUnit.test( 'it should handle logged out users', ( assert ) => {
const user = stubs.createStubUser( /* isAnon = */ true ),
config = new Map();
const cases = [
/*
[
<isAnon>, <expected value of isPagePreviewsEnabled>, <test description>
]
*/
[ undefined, true, 'When the user hasn\'t enabled or disabled' +
' the feature.' ],
[ false, false, 'When the user has disabled the feature' ],
[ true, true, 'When the user has enabled the feature' ]
];
let userSettings;
for ( let i = 0; i < cases.length; i++ ) {
const testCase = cases[ i ];
userSettings = createStubUserSettings( testCase[ 0 ] );
assert.strictEqual(
isPagePreviewsEnabled( user, userSettings, config ),
testCase[ 1 ],
testCase[ 2 ]
);
}
} );
QUnit.test( 'it should handle logged in users', ( assert ) => {
const user = stubs.createStubUser( /* isAnon = */ false ),
config = new Map();
assert.ok(
isPagePreviewsEnabled( user, {}, config ),
'If the user is logged in and the user is in the on group, then it\'s enabled.'
);
} );
QUnit.test( 'it should handle the conflict with the Navigation Popups Gadget', ( assert ) => {
const user = stubs.createStubUser( /* isAnon = */ false ),
config = new Map();
config.set( 'wgPopupsConflictsWithNavPopupGadget', true );
assert.notOk(
isPagePreviewsEnabled( user, {}, config ),
'Page Previews is disabled when it conflicts with the Navigation Popups Gadget.'
);
} );