Merge "[qunit] Use strict and wrap test scope"

This commit is contained in:
jenkins-bot 2024-12-20 11:23:17 +00:00 committed by Gerrit Code Review
commit 20ede4b0f4
3 changed files with 344 additions and 323 deletions

View file

@ -1,12 +1,15 @@
function createStubTitle( fragment = null ) { 'use strict';
( function () {
function createStubTitle( fragment = null ) {
return { return {
getFragment() { getFragment() {
return fragment; return fragment;
} }
}; };
} }
( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ? ( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ?
QUnit.module : QUnit.module :
QUnit.module.skip )( 'ext.cite.referencePreviews#createReferenceGateway', { QUnit.module.skip )( 'ext.cite.referencePreviews#createReferenceGateway', {
beforeEach: function () { beforeEach: function () {
@ -51,9 +54,9 @@ function createStubTitle( fragment = null ) {
this.$sourceElement.parent().remove(); this.$sourceElement.parent().remove();
this.$references.remove(); this.$references.remove();
} }
} ); } );
QUnit.test( 'Reference preview gateway returns the correct data', function ( assert ) { QUnit.test( 'Reference preview gateway returns the correct data', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'cite note-1' ); title = createStubTitle( 'cite note-1' );
@ -69,9 +72,9 @@ QUnit.test( 'Reference preview gateway returns the correct data', function ( ass
} }
); );
} ); } );
} ); } );
QUnit.test( 'Reference preview gateway accepts alternative text node class name', function ( assert ) { QUnit.test( 'Reference preview gateway accepts alternative text node class name', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'cite note-2' ); title = createStubTitle( 'cite note-2' );
@ -87,9 +90,9 @@ QUnit.test( 'Reference preview gateway accepts alternative text node class name'
} }
); );
} ); } );
} ); } );
QUnit.test( 'Reference preview gateway accepts duplicated types', function ( assert ) { QUnit.test( 'Reference preview gateway accepts duplicated types', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'cite note-3' ); title = createStubTitle( 'cite note-3' );
@ -105,9 +108,9 @@ QUnit.test( 'Reference preview gateway accepts duplicated types', function ( ass
} }
); );
} ); } );
} ); } );
QUnit.test( 'Reference preview gateway ignores conflicting types', function ( assert ) { QUnit.test( 'Reference preview gateway ignores conflicting types', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'cite note-4' ); title = createStubTitle( 'cite note-4' );
@ -123,9 +126,9 @@ QUnit.test( 'Reference preview gateway ignores conflicting types', function ( as
} }
); );
} ); } );
} ); } );
QUnit.test( 'Reference preview gateway returns source element id', function ( assert ) { QUnit.test( 'Reference preview gateway returns source element id', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'cite note-1' ); title = createStubTitle( 'cite note-1' );
@ -141,34 +144,43 @@ QUnit.test( 'Reference preview gateway returns source element id', function ( as
} }
); );
} ); } );
} ); } );
QUnit.test( 'Reference preview gateway rejects non-existing references', function ( assert ) { QUnit.test( 'Reference preview gateway rejects non-existing references', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'undefined' ); title = createStubTitle( 'undefined' );
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => { return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => {
assert.true( false, 'It should not resolve' ); assert.true( false, 'It should not resolve' );
} ).catch( ( result ) => { } ).catch( ( result ) => {
assert.propEqual( result, { textStatus: 'abort', textContext: 'Footnote not found or empty', xhr: { readyState: 0 } } ); assert.propEqual( result, {
textStatus: 'abort',
textContext: 'Footnote not found or empty',
xhr: { readyState: 0 }
} );
} );
} ); } );
} );
QUnit.test( 'Reference preview gateway rejects all-whitespace references', function ( assert ) { QUnit.test( 'Reference preview gateway rejects all-whitespace references', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'cite note-5' ); title = createStubTitle( 'cite note-5' );
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => { return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => {
assert.true( false, 'It should not resolve' ); assert.true( false, 'It should not resolve' );
} ).catch( ( result ) => { } ).catch( ( result ) => {
assert.propEqual( result, { textStatus: 'abort', textContext: 'Footnote not found or empty', xhr: { readyState: 0 } } ); assert.propEqual( result, {
textStatus: 'abort',
textContext: 'Footnote not found or empty',
xhr: { readyState: 0 }
} );
} );
} ); } );
} );
QUnit.test( 'Reference preview gateway is abortable', function ( assert ) { QUnit.test( 'Reference preview gateway is abortable', function ( assert ) {
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(),
title = createStubTitle( 'cite note-1' ), title = createStubTitle( 'cite note-1' ),
promise = gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ); promise = gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] );
assert.strictEqual( typeof promise.abort, 'function' ); assert.strictEqual( typeof promise.abort, 'function' );
} ); } );
}() );

View file

@ -1,12 +1,15 @@
function createStubUserSettings( expectEnabled ) { 'use strict';
( function () {
function createStubUserSettings( expectEnabled ) {
return { return {
isPreviewTypeEnabled() { isPreviewTypeEnabled() {
return expectEnabled !== false; return expectEnabled !== false;
} }
}; };
} }
function createStubUser( isAnon, options ) { function createStubUser( isAnon, options ) {
return { return {
isNamed() { isNamed() {
return !isAnon; return !isAnon;
@ -16,15 +19,15 @@ function createStubUser( isAnon, options ) {
}, },
options options
}; };
} }
const options = { get: () => '1' }; const options = { get: () => '1' };
( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ? ( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ?
QUnit.module : QUnit.module :
QUnit.module.skip )( 'ext.cite.referencePreviews#isReferencePreviewsEnabled' ); QUnit.module.skip )( 'ext.cite.referencePreviews#isReferencePreviewsEnabled' );
QUnit.test( 'relevant combinations of anonymous flags', ( assert ) => { QUnit.test( 'relevant combinations of anonymous flags', ( assert ) => {
[ [
{ {
testCase: 'enabled for an anonymous user', testCase: 'enabled for an anonymous user',
@ -52,7 +55,8 @@ QUnit.test( 'relevant combinations of anonymous flags', ( assert ) => {
isNamed: () => !data.isAnon && !data.isIPMasked, isNamed: () => !data.isAnon && !data.isIPMasked,
isAnon: () => data.isAnon, isAnon: () => data.isAnon,
options: { options: {
get: () => {} get: () => {
}
} }
}, },
isPreviewTypeEnabled = () => { isPreviewTypeEnabled = () => {
@ -76,9 +80,9 @@ QUnit.test( 'relevant combinations of anonymous flags', ( assert ) => {
data.testCase data.testCase
); );
} ); } );
} ); } );
QUnit.test( 'it should display reference previews when conditions are fulfilled', ( assert ) => { QUnit.test( 'it should display reference previews when conditions are fulfilled', ( assert ) => {
const user = createStubUser( false, options ), const user = createStubUser( false, options ),
userSettings = createStubUserSettings( false ), userSettings = createStubUserSettings( false ),
config = new Map(); config = new Map();
@ -89,9 +93,9 @@ QUnit.test( 'it should display reference previews when conditions are fulfilled'
require( 'ext.cite.referencePreviews' ).private.isReferencePreviewsEnabled( user, userSettings, config ), require( 'ext.cite.referencePreviews' ).private.isReferencePreviewsEnabled( user, userSettings, config ),
'If the user is logged in and the user is in the on group, then it\'s enabled.' 'If the user is logged in and the user is in the on group, then it\'s enabled.'
); );
} ); } );
QUnit.test( 'it should not be enabled when the global is disabling it', ( assert ) => { QUnit.test( 'it should not be enabled when the global is disabling it', ( assert ) => {
const user = createStubUser( false ), const user = createStubUser( false ),
userSettings = createStubUserSettings( false ), userSettings = createStubUserSettings( false ),
config = new Map(); config = new Map();
@ -103,4 +107,5 @@ QUnit.test( 'it should not be enabled when the global is disabling it', ( assert
null, null,
'Reference Previews is disabled.' 'Reference Previews is disabled.'
); );
} ); } );
}() );

View file

@ -1,7 +1,10 @@
let createReferencePreview; 'use strict';
const previewTypes = { TYPE_REFERENCE: 'reference' };
( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ? ( function () {
let createReferencePreview;
const previewTypes = { TYPE_REFERENCE: 'reference' };
( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ?
QUnit.module : QUnit.module :
QUnit.module.skip )( 'ext.cite.referencePreviews#renderer', { QUnit.module.skip )( 'ext.cite.referencePreviews#renderer', {
before() { before() {
@ -15,9 +18,9 @@ const previewTypes = { TYPE_REFERENCE: 'reference' };
} ) ); } ) );
this.sandbox.stub( mw.html, 'escape', ( str ) => str && str.replace( /'/g, '&apos;' ).replace( /</g, '&lt;' ) ); this.sandbox.stub( mw.html, 'escape', ( str ) => str && str.replace( /'/g, '&apos;' ).replace( /</g, '&lt;' ) );
} }
} ); } );
QUnit.test( 'createReferencePreview(model)', ( assert ) => { QUnit.test( 'createReferencePreview(model)', ( assert ) => {
const model = { const model = {
url: '#custom_id', url: '#custom_id',
extract: 'Custom <i>extract</i> with an <a href="/wiki/Internal">internal</a> and an <a href="//wikipedia.de" class="external">external</a> link', extract: 'Custom <i>extract</i> with an <a href="/wiki/Internal">internal</a> and an <a href="//wikipedia.de" class="external">external</a> link',
@ -42,9 +45,9 @@ QUnit.test( 'createReferencePreview(model)', ( assert ) => {
1, 1,
'only external links open in new tabs' 'only external links open in new tabs'
); );
} ); } );
QUnit.test( 'createReferencePreview default title', ( assert ) => { QUnit.test( 'createReferencePreview default title', ( assert ) => {
const model = { const model = {
url: '', url: '',
extract: '', extract: '',
@ -56,9 +59,9 @@ QUnit.test( 'createReferencePreview default title', ( assert ) => {
$( preview.el ).find( '.mwe-popups-title' ).text().trim(), $( preview.el ).find( '.mwe-popups-title' ).text().trim(),
'<cite-reference-previews-reference>' '<cite-reference-previews-reference>'
); );
} ); } );
QUnit.test( 'createReferencePreview updates fade-out effect on scroll', ( assert ) => { QUnit.test( 'createReferencePreview updates fade-out effect on scroll', ( assert ) => {
const model = { const model = {
url: '', url: '',
extract: '', extract: '',
@ -71,9 +74,9 @@ QUnit.test( 'createReferencePreview updates fade-out effect on scroll', ( assert
assert.false( $extract.children()[ 0 ].isScrolling ); assert.false( $extract.children()[ 0 ].isScrolling );
assert.false( $extract.hasClass( 'mwe-popups-fade-out' ) ); assert.false( $extract.hasClass( 'mwe-popups-fade-out' ) );
} ); } );
QUnit.test( 'createReferencePreview collapsible/sortable handling', ( assert ) => { QUnit.test( 'createReferencePreview collapsible/sortable handling', ( assert ) => {
const model = { const model = {
url: '', url: '',
extract: '<table class="mw-collapsible"></table>' + extract: '<table class="mw-collapsible"></table>' +
@ -89,4 +92,5 @@ QUnit.test( 'createReferencePreview collapsible/sortable handling', ( assert ) =
$( preview.el ).find( '.mwe-collapsible-placeholder' ).text(), $( preview.el ).find( '.mwe-collapsible-placeholder' ).text(),
'<cite-reference-previews-collapsible-placeholder>' '<cite-reference-previews-collapsible-placeholder>'
); );
} ); } );
}() );