mediawiki-extensions-Popups/tests/node-qunit/isPagePreviewsEnabled.test.js
Thiemo Kreuz ddf574afa3 Remove not needed userSettings.hasIsEnabled()
Note how getIsEnabled() is documented: "if the user hasn't
previously enabled or disabled Page Previews […] then they
are treated as if they have enabled them."

In other words: The idea that the default should be true is
encoded twice in this code. This is just not necessary. We can
remove one without loosing anything.

Motivtion: Simplifying the code and reducing the package size.

Since the code fundamentally depends on this default value
anyway, we can clear the users localStorage when they decide
to go back to the default – instead of storing a "1" which
does the same as the default.

Change-Id: I2814a1e9269979918609162a508eeee6944d9e52
2021-04-08 11:57:52 +02:00

71 lines
1.9 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 (logged out)', {
beforeEach() {
this.user = stubs.createStubUser( /* isAnon = */ true );
}
} );
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 ),
userSettings = createStubUserSettings( false ),
config = new Map();
assert.ok(
isPagePreviewsEnabled( user, userSettings, 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 ),
userSettings = createStubUserSettings( false ),
config = new Map();
config.set( 'wgPopupsConflictsWithNavPopupGadget', true );
assert.notOk(
isPagePreviewsEnabled( user, userSettings, config ),
'Page Previews is disabled when it conflicts with the Navigation Popups Gadget.'
);
} );