2019-01-23 19:26:48 +00:00
|
|
|
import { createStubTitle } from '../stubs';
|
|
|
|
import createReferenceGateway from '../../../src/gateway/reference';
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/gateway/reference', {
|
|
|
|
beforeEach() {
|
2019-10-17 08:51:49 +00:00
|
|
|
mw.msg = ( key ) => `<${key}>`;
|
2019-01-25 19:44:48 +00:00
|
|
|
|
2019-02-04 17:52:43 +00:00
|
|
|
this.$sourceElement = $( '<a>' ).appendTo(
|
|
|
|
$( '<sup>' ).attr( 'id', 'cite_ref-1' ).appendTo( document.body )
|
|
|
|
);
|
2019-02-20 10:35:34 +00:00
|
|
|
|
2019-01-25 19:44:48 +00:00
|
|
|
this.$references = $( '<ul>' ).append(
|
2019-01-17 22:17:27 +00:00
|
|
|
$( '<li>' ).attr( 'id', 'cite_note--1' ).append(
|
2019-02-20 10:35:34 +00:00
|
|
|
$( '<span>' ).addClass( 'mw-reference-text' ).text( 'Footnote 1' )
|
|
|
|
),
|
|
|
|
$( '<li>' ).attr( 'id', 'cite_note--2' ).append(
|
2019-02-19 16:01:16 +00:00
|
|
|
$( '<span>' ).addClass( 'reference-text' ).append(
|
|
|
|
$( '<cite>' ).addClass( 'citation web unknown' ).text( 'Footnote 2' )
|
|
|
|
)
|
2019-02-20 16:15:34 +00:00
|
|
|
),
|
|
|
|
$( '<li>' ).attr( 'id', 'cite_note--3' ).append(
|
|
|
|
$( '<span>' ).addClass( 'reference-text' ).append(
|
|
|
|
$( '<cite>' ).addClass( 'news' ).text( 'Footnote 3' ),
|
|
|
|
$( '<cite>' ).addClass( 'news citation' ),
|
|
|
|
$( '<cite>' ).addClass( 'citation' )
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$( '<li>' ).attr( 'id', 'cite_note--4' ).append(
|
|
|
|
$( '<span>' ).addClass( 'reference-text' ).append(
|
|
|
|
$( '<cite>' ).addClass( 'news' ).text( 'Footnote 4' ),
|
|
|
|
$( '<cite>' ).addClass( 'web' )
|
|
|
|
)
|
2019-01-25 19:44:48 +00:00
|
|
|
)
|
|
|
|
).appendTo( document.body );
|
2019-01-23 19:26:48 +00:00
|
|
|
},
|
|
|
|
afterEach() {
|
2019-10-17 08:51:49 +00:00
|
|
|
mw.msg = null;
|
2019-01-25 19:44:48 +00:00
|
|
|
|
|
|
|
this.$references.remove();
|
2019-01-23 19:26:48 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Reference preview gateway returns the correct data', function ( assert ) {
|
2019-01-24 10:37:30 +00:00
|
|
|
const gateway = createReferenceGateway(),
|
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note--1' );
|
2019-01-23 19:26:48 +00:00
|
|
|
|
2019-01-24 10:37:30 +00:00
|
|
|
return gateway.fetchPreviewForTitle( title ).then( ( result ) => {
|
2019-01-23 19:26:48 +00:00
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
|
|
|
url: '#cite_note--1',
|
2019-02-20 10:35:34 +00:00
|
|
|
extract: 'Footnote 1',
|
|
|
|
type: 'reference',
|
2019-02-19 16:01:16 +00:00
|
|
|
referenceType: null,
|
2019-02-20 10:35:34 +00:00
|
|
|
sourceElementId: undefined
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Reference preview gateway accepts alternative text node class name', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note--2' );
|
|
|
|
|
|
|
|
return gateway.fetchPreviewForTitle( title ).then( ( result ) => {
|
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
|
|
|
url: '#cite_note--2',
|
2019-02-19 16:01:16 +00:00
|
|
|
extract: '<cite class="citation web unknown">Footnote 2</cite>',
|
2019-02-04 12:08:36 +00:00
|
|
|
type: 'reference',
|
2019-02-19 16:01:16 +00:00
|
|
|
referenceType: 'web unknown',
|
2019-02-04 12:08:36 +00:00
|
|
|
sourceElementId: undefined
|
2019-01-23 19:26:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-02-20 16:15:34 +00:00
|
|
|
QUnit.test( 'Reference preview gateway accepts duplicated types', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note--3' );
|
|
|
|
|
|
|
|
return gateway.fetchPreviewForTitle( title ).then( ( result ) => {
|
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
|
|
|
url: '#cite_note--3',
|
|
|
|
extract: '<cite class="news">Footnote 3</cite><cite class="news citation"></cite><cite class="citation"></cite>',
|
|
|
|
type: 'reference',
|
|
|
|
referenceType: 'news',
|
|
|
|
sourceElementId: undefined
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Reference preview gateway rejects conflicting types', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note--4' );
|
|
|
|
|
|
|
|
return gateway.fetchPreviewForTitle( title ).then( ( result ) => {
|
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
|
|
|
url: '#cite_note--4',
|
|
|
|
extract: '<cite class="news">Footnote 4</cite><cite class="web"></cite>',
|
|
|
|
type: 'reference',
|
|
|
|
referenceType: null,
|
|
|
|
sourceElementId: undefined
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-02-04 17:52:43 +00:00
|
|
|
QUnit.test( 'Reference preview gateway returns source element id', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note--1' );
|
|
|
|
|
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => {
|
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
|
|
|
url: '#cite_note--1',
|
2019-02-20 10:35:34 +00:00
|
|
|
extract: 'Footnote 1',
|
2019-02-04 17:52:43 +00:00
|
|
|
type: 'reference',
|
2019-02-19 16:01:16 +00:00
|
|
|
referenceType: null,
|
2019-02-04 17:52:43 +00:00
|
|
|
sourceElementId: 'cite_ref-1'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-01-25 19:44:48 +00:00
|
|
|
QUnit.test( 'Reference preview gateway rejects non-existing references', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
|
|
|
title = createStubTitle( 1, 'Foo', 'undefined' );
|
|
|
|
|
|
|
|
return gateway.fetchPreviewForTitle( title ).then( () => {
|
|
|
|
assert.ok( false, 'It should not resolve' );
|
|
|
|
} ).catch( ( reason, result ) => {
|
|
|
|
assert.propEqual( result, { textStatus: 'abort', xhr: { readyState: 0 } } );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-01-23 19:26:48 +00:00
|
|
|
QUnit.test( 'Reference preview gateway is abortable', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
2019-01-24 10:37:30 +00:00
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note--1' ),
|
|
|
|
promise = gateway.fetchPreviewForTitle( title );
|
2019-01-23 19:26:48 +00:00
|
|
|
|
|
|
|
assert.strictEqual( typeof promise.abort, 'function' );
|
|
|
|
} );
|