Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/tests/ui/widgets/ve.ui.MWTransclusionOutlineTemplateWidget.test.js

raw symbols SCIP
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 // Representation of a parameter placeholder in the model, should be skipped
13 '': {}
14 }
15 } );
16 template.getSpec().setTemplateData( { params: {
17 // A deprecated parameter must be shown as long as it's used
18 4: { deprecated: true },
19 // An unused deprecated parameter should never be shown
20 5: { deprecated: true },
21 6: {}
22 } } );
23 const widget = new ve.ui.MWTransclusionOutlineTemplateWidget( template ),
24 // We need to skip .getItems() for this test because it makes a copy
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 // Note that documented parameters go first
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 // Skip .addPart(), the TemplateData API, specCache and everything related
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 // Expected order on construction time is:
72 // - Documented params in paramOrder (g, h, e), excluding unused deprected params (- h)
73 // - Undocumented params currently used in the template (+ b)
74 assertOrder( widget, [ 'g', 'e', 'b' ] );
75
76 let insertAt = widget.findCanonicalPosition( 'h' );
77 // Most minimal mock instead of an actual ve.ui.MWTransclusionOutlineParameterWidget
78 widget.parameterList.addItems( [ new OO.ui.Widget( { data: 'h' } ) ], insertAt );
79 // Deprecated param appears at its canonical position via paramOrder
80 assert.strictEqual( insertAt, 1 );
81 assertOrder( widget, [ 'g', 'h', 'e', 'b' ] );
82
83 const newParam = new ve.dm.MWParameterModel( template, 'a1' );
84 // This fires an "add" event the widget listens to, i.e. this covers onAddParameter() as well
85 template.addParameter( newParam );
86 assertOrder( widget, [ 'g', 'h', 'e', 'a1', 'b' ] );
87 // Removing the param doesn't remove it's checkbox item from the widget
88 template.removeParameter( newParam );
89 assertOrder( widget, [ 'g', 'h', 'e', 'a1', 'b' ] );
90
91 // This is effectively the same as above: teach the spec a new param without adding it to the
92 // template. This doesn't fire events, which allows us to test the private method in isolation.
93 template.getSpec().seenParameterNames.a2 = true;
94 insertAt = widget.findCanonicalPosition( 'a2' );
95 // Most minimal mock instead of an actual ve.ui.MWTransclusionOutlineParameterWidget
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 } );