mediawiki-extensions-Popups/tests/node-qunit/stubs.js
Thiemo Kreuz 97a5d335d7 Replace rare {!…} and {?…} JSDoc syntax
This is documented at http://usejsdoc.org/tags-type.html, but not in many
other places, especially not in the JSDuck documentation.

The {!…} syntax means "can not be null". This is the default anyway.

The {?…} syntax means nullable. In a few situation is was used when a
parameter can be undefined. I decided to remove it everywhere and replace
it with {…|null} when appropriate, because this is much more explicit. Less
syntax to remember.

Note I'm intentionally not using the […] syntax when a parameter is followed
by non-optional parameters. Actually skipping a parameter in such a situation
would mess the parameter order up. Having optional parameters not at the end
is sometimes used as a feature in JavaScript code, but not in this codebase,
as far as I can see.

Change-Id: Ie370cfe08c32d1af5b0341951bed044fc3511c57
2019-01-24 21:00:45 +01:00

77 lines
1.5 KiB
JavaScript

/**
* Creates a **minimal** stub that can be used in place of an `mw.User`
* instance.
*
* @param {boolean} isAnon The return value of the `#isAnon`.
* @return {Object}
*/
export function createStubUser( isAnon ) {
return {
getPageviewToken() {
return '9876543210';
},
isAnon() {
return isAnon;
},
sessionId() {
return '0123456789';
}
};
}
/**
* Creates a **minimal** stub that can be used in place of an `mw.Map`
* instance.
*
* @return {mw.Map}
*/
export function createStubMap() {
const m = new Map(); /* global Map */
m.get = function ( key, fallback ) {
fallback = arguments.length > 1 ? fallback : null;
if ( typeof key === 'string' ) {
return m.has( key ) ? Map.prototype.get.call( m, key ) : fallback;
}
// Invalid selection key
return null;
};
return m;
}
/**
* Creates a stub that can be used as a replacement to mw.experiements
* @param {string} bucket getBucket will respond with this bucket.
* @return {Object}
*/
export function createStubExperiments( bucket ) {
return {
getBucket() {
return bucket;
}
};
}
/**
* Creates a **minimal** stub that can be used in place of an instance of
* `mw.Title`.
*
* @param {number} namespace
* @param {string} prefixedDb, e.g. Foo, or File:Bar.jpg
* @param {string|null} [fragment]
* @return {Object}
*/
export function createStubTitle( namespace, prefixedDb, fragment = null ) {
return {
namespace,
getPrefixedDb() {
return prefixedDb;
},
getUrl() {
return `/wiki/${ prefixedDb }`;
},
getFragment() {
return fragment;
}
};
}