| 1 |
function createStubTitle( fragment = null ) { |
| 2 |
return { |
| 3 |
getFragment() { |
| 4 |
return fragment; |
| 5 |
} |
| 6 |
}; |
| 7 |
} |
| 8 |
|
| 9 |
( mw.loader.getModuleNames().indexOf( 'ext.popups.main' ) !== -1 ? |
| 10 |
QUnit.module : |
| 11 |
QUnit.module.skip )( 'ext.cite.referencePreviews#createReferenceGateway', { |
| 12 |
beforeEach() { |
| 13 |
this.$sourceElement = $( '<a>' ).appendTo( |
| 14 |
$( '<sup>' ).attr( 'id', 'cite_ref-1' ).appendTo( document.body ) |
| 15 |
); |
| 16 |
|
| 17 |
this.$references = $( '<ul>' ).append( |
| 18 |
$( '<li>' ).attr( 'id', 'cite_note-1' ).append( |
| 19 |
$( '<span>' ).addClass( 'mw-reference-text' ).text( 'Footnote 1' ) |
| 20 |
), |
| 21 |
$( '<li>' ).attr( 'id', 'cite_note-2' ).append( |
| 22 |
$( '<span>' ).addClass( 'reference-text' ).append( |
| 23 |
$( '<cite>' ).addClass( 'journal web unknown' ).text( 'Footnote 2' ) |
| 24 |
) |
| 25 |
), |
| 26 |
$( '<li>' ).attr( 'id', 'cite_note-3' ).append( |
| 27 |
$( '<span>' ).addClass( 'reference-text' ).append( |
| 28 |
$( '<cite>' ).addClass( 'news' ).text( 'Footnote 3' ), |
| 29 |
$( '<cite>' ).addClass( 'news citation' ), |
| 30 |
$( '<cite>' ).addClass( 'citation' ) |
| 31 |
) |
| 32 |
), |
| 33 |
$( '<li>' ).attr( 'id', 'cite_note-4' ).append( |
| 34 |
$( '<span>' ).addClass( 'reference-text' ).append( |
| 35 |
$( '<cite>' ).addClass( 'news' ).text( 'Footnote 4' ), |
| 36 |
$( '<cite>' ).addClass( 'web' ) |
| 37 |
) |
| 38 |
), |
| 39 |
$( '<li>' ).attr( 'id', 'cite_note-5' ).append( |
| 40 |
$( '<span>' ).addClass( 'mw-reference-text' ).html( ' ' ) |
| 41 |
) |
| 42 |
).appendTo( document.body ); |
| 43 |
}, |
| 44 |
afterEach() { |
| 45 |
this.$sourceElement.parent().remove(); |
| 46 |
this.$references.remove(); |
| 47 |
} |
| 48 |
} ); |
| 49 |
|
| 50 |
QUnit.test( 'Reference preview gateway returns the correct data', function ( assert ) { |
| 51 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 52 |
title = createStubTitle( 'cite note-1' ); |
| 53 |
|
| 54 |
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => { |
| 55 |
assert.propEqual( |
| 56 |
result, |
| 57 |
{ |
| 58 |
url: '#cite_note-1', |
| 59 |
extract: 'Footnote 1', |
| 60 |
type: 'reference', |
| 61 |
referenceType: null, |
| 62 |
sourceElementId: 'cite_ref-1' |
| 63 |
} |
| 64 |
); |
| 65 |
} ); |
| 66 |
} ); |
| 67 |
|
| 68 |
QUnit.test( 'Reference preview gateway accepts alternative text node class name', function ( assert ) { |
| 69 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 70 |
title = createStubTitle( 'cite note-2' ); |
| 71 |
|
| 72 |
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => { |
| 73 |
assert.propEqual( |
| 74 |
result, |
| 75 |
{ |
| 76 |
url: '#cite_note-2', |
| 77 |
extract: '<cite class="journal web unknown">Footnote 2</cite>', |
| 78 |
type: 'reference', |
| 79 |
referenceType: 'web', |
| 80 |
sourceElementId: 'cite_ref-1' |
| 81 |
} |
| 82 |
); |
| 83 |
} ); |
| 84 |
} ); |
| 85 |
|
| 86 |
QUnit.test( 'Reference preview gateway accepts duplicated types', function ( assert ) { |
| 87 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 88 |
title = createStubTitle( 'cite note-3' ); |
| 89 |
|
| 90 |
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => { |
| 91 |
assert.propEqual( |
| 92 |
result, |
| 93 |
{ |
| 94 |
url: '#cite_note-3', |
| 95 |
extract: '<cite class="news">Footnote 3</cite><cite class="news citation"></cite><cite class="citation"></cite>', |
| 96 |
type: 'reference', |
| 97 |
referenceType: 'news', |
| 98 |
sourceElementId: 'cite_ref-1' |
| 99 |
} |
| 100 |
); |
| 101 |
} ); |
| 102 |
} ); |
| 103 |
|
| 104 |
QUnit.test( 'Reference preview gateway ignores conflicting types', function ( assert ) { |
| 105 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 106 |
title = createStubTitle( 'cite note-4' ); |
| 107 |
|
| 108 |
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => { |
| 109 |
assert.propEqual( |
| 110 |
result, |
| 111 |
{ |
| 112 |
url: '#cite_note-4', |
| 113 |
extract: '<cite class="news">Footnote 4</cite><cite class="web"></cite>', |
| 114 |
type: 'reference', |
| 115 |
referenceType: 'news', |
| 116 |
sourceElementId: 'cite_ref-1' |
| 117 |
} |
| 118 |
); |
| 119 |
} ); |
| 120 |
} ); |
| 121 |
|
| 122 |
QUnit.test( 'Reference preview gateway returns source element id', function ( assert ) { |
| 123 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 124 |
title = createStubTitle( 'cite note-1' ); |
| 125 |
|
| 126 |
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( ( result ) => { |
| 127 |
assert.propEqual( |
| 128 |
result, |
| 129 |
{ |
| 130 |
url: '#cite_note-1', |
| 131 |
extract: 'Footnote 1', |
| 132 |
type: 'reference', |
| 133 |
referenceType: null, |
| 134 |
sourceElementId: 'cite_ref-1' |
| 135 |
} |
| 136 |
); |
| 137 |
} ); |
| 138 |
} ); |
| 139 |
|
| 140 |
QUnit.test( 'Reference preview gateway rejects non-existing references', function ( assert ) { |
| 141 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 142 |
title = createStubTitle( 'undefined' ); |
| 143 |
|
| 144 |
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => { |
| 145 |
assert.true( false, 'It should not resolve' ); |
| 146 |
} ).catch( ( result ) => { |
| 147 |
assert.propEqual( result, { textStatus: 'abort', textContext: 'Footnote not found or empty', xhr: { readyState: 0 } } ); |
| 148 |
} ); |
| 149 |
} ); |
| 150 |
|
| 151 |
QUnit.test( 'Reference preview gateway rejects all-whitespace references', function ( assert ) { |
| 152 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 153 |
title = createStubTitle( 'cite note-5' ); |
| 154 |
|
| 155 |
return gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ).then( () => { |
| 156 |
assert.true( false, 'It should not resolve' ); |
| 157 |
} ).catch( ( result ) => { |
| 158 |
assert.propEqual( result, { textStatus: 'abort', textContext: 'Footnote not found or empty', xhr: { readyState: 0 } } ); |
| 159 |
} ); |
| 160 |
} ); |
| 161 |
|
| 162 |
QUnit.test( 'Reference preview gateway is abortable', function ( assert ) { |
| 163 |
const gateway = require( 'ext.cite.referencePreviews' ).private.createReferenceGateway(), |
| 164 |
title = createStubTitle( 'cite note-1' ), |
| 165 |
promise = gateway.fetchPreviewForTitle( title, this.$sourceElement[ 0 ] ); |
| 166 |
|
| 167 |
assert.strictEqual( typeof promise.abort, 'function' ); |
| 168 |
} ); |