2021-04-16 11:06:13 +00:00
|
|
|
import * as stubs from './stubs';
|
2020-12-02 15:48:33 +00:00
|
|
|
import isReferencePreviewsEnabled from '../../src/isReferencePreviewsEnabled';
|
|
|
|
|
2021-04-15 07:20:00 +00:00
|
|
|
function createStubUserSettings( expectEnabled ) {
|
|
|
|
return {
|
|
|
|
isReferencePreviewsEnabled() {
|
|
|
|
return expectEnabled !== false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-16 11:06:13 +00:00
|
|
|
QUnit.module( 'ext.popups#isReferencePreviewsEnabled', {
|
|
|
|
beforeEach() {
|
|
|
|
mw.user = { options: { get: () => '1' } };
|
|
|
|
},
|
|
|
|
afterEach() {
|
|
|
|
mw.user = null;
|
|
|
|
}
|
|
|
|
} );
|
2021-04-16 11:23:10 +00:00
|
|
|
|
2021-05-03 16:55:16 +00:00
|
|
|
QUnit.test( 'all relevant combinations of flags', ( assert ) => {
|
|
|
|
[
|
|
|
|
{
|
|
|
|
testCase: 'enabled for an anonymous user',
|
|
|
|
wgPopupsReferencePreviews: true,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: false,
|
|
|
|
expected: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'turned off via the feature flag (anonymous user)',
|
|
|
|
wgPopupsReferencePreviews: false,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'not available because of a conflicting gadget (anonymous user)',
|
|
|
|
wgPopupsReferencePreviews: true,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: true,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'not available in the mobile skin (anonymous user)',
|
|
|
|
wgPopupsReferencePreviews: true,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: true,
|
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'manually disabled by the anonymous user',
|
|
|
|
wgPopupsReferencePreviews: true,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: true,
|
|
|
|
enabledByAnon: false,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'enabled for a registered user',
|
|
|
|
wgPopupsReferencePreviews: true,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: false,
|
|
|
|
enabledByAnon: false,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'turned off via the feature flag (registered user)',
|
|
|
|
wgPopupsReferencePreviews: false,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: false,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'not available because of a conflicting gadget (registered user)',
|
|
|
|
wgPopupsReferencePreviews: true,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: true,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: false,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testCase: 'not available in the mobile skin (registered user)',
|
|
|
|
wgPopupsReferencePreviews: true,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: true,
|
|
|
|
isAnon: false,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: true,
|
|
|
|
expected: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// TODO: This combination will make much more sense when the server-side
|
|
|
|
// wgPopupsReferencePreviews flag doesn't include the user's setting any more
|
|
|
|
testCase: 'manually disabled by the registered user',
|
|
|
|
wgPopupsReferencePreviews: false,
|
|
|
|
wgPopupsConflictsWithRefTooltipsGadget: false,
|
|
|
|
isMobile: false,
|
|
|
|
isAnon: false,
|
|
|
|
enabledByAnon: true,
|
|
|
|
enabledByRegistered: false,
|
|
|
|
expected: null
|
|
|
|
}
|
|
|
|
].forEach( ( data ) => {
|
|
|
|
const user = { isAnon: () => data.isAnon },
|
|
|
|
userSettings = {
|
|
|
|
isReferencePreviewsEnabled: () => data.isAnon ?
|
|
|
|
data.enabledByAnon :
|
|
|
|
assert.ok( false, 'not expected to be called' )
|
|
|
|
},
|
|
|
|
config = {
|
|
|
|
get: ( key ) => key === 'skin' && data.isMobile ? 'minerva' : data[ key ]
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( data.isAnon ) {
|
|
|
|
mw.user.options.get = () => assert.ok( false, 'not expected to be called' );
|
|
|
|
} else {
|
|
|
|
mw.user.options.get = () => data.enabledByRegistered ? '1' : '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
isReferencePreviewsEnabled( user, userSettings, config ),
|
|
|
|
data.expected,
|
|
|
|
data.testCase
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2020-12-02 15:48:33 +00:00
|
|
|
QUnit.test( 'it should display reference previews when conditions are fulfilled', ( assert ) => {
|
2021-04-16 11:06:13 +00:00
|
|
|
const user = stubs.createStubUser( false ),
|
2021-04-15 07:20:00 +00:00
|
|
|
userSettings = createStubUserSettings( false ),
|
2021-04-16 11:06:13 +00:00
|
|
|
config = new Map();
|
2020-12-02 15:48:33 +00:00
|
|
|
|
|
|
|
config.set( 'wgPopupsReferencePreviews', true );
|
|
|
|
config.set( 'wgPopupsConflictsWithRefTooltipsGadget', false );
|
|
|
|
|
|
|
|
assert.ok(
|
2021-04-15 07:20:00 +00:00
|
|
|
isReferencePreviewsEnabled( user, userSettings, config ),
|
2020-12-02 15:48:33 +00:00
|
|
|
'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 Reference Tooltips Gadget', ( assert ) => {
|
2021-04-16 11:06:13 +00:00
|
|
|
const user = stubs.createStubUser( false ),
|
2021-04-15 07:20:00 +00:00
|
|
|
userSettings = createStubUserSettings( false ),
|
2021-04-16 11:06:13 +00:00
|
|
|
config = new Map();
|
2020-12-02 15:48:33 +00:00
|
|
|
|
|
|
|
config.set( 'wgPopupsReferencePreviews', true );
|
|
|
|
config.set( 'wgPopupsConflictsWithRefTooltipsGadget', true );
|
|
|
|
|
|
|
|
assert.notOk(
|
2021-04-15 07:20:00 +00:00
|
|
|
isReferencePreviewsEnabled( user, userSettings, config ),
|
2020-12-02 15:48:33 +00:00
|
|
|
'Reference Previews is disabled.'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should not be enabled when the global is disabling it', ( assert ) => {
|
2021-04-16 11:06:13 +00:00
|
|
|
const user = stubs.createStubUser( false ),
|
2021-04-15 07:20:00 +00:00
|
|
|
userSettings = createStubUserSettings( false ),
|
2021-04-16 11:06:13 +00:00
|
|
|
config = new Map();
|
2020-12-02 15:48:33 +00:00
|
|
|
|
|
|
|
config.set( 'wgPopupsReferencePreviews', false );
|
|
|
|
config.set( 'wgPopupsConflictsWithRefTooltipsGadget', false );
|
|
|
|
|
|
|
|
assert.notOk(
|
2021-04-15 07:20:00 +00:00
|
|
|
isReferencePreviewsEnabled( user, userSettings, config ),
|
2020-12-02 15:48:33 +00:00
|
|
|
'Reference Previews is disabled.'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should not be enabled when minerva skin used', ( assert ) => {
|
2021-04-16 11:06:13 +00:00
|
|
|
const user = stubs.createStubUser( false ),
|
2021-04-15 07:20:00 +00:00
|
|
|
userSettings = createStubUserSettings( false ),
|
2021-04-16 11:06:13 +00:00
|
|
|
config = new Map();
|
2020-12-02 15:48:33 +00:00
|
|
|
|
|
|
|
config.set( 'wgPopupsReferencePreviews', true );
|
|
|
|
config.set( 'wgPopupsConflictsWithRefTooltipsGadget', false );
|
|
|
|
config.set( 'skin', 'minerva' );
|
|
|
|
|
|
|
|
assert.notOk(
|
2021-04-15 07:20:00 +00:00
|
|
|
isReferencePreviewsEnabled( user, userSettings, config ),
|
2020-12-02 15:48:33 +00:00
|
|
|
'Reference Previews is disabled.'
|
|
|
|
);
|
|
|
|
} );
|