mediawiki-extensions-Popups/tests/node-qunit/gateway/index.test.js
joakin 010a4d91a6 Hygiene: Simplify gateways
gateway/*/rest were copies of gateway/restProvider just passing
a different provider. Docs were the same, they were untested, and
looking at them they seemed like unnecessary abstraction.

This patch removes the plain vs html structure, and separates gateways
like before, by endpoint.

There is a light utility in gateway/restFormatters.js that adapts the
call from the rest gateway to use formatters.js functions. It needs
testing, that I'll add in the next patch.

The flow for creating a gateway ends up as follows:

1. index.js calls gateway/index#createGateway( mw.config )
2. createGateway chooses based on wgPopupsGateway and invokes
  * mediawiki.js#createMediaWikiApiGateway or
  * rest.js#createRESTBaseGateway w/ restFormatters.js#parsePlainTextResponse or
  * rest.js#createRESTBaseGateway w/ restFormatters.js#parseHTMLResponse

Changes:
* Removed src/gateway/{plain,html}/rest.js
  * Extracted formatter functions to src/gateway/restFormatters.js
* src/gateway/plain/mediawiki.js -> src/gateway/mediawiki.js
         * tests/node-qunit/gateway/plain/mediawiki.test.js ->
           tests/node-qunit/gateway/mediawiki.test.js
* gateway/restProvider{,.test}.js -> gateway/rest{,.test}.js
* Change gateway/index.js#createGateway to properly call the rest
  gateways with the rest formatters

Bug: T165018
Change-Id: Ia75695dfc192aad5bc581a68882514bad6c29646
2017-06-16 14:49:59 +02:00

48 lines
1.6 KiB
JavaScript

var mock = require( 'mock-require' ),
createGateway;
QUnit.module( 'gateway/index.js', {
beforeEach: function () {
jQuery.bracketedDevicePixelRatio = function () { return 1; };
mediaWiki.Api = function () {};
this.createMediaWikiApiGateway = this.sandbox.stub();
mock( '../../../src/gateway/mediawiki', this.createMediaWikiApiGateway );
this.createRESTBaseGateway = this.sandbox.stub();
mock( '../../../src/gateway/rest', this.createRESTBaseGateway );
createGateway = mock.reRequire( '../../../src/gateway' );
this.config = new Map(); /* global Map */
},
afterEach: function () {
jQuery.bracketedDevicePixelRatio = undefined;
mock.stop( '../../../src/gateway/mediawiki' );
mock.stop( '../../../src/gateway/rest' );
}
} );
QUnit.test( 'it uses mediawiki plain text gateway with wgPopupsGateway == "mwApiPlain"', function ( assert ) {
this.config.set( 'wgPopupsGateway', 'mwApiPlain' );
createGateway( this.config );
assert.ok( this.createMediaWikiApiGateway.called, 'MW plain text gateway called' );
} );
QUnit.test( 'it uses rest plain text gateway with wgPopupsGateway == "restbasePlain"', function ( assert ) {
this.config.set( 'wgPopupsGateway', 'restbasePlain' );
createGateway( this.config );
assert.ok( this.createRESTBaseGateway.called, 'REST plain text gateway called' );
} );
QUnit.test( 'it uses rest HTML gateway with wgPopupsGateway == "restbaseHTML"', function ( assert ) {
this.config.set( 'wgPopupsGateway', 'restbaseHTML' );
createGateway( this.config );
assert.ok( this.createRESTBaseGateway.called, 'REST HTML gateway called' );
} );