Change getPageSummary() to use mw.Title object instead of string

This is split from the current draft patch Ie0ccb03. This is part of a
series of very small patches that prepare the code for new types of popups.

Bug: T213415
Change-Id: I00d46a716c0e6ada82ffc0034a7dd5582363c657
This commit is contained in:
Thiemo Kreuz 2019-01-17 13:51:13 +01:00
parent dbb16dadc7
commit 2f2286921d
9 changed files with 30 additions and 18 deletions

Binary file not shown.

Binary file not shown.

View file

@ -110,7 +110,7 @@ export function fetch( gateway, title, el, token ) {
namespaceId = title.namespace;
return ( dispatch ) => {
const xhr = gateway.getPageSummary( titleText );
const xhr = gateway.getPageSummary( title );
dispatch( timedAction( {
type: types.FETCH_START,

View file

@ -28,7 +28,7 @@ const mw = mediaWiki,
* If the underlying request is successful and contains data about the page,
* then the resulting promise will resolve. If not, then it will reject.
*
* @typedef {Function(string): AbortPromise<PreviewModel>} GetPageSummary
* @typedef {Function(mw.Title): AbortPromise<PreviewModel>} GetPageSummary
*/
/**

View file

@ -62,8 +62,12 @@ export default function createMediaWikiApiGateway( api, config ) {
} );
}
/**
* @param {mw.Title} title
* @returns {AbortPromise<PreviewModel>}
*/
function getPageSummary( title ) {
const xhr = fetch( title );
const xhr = fetch( title.getPrefixedDb() );
return xhr.then( ( data ) => {
const page = extractPageFromResponse( data );
const plainTextExtract = formatPlainTextExtract( page );

View file

@ -48,12 +48,17 @@ export default function createRESTBaseGateway( ajax, config, extractParser ) {
} );
}
/**
* @param {mw.Title} title
* @returns {AbortPromise<PreviewModel>}
*/
function getPageSummary( title ) {
const xhr = fetch( title );
const titleText = title.getPrefixedDb(),
xhr = fetch( titleText );
return xhr.then( ( page ) => {
// Endpoint response may be empty or simply missing a title.
if ( !page || !page.title ) {
page = $.extend( true, page || {}, { title } );
page = $.extend( true, page || {}, { title: titleText } );
}
// And extract may be omitted if empty string
if ( page.extract === undefined ) {

View file

@ -4,7 +4,8 @@ import * as WaitModule from '../../src/wait';
import actionTypes from '../../src/actionTypes';
const mw = mediaWiki,
REFERRER = 'https://en.wikipedia.org/wiki/Kitten';
REFERRER = 'https://en.wikipedia.org/wiki/Kitten',
TEST_TITLE = createStubTitle( 1, 'Foo' );
function generateToken() {
return 'ABC';
@ -82,7 +83,7 @@ function setupWait( module ) {
* @return {void}
*/
function setupEl( module ) {
module.title = createStubTitle( 1, 'Foo' );
module.title = TEST_TITLE;
module.el = $( '<a>' ).eq( 0 );
}
@ -279,7 +280,7 @@ QUnit.test( 'it should fetch data from the gateway immediately', function ( asse
this.fetch();
assert.ok(
this.gateway.getPageSummary.calledWith( 'Foo' ),
this.gateway.getPageSummary.calledWith( TEST_TITLE ),
'The gateway was called with the correct arguments.'
);

View file

@ -1,3 +1,4 @@
import { createStubTitle } from '../stubs';
import { createModel } from '../../../src/preview/model';
import createMediaWikiApiGateway from '../../../src/gateway/mediawiki';
@ -166,7 +167,7 @@ QUnit.test( 'MediaWiki API gateway handles API failure', function ( assert ) {
},
gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS );
return gateway.getPageSummary( 'Test Title' ).catch( () => {
return gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) ).catch( () => {
assert.ok( true, 'The gateway threw an error.' );
} );
} );
@ -179,7 +180,7 @@ QUnit.test( 'MediaWiki API gateway returns the correct data', function ( assert
},
gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS );
return gateway.getPageSummary( 'Test Title' ).then( ( result ) => {
return gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) ).then( ( result ) => {
assert.deepEqual(
result,
MEDIAWIKI_API_RESPONSE_PREVIEW_MODEL,
@ -221,7 +222,7 @@ QUnit.test( 'MediaWiki API gateway handles missing pages', function ( assert ) {
},
gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS );
return gateway.getPageSummary( 'Test Title' ).then( ( result ) => {
return gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) ).then( ( result ) => {
assert.deepEqual(
result,
model,
@ -240,7 +241,7 @@ QUnit.test( 'MediaWiki API gateway is abortable', function ( assert ) {
},
gateway = createMediaWikiApiGateway( api, DEFAULT_CONSTANTS );
const xhr = gateway.getPageSummary( 'Test Title' );
const xhr = gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) );
const chain = xhr.then( () => {
assert.ok( false, 'It never calls a thenable after rejection' );

View file

@ -1,3 +1,4 @@
import { createStubTitle } from '../stubs';
import { createModel } from '../../../src/preview/model';
import createRESTBaseGateway from '../../../src/gateway/rest';
@ -357,7 +358,7 @@ QUnit.test( 'RESTBase gateway handles API failure', function ( assert ) {
.returns( $.Deferred().reject( { status: 500 } ).promise() ),
gateway = createRESTBaseGateway( api, {} );
return gateway.getPageSummary( 'Test Title' ).catch( () => {
return gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) ).catch( () => {
assert.ok( true, 'The gateway threw an error.' );
} );
} );
@ -367,7 +368,7 @@ QUnit.test( 'RESTBase gateway handles 404 as a failure', function ( assert ) {
.returns( $.Deferred().reject( { status: 404 } ).promise() ),
gateway = createRESTBaseGateway( api, {} );
return gateway.getPageSummary( 'Test Title' ).catch( () => {
return gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) ).catch( () => {
assert.ok( true, 'The gateway threw an error.' );
} );
} );
@ -379,7 +380,7 @@ QUnit.test( 'RESTBase gateway returns the correct data ', function ( assert ) {
gateway = createRESTBaseGateway(
api, DEFAULT_CONSTANTS, provideParsedExtract );
return gateway.getPageSummary( 'Test Title' ).then( ( result ) => {
return gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) ).then( ( result ) => {
assert.deepEqual(
result,
RESTBASE_RESPONSE_PREVIEW_MODEL,
@ -406,7 +407,7 @@ QUnit.test( 'RESTBase gateway handles missing extracts', function ( assert ) {
gateway = createRESTBaseGateway(
api, DEFAULT_CONSTANTS, provideParsedExtract );
return gateway.getPageSummary( 'Test Title with missing extract' )
return gateway.getPageSummary( createStubTitle( 1, 'Test Title with missing extract' ) )
.then( ( result ) => {
assert.strictEqual( result.title, 'Test Title with missing extract', 'Title' );
assert.strictEqual( result.extract, '!!', 'Extract' );
@ -419,7 +420,7 @@ QUnit.test( 'RESTBase gateway handles no content success responses', function (
gateway = createRESTBaseGateway(
api, DEFAULT_CONSTANTS, provideParsedExtract );
return gateway.getPageSummary( 'Test Title with empty response' )
return gateway.getPageSummary( createStubTitle( 1, 'Test Title with empty response' ) )
.then( ( result ) => {
assert.strictEqual( result.title, 'Test Title with empty response', 'Title' );
assert.strictEqual( result.extract, '!!', 'Extract' );
@ -439,7 +440,7 @@ QUnit.test( 'RESTBase gateway is abortable', function ( assert ) {
gateway = createRESTBaseGateway(
api, DEFAULT_CONSTANTS, provideParsedExtract );
const xhr = gateway.getPageSummary( 'Test Title' );
const xhr = gateway.getPageSummary( createStubTitle( 1, 'Test Title' ) );
const chain = xhr.then( () => {
assert.ok( false, 'It never calls a thenable after rejection' );