| 1 |
QUnit.module( 've.ui.MWTransclusionOutlineTemplateWidget' ); |
| 2 |
|
| 3 |
QUnit.test( 'Constructor', ( assert ) => { |
| 4 |
const transclusion = new ve.dm.MWTransclusionModel(), |
| 5 |
template = ve.dm.MWTemplateModel.newFromData( transclusion, { |
| 6 |
target: { wt: 'Example' }, |
| 7 |
params: { |
| 8 |
1: {}, |
| 9 |
2: {}, |
| 10 |
3: {}, |
| 11 |
4: {}, |
| 12 |
|
| 13 |
'': {} |
| 14 |
} |
| 15 |
} ); |
| 16 |
template.getSpec().setTemplateData( { params: { |
| 17 |
|
| 18 |
4: { deprecated: true }, |
| 19 |
|
| 20 |
5: { deprecated: true }, |
| 21 |
6: {} |
| 22 |
} } ); |
| 23 |
const widget = new ve.ui.MWTransclusionOutlineTemplateWidget( template ), |
| 24 |
|
| 25 |
parameters = widget.parameterList.items; |
| 26 |
|
| 27 |
assert.strictEqual( widget.getData(), 'part_0' ); |
| 28 |
assert.strictEqual( |
| 29 |
widget.$element.find( '.ve-ui-mwTransclusionOutlineButtonWidget .oo-ui-buttonElement-button' ).text(), |
| 30 |
'Example' |
| 31 |
); |
| 32 |
assert.true( widget.searchWidget.isVisible() ); |
| 33 |
assert.true( widget.toggleUnusedWidget.isVisible() ); |
| 34 |
assert.false( widget.infoWidget.isVisible() ); |
| 35 |
|
| 36 |
assert.deepEqual( parameters.map( ( item ) => item.data ), [ '4', '1', '2', '3' ] ); |
| 37 |
|
| 38 |
widget.toggleUnusedWidget.emit( 'click' ); |
| 39 |
|
| 40 |
assert.deepEqual( parameters.map( ( item ) => item.data ), [ '4', '6', '1', '2', '3' ] ); |
| 41 |
|
| 42 |
widget.searchWidget.setValue( 'can not find anything' ); |
| 43 |
|
| 44 |
assert.false( widget.toggleUnusedWidget.isVisible() ); |
| 45 |
assert.true( widget.infoWidget.isVisible() ); |
| 46 |
assert.strictEqual( parameters.filter( ( p ) => p.isVisible() ).length, 0 ); |
| 47 |
} ); |
| 48 |
|
| 49 |
QUnit.test( 'findCanonicalPosition()', ( assert ) => { |
| 50 |
function assertOrder( w, expected ) { |
| 51 |
assert.deepEqual( w.parameterList.items.map( ( item ) => item.data ), expected ); |
| 52 |
} |
| 53 |
|
| 54 |
const transclusion = new ve.dm.MWTransclusionModel(), |
| 55 |
template = ve.dm.MWTemplateModel.newFromData( transclusion, { |
| 56 |
target: { wt: '' }, |
| 57 |
params: { b: {}, e: {} } |
| 58 |
} ); |
| 59 |
|
| 60 |
transclusion.parts.push( template ); |
| 61 |
template.getSpec().setTemplateData( { |
| 62 |
params: { |
| 63 |
e: { deprecated: true }, |
| 64 |
h: { deprecated: true }, |
| 65 |
g: {} |
| 66 |
}, |
| 67 |
paramOrder: [ 'g', 'h', 'e' ] |
| 68 |
} ); |
| 69 |
const widget = new ve.ui.MWTransclusionOutlineTemplateWidget( template ); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
assertOrder( widget, [ 'g', 'e', 'b' ] ); |
| 75 |
|
| 76 |
let insertAt = widget.findCanonicalPosition( 'h' ); |
| 77 |
|
| 78 |
widget.parameterList.addItems( [ new OO.ui.Widget( { data: 'h' } ) ], insertAt ); |
| 79 |
|
| 80 |
assert.strictEqual( insertAt, 1 ); |
| 81 |
assertOrder( widget, [ 'g', 'h', 'e', 'b' ] ); |
| 82 |
|
| 83 |
const newParam = new ve.dm.MWParameterModel( template, 'a1' ); |
| 84 |
|
| 85 |
template.addParameter( newParam ); |
| 86 |
assertOrder( widget, [ 'g', 'h', 'e', 'a1', 'b' ] ); |
| 87 |
|
| 88 |
template.removeParameter( newParam ); |
| 89 |
assertOrder( widget, [ 'g', 'h', 'e', 'a1', 'b' ] ); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
template.getSpec().seenParameterNames.a2 = true; |
| 94 |
insertAt = widget.findCanonicalPosition( 'a2' ); |
| 95 |
|
| 96 |
widget.parameterList.addItems( [ new OO.ui.Widget( { data: 'a2' } ) ], insertAt ); |
| 97 |
assert.strictEqual( insertAt, 4 ); |
| 98 |
assertOrder( widget, [ 'g', 'h', 'e', 'a1', 'a2', 'b' ] ); |
| 99 |
} ); |
| 100 |
|
| 101 |
QUnit.test( 'filterParameters() when it cannot find anything', ( assert ) => { |
| 102 |
const transclusion = new ve.dm.MWTransclusionModel(), |
| 103 |
template = ve.dm.MWTemplateModel.newFromData( transclusion, { |
| 104 |
target: { wt: '' }, |
| 105 |
params: { 1: {}, 2: {}, 3: {}, 4: {} } |
| 106 |
} ), |
| 107 |
widget = new ve.ui.MWTransclusionOutlineTemplateWidget( template ); |
| 108 |
let eventsFired = 0; |
| 109 |
widget.connect( this, { |
| 110 |
filterParametersById: ( visibility ) => { |
| 111 |
assert.deepEqual( visibility, { |
| 112 |
'part_0/1': false, |
| 113 |
'part_0/2': false, |
| 114 |
'part_0/3': false, |
| 115 |
'part_0/4': false |
| 116 |
} ); |
| 117 |
eventsFired++; |
| 118 |
} |
| 119 |
} ); |
| 120 |
widget.filterParameters( 'b' ); |
| 121 |
assert.true( widget.infoWidget.isVisible() ); |
| 122 |
assert.strictEqual( eventsFired, 1 ); |
| 123 |
} ); |
| 124 |
|
| 125 |
QUnit.test( 'filterParameters() considers everything from the spec', ( assert ) => { |
| 126 |
const transclusion = new ve.dm.MWTransclusionModel(), |
| 127 |
template = ve.dm.MWTemplateModel.newFromData( transclusion, { |
| 128 |
target: { wt: '' }, |
| 129 |
params: { a: {}, b: {}, c: {}, d: {}, e: {} } |
| 130 |
} ), |
| 131 |
widget = new ve.ui.MWTransclusionOutlineTemplateWidget( template ); |
| 132 |
|
| 133 |
template.getSpec().setTemplateData( { |
| 134 |
params: { |
| 135 |
c: { label: 'Contains a' }, |
| 136 |
d: { description: 'Contains a' }, |
| 137 |
e: { aliases: [ 'Contains a' ] }, |
| 138 |
f: { label: 'Also contains a, but is not used in the template' } |
| 139 |
} |
| 140 |
} ); |
| 141 |
|
| 142 |
let eventsFired = 0; |
| 143 |
widget.connect( this, { |
| 144 |
filterParametersById: ( visibility ) => { |
| 145 |
assert.deepEqual( visibility, { |
| 146 |
'part_0/a': true, |
| 147 |
'part_0/b': false, |
| 148 |
'part_0/c': true, |
| 149 |
'part_0/d': true, |
| 150 |
'part_0/e': true |
| 151 |
} ); |
| 152 |
eventsFired++; |
| 153 |
} |
| 154 |
} ); |
| 155 |
|
| 156 |
assert.true( widget.searchWidget.isVisible() ); |
| 157 |
widget.filterParameters( ' A ' ); |
| 158 |
assert.false( widget.infoWidget.isVisible() ); |
| 159 |
assert.strictEqual( eventsFired, 1 ); |
| 160 |
} ); |