2015-11-03 23:39:48 +00:00
|
|
|
( function ( M, $ ) {
|
2015-11-14 00:03:39 +00:00
|
|
|
var RelatedPagesGateway = mw.relatedPages.RelatedPagesGateway,
|
2015-11-03 23:39:48 +00:00
|
|
|
relatedPages = {
|
|
|
|
query: {
|
2015-11-25 14:34:11 +00:00
|
|
|
pages: [
|
|
|
|
{
|
|
|
|
pageid: 123,
|
2015-11-03 23:39:48 +00:00
|
|
|
title: 'Oh noes',
|
|
|
|
ns: 0,
|
|
|
|
thumbnail: {
|
|
|
|
source: 'http://placehold.it/200x100'
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 14:34:11 +00:00
|
|
|
]
|
2015-11-03 23:39:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
emptyRelatedPages = {
|
|
|
|
query: {
|
2015-11-25 14:34:11 +00:00
|
|
|
pages: []
|
2015-11-03 23:39:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-25 14:34:11 +00:00
|
|
|
QUnit.module( 'ext.relatedArticles.gateway', {
|
2015-11-03 23:39:48 +00:00
|
|
|
setup: function () {
|
|
|
|
this.api = new mw.Api();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Returns an array with the results when api responds', 2, function ( assert ) {
|
|
|
|
var gateway = new RelatedPagesGateway( this.api, 'Foo', null, true );
|
|
|
|
this.sandbox.stub( this.api, 'get' ).returns( $.Deferred().resolve( relatedPages ) );
|
|
|
|
|
|
|
|
gateway.getForCurrentPage( 1 ).then( function ( results ) {
|
|
|
|
assert.ok( $.isArray( results ), 'Results must be an array' );
|
|
|
|
assert.strictEqual( results[ 0 ].title, 'Oh noes' );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Empty related pages is handled fine.', 2, function ( assert ) {
|
|
|
|
var gateway = new RelatedPagesGateway( this.api, 'Foo', null, true );
|
|
|
|
this.sandbox.stub( this.api, 'get' ).returns( $.Deferred().resolve( emptyRelatedPages ) );
|
|
|
|
|
|
|
|
gateway.getForCurrentPage( 1 ).then( function ( results ) {
|
|
|
|
assert.ok( $.isArray( results ), 'Results must be an array' );
|
|
|
|
assert.strictEqual( results.length, 0 );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Empty related pages with no cirrus search is handled fine. No API request.', 3, function ( assert ) {
|
|
|
|
var gateway = new RelatedPagesGateway( this.api, 'Foo', [], false ),
|
|
|
|
spy = this.sandbox.stub( this.api, 'get' ).returns( $.Deferred().resolve( relatedPages ) );
|
|
|
|
|
|
|
|
gateway.getForCurrentPage( 1 ).then( function ( results ) {
|
|
|
|
assert.ok( $.isArray( results ), 'Results must be an array' );
|
|
|
|
assert.ok( !spy.called, 'API is not invoked' );
|
|
|
|
assert.strictEqual( results.length, 0 );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Related pages from editor curated content', 1, function ( assert ) {
|
|
|
|
var gateway = new RelatedPagesGateway( this.api, 'Foo', [ { title: 1 } ], false );
|
|
|
|
this.sandbox.stub( this.api, 'get' ).returns( $.Deferred().resolve( relatedPages ) );
|
|
|
|
|
|
|
|
gateway.getForCurrentPage( 1 ).then( function ( results ) {
|
|
|
|
assert.strictEqual( results.length, 1,
|
|
|
|
'API still hit despite cirrus being disabled.' );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-11-11 15:30:19 +00:00
|
|
|
QUnit.test( 'Ignore related pages from editor curated content', 1, function ( assert ) {
|
|
|
|
var wgRelatedArticles = [
|
|
|
|
'Bar',
|
|
|
|
'Baz',
|
|
|
|
'Qux'
|
|
|
|
],
|
|
|
|
gateway = new RelatedPagesGateway( this.api, 'Foo', wgRelatedArticles, true, true ),
|
|
|
|
spy;
|
|
|
|
|
|
|
|
spy = this.sandbox.stub( this.api, 'get' )
|
|
|
|
.returns( $.Deferred().resolve( relatedPages ) );
|
|
|
|
|
|
|
|
gateway.getForCurrentPage( 1 ).then( function () {
|
|
|
|
var parameters = spy.lastCall.args[ 0 ];
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
parameters.generator,
|
|
|
|
'search',
|
|
|
|
'it should hit the CirrusSearch API even though wgRelatedArticles is non-empty'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-11-03 23:39:48 +00:00
|
|
|
}( mw.mobileFrontend, jQuery ) );
|