mediawiki-extensions-Popups/tests/qunit/ext.popups/gateway.test.js
Sam Smith 722bfe12a5 Add gateway
Supporting changes:
* Automatically register JavaScript files in the
  tests/qunit/ext.popups/ directory.

Additional changes:
* Fix a grammatical error in the docblock for
  mw.popups.createExperiment.

Change-Id: Ieb00709e353f0b960375fdaa0ca0dcdf950f2eb9
2016-11-11 18:59:44 +00:00

156 lines
4.4 KiB
JavaScript

( function ( mw, $ ) {
/**
* @private
*
* @param {Object} data
*/
function createGateway( data ) {
var api = {
get: function () {
return $.Deferred()
.resolve( data )
.promise();
}
};
return mw.popups.createGateway( api );
}
QUnit.module( 'ext.popups/gateway', {
setup: function () {
}
} );
QUnit.test( 'it should handle the API request failing', function ( assert ) {
var getDeferred = this.getDeferred = $.Deferred(),
api = {
get: function () {
return getDeferred.promise();
}
},
fetchPreview = mw.popups.createGateway( api ),
done = assert.async( 1 );
fetchPreview( 'Foo' ).fail( function () {
assert.ok( true );
done();
} );
getDeferred.reject();
} );
QUnit.test( 'it should fail if the response is empty', function ( assert ) {
var cases,
done;
cases = [
{},
{
query: {}
},
{
query: {
pages: []
}
}
];
done = assert.async( cases.length );
$.each( cases, function ( _, data ) {
var fetchPreview = createGateway( data );
fetchPreview( 'Foo' ).fail( function () {
assert.ok( true );
done();
} );
} );
} );
QUnit.test( 'it should handle an API response', function ( assert ) {
var done = assert.async( 1 ),
fetchPreview;
fetchPreview = createGateway( {
batchcomplete: true,
query: {
pages: [ {
contentmodel: 'wikitext',
extract: 'Richard Paul "Rick" Astley (/\u02c8r\u026ak \u02c8\u00e6stli/; born 6 February 1966) is an English singer, songwriter, musician, and radio personality. His 1987 song, "Never Gonna Give You Up" was a No. 1 hit single in 25 countries. By the time of his retirement in 1993, Astley had sold approximately 40 million records worldwide.\nAstley made a comeback in 2007, becoming an Internet phenomenon when his video "Never Gonna Give You Up" became integral to the meme known as "rickrolling". Astley was voted "Best Act Ever" by Internet users at the...',
lastrevid: 748725726,
length: 32132,
fullurl: 'https://en.wikipedia.org/wiki/Rick_Astley',
editurl: 'https://en.wikipedia.org/w/index.php?title=Rick_Astley&action=edit',
canonicalurl: 'https://en.wikipedia.org/wiki/Rick_Astley',
ns: 0,
pageid: 447541,
pagelanguage: 'en',
pagelanguagedir: 'ltr',
pagelanguagehtmlcode: 'en',
revisions: [ {
timestamp: '2016-11-10T00:14:14Z'
} ],
thumbnail: {
height: 300,
source: 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Rick_Astley_-_Pepsifest_2009.jpg/200px-Rick_Astley_-_Pepsifest_2009.jpg',
width: 200
},
title: 'Rick Astley',
touched: '2016-11-10T00:14:14Z'
} ]
}
} );
fetchPreview( 'Rick Astley' ).done( function ( result ) {
assert.deepEqual( result, {
title: 'Rick Astley',
languageCode: 'en',
languageDirection: 'ltr',
url: 'https://en.wikipedia.org/wiki/Rick_Astley',
thumbnail: {
height: 300,
url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Rick_Astley_-_Pepsifest_2009.jpg/200px-Rick_Astley_-_Pepsifest_2009.jpg',
width: 200
},
lastModified: new Date( '2016-11-10T00:14:14Z' ),
extract: 'Richard Paul "Rick" Astley (/\u02c8r\u026ak \u02c8\u00e6stli/; born 6 February 1966) is an English singer, songwriter, musician, and radio personality. His 1987 song, "Never Gonna Give You Up" was a No. 1 hit single in 25 countries. By the time of his retirement in 1993, Astley had sold approximately 40 million records worldwide.\nAstley made a comeback in 2007, becoming an Internet phenomenon when his video "Never Gonna Give You Up" became integral to the meme known as "rickrolling". Astley was voted "Best Act Ever" by Internet users at the'
} );
done();
} );
} );
QUnit.test( 'it handle empty-ish extracts', function ( assert ) {
var cases = [
[ '', undefined ],
[ 'Extract...', 'Extract' ],
[ 'Extract.', 'Extract.' ],
[ '...', undefined ]
],
done = assert.async( cases.length );
$.each( cases, function ( _, testCase ) {
var fetchPreview = createGateway( {
query: {
pages: [ {
revisions: [ {
timestamp: '2016-11-11T18:28:43Z' // As data.revisions[0] is accessed.
} ],
extract: testCase[0]
} ]
}
} );
fetchPreview( 'Extract' ).done( function ( result ) {
assert.strictEqual( result.extract, testCase[1] );
done();
} );
} );
} );
}( mediaWiki, jQuery ) );