mediawiki-extensions-Popups/tests/node-qunit/stubs.js

83 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
Tests: Migrate processLinks.test.js to node-qunit Tests are basically unchanged, except for some stubs on beforeEach. Supporting changes: * Bring stubs from the mediawiki library for mw.Uri, mw.Title.newFromText and mw.RegExp into stubs.js * Remove hook onResourceLoaderTestModules given there are no resource loader test modules after migrating processLinks.test.js Why bring stubs from real source? This is not optimal. It could be the case that the stubs would need to be updated at some point in the future. That's why in the comment of each stub, it is specified where it came from, and what was changed to make it work. It is not optimal but it should help with a future update if necessary. Also checked the history of the stubs and these three stubs are very stable with a small commits per year, usually adding some extra functionality (not breaking changes) (the rest of the commits are docs/format stuff), so the core behavior that we rely on here shouldn't change in a fundamental way. See the github links: * https://github.com/wikimedia/mediawiki/commits/master/resources/src/mediawiki/mediawiki.Uri.js * https://github.com/wikimedia/mediawiki/commits/master/resources/src/mediawiki/mediawiki.Title.js * https://github.com/wikimedia/mediawiki/commits/master/resources/src/mediawiki/mediawiki.RegExp.js Right now this stubs allow us to bring the test to run in isolation in node. The initial plan was to do change the test to be less test-case oriented with dependencies on mediawiki.*.js and not to bring fake "real" stubs, but after looking into it, given that: 1. the test cases in the test seem pretty informative showing the kind of links that popups accepts 2. the stubs are acceptably easy to bring in, and are pretty stable I decided to go with this approach initially to finish the migration without changing the meaning of the tests. If we want to remove the stubs and morph the test to verify stub calls and move the test cases to documentation on the source, I'll tackle that on a future commit. Bug: T160406 Change-Id: Ieea378c9b7fec9116222b4a099c226d1f1131f65
2017-04-25 11:58:41 +00:00
* 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} name Page name without namespace prefix
* @param {string|null} [fragment]
* @return {Object}
*/
export function createStubTitle( namespace, name, fragment = null ) {
return {
namespace,
getPrefixedDb() {
return ( namespace ? `Namespace ${ namespace }:` : '' ) + name;
},
getNameText() {
return name;
},
getNamespaceId() {
return namespace;
},
getUrl() {
return `/wiki/${ this.getPrefixedDb() }`;
},
getFragment() {
return fragment;
}
};
}