mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-24 07:34:11 +00:00
Tests: Migrate gateway/rest.test.js to node qunit
Additional changes: * Fix eslint errors in the test file * Mock global mw.Title usage in the test Change-Id: Ia2778457239184639150d03ab0f0a1ca597a862e
This commit is contained in:
parent
2ca5fed3ee
commit
4f71f6f740
171
tests/node-qunit/gateway/rest.test.js
Normal file
171
tests/node-qunit/gateway/rest.test.js
Normal file
|
@ -0,0 +1,171 @@
|
|||
var createModel = require( '../../../src/preview/model' ).createModel,
|
||||
createRESTBaseGateway = require( '../../../src/gateway/rest' ),
|
||||
DEFAULT_CONSTANTS = {
|
||||
THUMBNAIL_SIZE: 512
|
||||
},
|
||||
SVG_ORIGINAL_IMAGE = {
|
||||
source: 'https://upload.wikimedia.org/wikipedia/commons/8/8d/Ceiling_cat.svg',
|
||||
width: 800,
|
||||
height: 1000
|
||||
},
|
||||
RESTBASE_RESPONSE = {
|
||||
title: 'Barack Obama',
|
||||
extract: 'Barack Hussein Obama II born August 4, 1961) ...',
|
||||
thumbnail: {
|
||||
source: 'https://upload.wikimedia.org/someImage.jpg',
|
||||
width: 200,
|
||||
height: 250
|
||||
},
|
||||
originalimage: {
|
||||
source: 'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg',
|
||||
width: 800,
|
||||
height: 1000
|
||||
},
|
||||
lang: 'en',
|
||||
dir: 'ltr',
|
||||
timestamp: '2017-01-30T10:17:41Z',
|
||||
description: '44th President of the United States of America'
|
||||
},
|
||||
RESTBASE_RESPONSE_WITHOUT_IMAGE = {
|
||||
title: 'Barack Obama',
|
||||
extract: 'Barack Hussein Obama II born August 4, 1961) ...',
|
||||
lang: 'en',
|
||||
dir: 'ltr',
|
||||
timestamp: '2017-01-30T10:17:41Z',
|
||||
description: '44th President of the United States of America'
|
||||
},
|
||||
RESTBASE_RESPONSE_PREVIEW_MODEL = createModel(
|
||||
'Barack Obama',
|
||||
'url/Barack Obama', // Generated in the stub below
|
||||
'en',
|
||||
'ltr',
|
||||
'Barack Hussein Obama II born August 4, 1961) ...',
|
||||
{
|
||||
source: 'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg/512px-President_Barack_Obama.jpg',
|
||||
width: 800,
|
||||
height: 1000
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.module( 'gateway/rest', {
|
||||
beforeEach: function () {
|
||||
mediaWiki.Title = function ( title ) {
|
||||
this.getUrl = function () { return 'url/' + title; };
|
||||
};
|
||||
},
|
||||
afterEach: function () {
|
||||
mediaWiki.Title = null;
|
||||
}
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway is called with correct arguments', function ( assert ) {
|
||||
var getSpy = this.sandbox.spy(),
|
||||
gateway = createRESTBaseGateway( getSpy ),
|
||||
expectedOptions = {
|
||||
url: '/api/rest_v1/page/summary/' + encodeURIComponent( 'Test Title' ),
|
||||
headers: {
|
||||
Accept: 'application/json; charset=utf-8' +
|
||||
'profile="https://www.mediawiki.org/wiki/Specs/Summary/1.0.0"'
|
||||
}
|
||||
};
|
||||
|
||||
gateway.fetch( 'Test Title' );
|
||||
|
||||
assert.deepEqual( getSpy.getCall( 0 ).args[ 0 ], expectedOptions, 'options' );
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway is correctly converting the page data to a model ', function ( assert ) {
|
||||
var gateway = createRESTBaseGateway();
|
||||
|
||||
assert.deepEqual(
|
||||
gateway.convertPageToModel( RESTBASE_RESPONSE, 512 ),
|
||||
RESTBASE_RESPONSE_PREVIEW_MODEL
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway doesn\'t stretch thumbnails', function ( assert ) {
|
||||
var model,
|
||||
gateway = createRESTBaseGateway();
|
||||
|
||||
model = gateway.convertPageToModel( RESTBASE_RESPONSE, 2000 );
|
||||
|
||||
assert.equal(
|
||||
model.thumbnail.source,
|
||||
'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg/800px-President_Barack_Obama.jpg',
|
||||
'If the requested thumbnail size is bigger than the originalimage width the originalimage width is used'
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway stretches SVGs', function ( assert ) {
|
||||
var model,
|
||||
gateway = createRESTBaseGateway();
|
||||
|
||||
model = gateway.convertPageToModel( $.extend( {}, RESTBASE_RESPONSE, { originalimage: SVG_ORIGINAL_IMAGE } ),
|
||||
2000 );
|
||||
|
||||
assert.equal(
|
||||
model.thumbnail.source,
|
||||
'https://upload.wikimedia.org/wikipedia/commons/8/8d/Ceiling_cat.svg/2000px-Ceiling_cat.svg',
|
||||
'If the requested thumbnail size is bigger than the originalimage and its an SVG all is good'
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway handles the API failure', function ( assert ) {
|
||||
var deferred = $.Deferred(),
|
||||
api = this.sandbox.stub().returns( deferred.promise() ),
|
||||
gateway = createRESTBaseGateway( api ),
|
||||
done = assert.async( 1 );
|
||||
|
||||
gateway.getPageSummary( 'Test Title' ).fail( function () {
|
||||
assert.ok( true );
|
||||
done();
|
||||
} );
|
||||
|
||||
deferred.reject();
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway returns the correct data ', function ( assert ) {
|
||||
var api = this.sandbox.stub().returns(
|
||||
$.Deferred().resolve( RESTBASE_RESPONSE ).promise()
|
||||
),
|
||||
gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS ),
|
||||
done = assert.async( 1 );
|
||||
|
||||
gateway.getPageSummary( 'Test Title' ).done( function ( result ) {
|
||||
assert.deepEqual( result, RESTBASE_RESPONSE_PREVIEW_MODEL );
|
||||
done();
|
||||
} );
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway handles missing images ', function ( assert ) {
|
||||
var model,
|
||||
gateway = createRESTBaseGateway();
|
||||
model = gateway.convertPageToModel( RESTBASE_RESPONSE_WITHOUT_IMAGE, 300 );
|
||||
|
||||
assert.equal(
|
||||
model.originalimage,
|
||||
undefined,
|
||||
'If restbase handles missing image information'
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway handles missing pages ', function ( assert ) {
|
||||
var response = {
|
||||
type: 'https://mediawiki.org/wiki/HyperSwitch/errors/not_found',
|
||||
title: 'Not found.',
|
||||
method: 'get',
|
||||
detail: 'Page or revision not found.',
|
||||
uri: '/en.wikipedia.org/v1/page/summary/Missing_page'
|
||||
},
|
||||
api = this.sandbox.stub().returns(
|
||||
$.Deferred().rejectWith( response ).promise()
|
||||
),
|
||||
gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS ),
|
||||
done = assert.async( 1 );
|
||||
|
||||
gateway.getPageSummary( 'Missing Page' ).fail( function () {
|
||||
assert.ok( true );
|
||||
|
||||
done();
|
||||
} );
|
||||
} );
|
|
@ -1,164 +0,0 @@
|
|||
( function ( mw, $ ) {
|
||||
|
||||
var createModel = mw.popups.preview.createModel,
|
||||
createRESTBaseGateway = mw.popups.gateway.createRESTBaseGateway,
|
||||
DEFAULT_CONSTANTS = {
|
||||
THUMBNAIL_SIZE: 512
|
||||
},
|
||||
SVG_ORIGINAL_IMAGE = {
|
||||
source: "https://upload.wikimedia.org/wikipedia/commons/8/8d/Ceiling_cat.svg",
|
||||
width: 800,
|
||||
height: 1000
|
||||
},
|
||||
RESTBASE_RESPONSE = {
|
||||
title: 'Barack Obama',
|
||||
extract: 'Barack Hussein Obama II born August 4, 1961) ...',
|
||||
thumbnail: {
|
||||
source: 'https://upload.wikimedia.org/someImage.jpg',
|
||||
width: 200,
|
||||
height: 250
|
||||
},
|
||||
originalimage: {
|
||||
source: "https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg",
|
||||
width: 800,
|
||||
height: 1000
|
||||
},
|
||||
lang: 'en',
|
||||
dir: 'ltr',
|
||||
timestamp: '2017-01-30T10:17:41Z',
|
||||
description: '44th President of the United States of America'
|
||||
},
|
||||
RESTBASE_RESPONSE_WITHOUT_IMAGE = {
|
||||
title: 'Barack Obama',
|
||||
extract: 'Barack Hussein Obama II born August 4, 1961) ...',
|
||||
lang: 'en',
|
||||
dir: 'ltr',
|
||||
timestamp: '2017-01-30T10:17:41Z',
|
||||
description: '44th President of the United States of America'
|
||||
},
|
||||
RESTBASE_RESPONSE_PREVIEW_MODEL = createModel(
|
||||
'Barack Obama',
|
||||
new mw.Title( 'Barack Obama' ).getUrl(),
|
||||
'en',
|
||||
'ltr',
|
||||
'Barack Hussein Obama II born August 4, 1961) ...',
|
||||
{
|
||||
source: 'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg/512px-President_Barack_Obama.jpg',
|
||||
width: 800,
|
||||
height: 1000
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.test( 'RESTBase gateway is called with correct arguments', function ( assert ) {
|
||||
var getSpy = this.sandbox.spy(),
|
||||
gateway = createRESTBaseGateway( getSpy ),
|
||||
expectedOptions = {
|
||||
url: '/api/rest_v1/page/summary/' + encodeURIComponent( 'Test Title' ),
|
||||
headers: {
|
||||
Accept: 'application/json; charset=utf-8' +
|
||||
'profile="https://www.mediawiki.org/wiki/Specs/Summary/1.0.0"'
|
||||
}
|
||||
};
|
||||
|
||||
gateway.fetch( 'Test Title' );
|
||||
|
||||
assert.deepEqual( getSpy.getCall( 0 ).args[ 0 ], expectedOptions, 'options' );
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway is correctly converting the page data to a model ', function ( assert ) {
|
||||
var gateway = createRESTBaseGateway();
|
||||
|
||||
assert.deepEqual(
|
||||
gateway.convertPageToModel( RESTBASE_RESPONSE, 512 ),
|
||||
RESTBASE_RESPONSE_PREVIEW_MODEL
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway doesn\'t stretch thumbnails', function ( assert ) {
|
||||
var model,
|
||||
gateway = createRESTBaseGateway();
|
||||
|
||||
model = gateway.convertPageToModel( RESTBASE_RESPONSE, 2000);
|
||||
|
||||
assert.equal(
|
||||
model.thumbnail.source,
|
||||
'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg/800px-President_Barack_Obama.jpg',
|
||||
'If the requested thumbnail size is bigger than the originalimage width the originalimage width is used'
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway stretches SVGs', function ( assert ) {
|
||||
var model,
|
||||
gateway = createRESTBaseGateway();
|
||||
|
||||
model = gateway.convertPageToModel( $.extend( {}, RESTBASE_RESPONSE, { originalimage: SVG_ORIGINAL_IMAGE } ),
|
||||
2000 );
|
||||
|
||||
assert.equal(
|
||||
model.thumbnail.source,
|
||||
'https://upload.wikimedia.org/wikipedia/commons/8/8d/Ceiling_cat.svg/2000px-Ceiling_cat.svg',
|
||||
'If the requested thumbnail size is bigger than the originalimage and its an SVG all is good'
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway handles the API failure', function ( assert ) {
|
||||
var deferred = $.Deferred(),
|
||||
api = this.sandbox.stub().returns( deferred.promise() ),
|
||||
gateway = createRESTBaseGateway( api ),
|
||||
done = assert.async( 1 );
|
||||
|
||||
gateway.getPageSummary( 'Test Title' ).fail( function () {
|
||||
assert.ok( true );
|
||||
done();
|
||||
} );
|
||||
|
||||
deferred.reject();
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway returns the correct data ', function ( assert ) {
|
||||
var api = this.sandbox.stub().returns(
|
||||
$.Deferred().resolve( RESTBASE_RESPONSE ).promise()
|
||||
),
|
||||
gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS),
|
||||
done = assert.async( 1 );
|
||||
|
||||
gateway.getPageSummary( 'Test Title' ).done( function ( result ) {
|
||||
assert.deepEqual( result, RESTBASE_RESPONSE_PREVIEW_MODEL );
|
||||
done();
|
||||
} );
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway handles missing images ', function ( assert ) {
|
||||
var model,
|
||||
gateway = createRESTBaseGateway();
|
||||
model = gateway.convertPageToModel( RESTBASE_RESPONSE_WITHOUT_IMAGE, 300 );
|
||||
|
||||
assert.equal(
|
||||
model.originalimage,
|
||||
undefined,
|
||||
'If restbase handles missing image information'
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'RESTBase gateway handles missing pages ', function ( assert ) {
|
||||
var response = {
|
||||
type: 'https://mediawiki.org/wiki/HyperSwitch/errors/not_found',
|
||||
title: 'Not found.',
|
||||
method: 'get',
|
||||
detail: 'Page or revision not found.',
|
||||
uri: '/en.wikipedia.org/v1/page/summary/Missing_page'
|
||||
},
|
||||
api = this.sandbox.stub().returns(
|
||||
$.Deferred().rejectWith( response ).promise()
|
||||
),
|
||||
gateway = createRESTBaseGateway( api, DEFAULT_CONSTANTS ),
|
||||
done = assert.async( 1 );
|
||||
|
||||
gateway.getPageSummary( 'Missing Page' ).fail( function () {
|
||||
assert.ok( true );
|
||||
|
||||
done();
|
||||
} );
|
||||
} );
|
||||
|
||||
}( mediaWiki, jQuery ) );
|
Loading…
Reference in a new issue