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() {
|
2023-05-12 21:12:16 +00:00
|
|
|
global.CSS = {
|
|
|
|
escape: ( str ) => $.escapeSelector( str )
|
|
|
|
};
|
2019-10-17 08:51:49 +00:00
|
|
|
mw.msg = ( key ) => `<${key}>`;
|
2021-02-18 17:45:37 +00:00
|
|
|
mw.message = ( key ) => {
|
|
|
|
return { exists: () => !key.endsWith( 'generic' ), text: () => `<${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(
|
2020-01-23 17:41:50 +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' )
|
|
|
|
),
|
2020-01-23 17:41:50 +00:00
|
|
|
$( '<li>' ).attr( 'id', 'cite_note-2' ).append(
|
2019-02-19 16:01:16 +00:00
|
|
|
$( '<span>' ).addClass( 'reference-text' ).append(
|
2021-02-18 17:45:37 +00:00
|
|
|
$( '<cite>' ).addClass( 'journal web unknown' ).text( 'Footnote 2' )
|
2019-02-19 16:01:16 +00:00
|
|
|
)
|
2019-02-20 16:15:34 +00:00
|
|
|
),
|
2020-01-23 17:41:50 +00:00
|
|
|
$( '<li>' ).attr( 'id', 'cite_note-3' ).append(
|
2019-02-20 16:15:34 +00:00
|
|
|
$( '<span>' ).addClass( 'reference-text' ).append(
|
|
|
|
$( '<cite>' ).addClass( 'news' ).text( 'Footnote 3' ),
|
|
|
|
$( '<cite>' ).addClass( 'news citation' ),
|
|
|
|
$( '<cite>' ).addClass( 'citation' )
|
|
|
|
)
|
|
|
|
),
|
2020-01-23 17:41:50 +00:00
|
|
|
$( '<li>' ).attr( 'id', 'cite_note-4' ).append(
|
2019-02-20 16:15:34 +00:00
|
|
|
$( '<span>' ).addClass( 'reference-text' ).append(
|
|
|
|
$( '<cite>' ).addClass( 'news' ).text( 'Footnote 4' ),
|
|
|
|
$( '<cite>' ).addClass( 'web' )
|
|
|
|
)
|
2019-12-12 11:16:43 +00:00
|
|
|
),
|
|
|
|
$( '<li>' ).attr( 'id', 'cite_note-5' ).append(
|
|
|
|
$( '<span>' ).addClass( 'mw-reference-text' ).html( ' ' )
|
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;
|
2021-05-07 10:09:32 +00:00
|
|
|
mw.message = null;
|
|
|
|
this.$sourceElement.parent().remove();
|
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(),
|
2020-01-23 17:41:50 +00:00
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note-1' );
|
2019-01-23 19:26:48 +00:00
|
|
|
|
2021-05-07 10:09:32 +00:00
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => {
|
2019-01-23 19:26:48 +00:00
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
2020-01-23 17:41:50 +00:00
|
|
|
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,
|
2021-05-07 10:09:32 +00:00
|
|
|
sourceElementId: 'cite_ref-1'
|
2019-02-20 10:35:34 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Reference preview gateway accepts alternative text node class name', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
2020-01-23 17:41:50 +00:00
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note-2' );
|
2019-02-20 10:35:34 +00:00
|
|
|
|
2021-05-07 10:09:32 +00:00
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => {
|
2019-02-20 10:35:34 +00:00
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
2020-01-23 17:41:50 +00:00
|
|
|
url: '#cite_note-2',
|
2021-02-18 17:45:37 +00:00
|
|
|
extract: '<cite class="journal web unknown">Footnote 2</cite>',
|
2019-02-04 12:08:36 +00:00
|
|
|
type: 'reference',
|
2021-02-18 17:45:37 +00:00
|
|
|
referenceType: 'web',
|
2021-05-07 10:09:32 +00:00
|
|
|
sourceElementId: 'cite_ref-1'
|
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(),
|
2020-01-23 17:41:50 +00:00
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note-3' );
|
2019-02-20 16:15:34 +00:00
|
|
|
|
2021-05-07 10:09:32 +00:00
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => {
|
2019-02-20 16:15:34 +00:00
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
2020-01-23 17:41:50 +00:00
|
|
|
url: '#cite_note-3',
|
2019-02-20 16:15:34 +00:00
|
|
|
extract: '<cite class="news">Footnote 3</cite><cite class="news citation"></cite><cite class="citation"></cite>',
|
|
|
|
type: 'reference',
|
|
|
|
referenceType: 'news',
|
2021-05-07 10:09:32 +00:00
|
|
|
sourceElementId: 'cite_ref-1'
|
2019-02-20 16:15:34 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2021-02-18 17:45:37 +00:00
|
|
|
QUnit.test( 'Reference preview gateway ignores conflicting types', function ( assert ) {
|
2019-02-20 16:15:34 +00:00
|
|
|
const gateway = createReferenceGateway(),
|
2020-01-23 17:41:50 +00:00
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note-4' );
|
2019-02-20 16:15:34 +00:00
|
|
|
|
2021-05-07 10:09:32 +00:00
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => {
|
2019-02-20 16:15:34 +00:00
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
2020-01-23 17:41:50 +00:00
|
|
|
url: '#cite_note-4',
|
2019-02-20 16:15:34 +00:00
|
|
|
extract: '<cite class="news">Footnote 4</cite><cite class="web"></cite>',
|
|
|
|
type: 'reference',
|
2021-02-18 17:45:37 +00:00
|
|
|
referenceType: 'news',
|
2021-05-07 10:09:32 +00:00
|
|
|
sourceElementId: 'cite_ref-1'
|
2019-02-20 16:15:34 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-02-04 17:52:43 +00:00
|
|
|
QUnit.test( 'Reference preview gateway returns source element id', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
2020-01-23 17:41:50 +00:00
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note-1' );
|
2019-02-04 17:52:43 +00:00
|
|
|
|
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => {
|
|
|
|
assert.propEqual(
|
|
|
|
result,
|
|
|
|
{
|
2020-01-23 17:41:50 +00:00
|
|
|
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' );
|
|
|
|
|
2021-05-07 10:09:32 +00:00
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => {
|
2022-02-09 15:09:19 +00:00
|
|
|
assert.true( false, 'It should not resolve' );
|
2023-04-14 17:54:55 +00:00
|
|
|
} ).catch( ( result ) => {
|
|
|
|
assert.propEqual( result, { textStatus: 'abort', textContext: 'Footnote not found or empty', xhr: { readyState: 0 } } );
|
2019-01-25 19:44:48 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-12-12 11:16:43 +00:00
|
|
|
QUnit.test( 'Reference preview gateway rejects all-whitespace references', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note-5' );
|
|
|
|
|
2021-05-07 10:09:32 +00:00
|
|
|
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => {
|
2022-02-09 15:09:19 +00:00
|
|
|
assert.true( false, 'It should not resolve' );
|
2023-04-14 17:54:55 +00:00
|
|
|
} ).catch( ( result ) => {
|
|
|
|
assert.propEqual( result, { textStatus: 'abort', textContext: 'Footnote not found or empty', xhr: { readyState: 0 } } );
|
2019-12-12 11:16:43 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2019-01-23 19:26:48 +00:00
|
|
|
QUnit.test( 'Reference preview gateway is abortable', function ( assert ) {
|
|
|
|
const gateway = createReferenceGateway(),
|
2020-01-23 17:41:50 +00:00
|
|
|
title = createStubTitle( 1, 'Foo', 'cite note-1' ),
|
2021-05-07 10:09:32 +00:00
|
|
|
promise = gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] );
|
2019-01-23 19:26:48 +00:00
|
|
|
|
|
|
|
assert.strictEqual( typeof promise.abort, 'function' );
|
|
|
|
} );
|