| 1 |
{ |
| 2 |
const toggleCirrusSearchLookup = ( enabled ) => mw.config.set( 'wgVisualEditorConfig', ve.extendObject( {}, mw.config.get( 'wgVisualEditorConfig' ), { |
| 3 |
cirrusSearchLookup: enabled !== false |
| 4 |
} ) ); |
| 5 |
|
| 6 |
QUnit.module( 've.ui.MWTemplateTitleInputWidget', ve.test.utils.newMwEnvironment( { |
| 7 |
messages: { |
| 8 |
|
| 9 |
'templatedata-doc-subpage': '(templatedata-doc-subpage)' |
| 10 |
}, |
| 11 |
|
| 12 |
beforeEach: function () { |
| 13 |
this.server = this.sandbox.useFakeServer(); |
| 14 |
toggleCirrusSearchLookup(); |
| 15 |
} |
| 16 |
} ) ); |
| 17 |
|
| 18 |
QUnit.test( 'default prefixsearch', ( assert ) => { |
| 19 |
toggleCirrusSearchLookup( false ); |
| 20 |
|
| 21 |
const widget = new ve.ui.MWTemplateTitleInputWidget(); |
| 22 |
const query = 'a'; |
| 23 |
const apiParams = widget.getApiParams( query ); |
| 24 |
|
| 25 |
assert.deepEqual( apiParams, { |
| 26 |
action: 'query', |
| 27 |
generator: 'prefixsearch', |
| 28 |
gpslimit: 10, |
| 29 |
gpsnamespace: 10, |
| 30 |
gpssearch: 'a', |
| 31 |
ppprop: 'disambiguation', |
| 32 |
prop: [ 'info', 'pageprops' ], |
| 33 |
redirects: true |
| 34 |
} ); |
| 35 |
} ); |
| 36 |
|
| 37 |
QUnit.test( 'CirrusSearch: all API parameters', ( assert ) => { |
| 38 |
const widget = new ve.ui.MWTemplateTitleInputWidget(); |
| 39 |
const query = 'a'; |
| 40 |
const apiParams = widget.getApiParams( query ); |
| 41 |
|
| 42 |
assert.deepEqual( apiParams, { |
| 43 |
action: 'query', |
| 44 |
generator: 'search', |
| 45 |
gsrlimit: 10, |
| 46 |
gsrnamespace: 10, |
| 47 |
gsrprop: 'redirecttitle', |
| 48 |
gsrsearch: 'a*', |
| 49 |
ppprop: 'disambiguation', |
| 50 |
prop: [ 'info', 'pageprops' ], |
| 51 |
redirects: true |
| 52 |
} ); |
| 53 |
} ); |
| 54 |
|
| 55 |
QUnit.test( 'CirrusSearch: showRedirectTargets disabled', ( assert ) => { |
| 56 |
const widget = new ve.ui.MWTemplateTitleInputWidget( { showRedirectTargets: false } ), |
| 57 |
apiParams = widget.getApiParams(); |
| 58 |
|
| 59 |
assert.false( 'gsrprop' in apiParams ); |
| 60 |
} ); |
| 61 |
|
| 62 |
QUnit.test.each( 'CirrusSearch: prefixsearch behavior', [ |
| 63 |
{ |
| 64 |
query: 'a', |
| 65 |
expected: 'a*' |
| 66 |
}, |
| 67 |
{ |
| 68 |
query: 'a ', |
| 69 |
expected: 'a ' |
| 70 |
}, |
| 71 |
{ |
| 72 |
query: 'ü', |
| 73 |
expected: 'ü*' |
| 74 |
}, |
| 75 |
{ |
| 76 |
query: '3', |
| 77 |
expected: '3*' |
| 78 |
}, |
| 79 |
{ |
| 80 |
query: '!!', |
| 81 |
expected: '!!' |
| 82 |
}, |
| 83 |
{ |
| 84 |
query: 'Foo:', |
| 85 |
expected: 'Foo:' |
| 86 |
}, |
| 87 |
{ |
| 88 |
query: 'Foo:Bar', |
| 89 |
expected: 'Foo:Bar*' |
| 90 |
}, |
| 91 |
{ |
| 92 |
query: 'foo_', |
| 93 |
expected: 'foo_' |
| 94 |
}, |
| 95 |
{ |
| 96 |
query: 'foo-', |
| 97 |
expected: 'foo-' |
| 98 |
}, |
| 99 |
{ |
| 100 |
query: 'foo+', |
| 101 |
expected: 'foo+' |
| 102 |
}, |
| 103 |
{ |
| 104 |
query: 'foo/', |
| 105 |
expected: 'foo/' |
| 106 |
}, |
| 107 |
{ |
| 108 |
query: 'foo~', |
| 109 |
expected: 'foo~' |
| 110 |
}, |
| 111 |
{ |
| 112 |
query: 'foo*', |
| 113 |
expected: 'foo*' |
| 114 |
}, |
| 115 |
{ |
| 116 |
query: '(foo)', |
| 117 |
expected: '(foo)' |
| 118 |
}, |
| 119 |
{ |
| 120 |
query: '[foo]', |
| 121 |
expected: '[foo]' |
| 122 |
}, |
| 123 |
{ |
| 124 |
query: '{foo}', |
| 125 |
expected: '{foo}' |
| 126 |
}, |
| 127 |
{ |
| 128 |
query: '"foo"', |
| 129 |
expected: '"foo"' |
| 130 |
}, |
| 131 |
{ |
| 132 |
query: 'foß', |
| 133 |
expected: 'foß*' |
| 134 |
}, |
| 135 |
{ |
| 136 |
query: '中文字', |
| 137 |
expected: '中文字*' |
| 138 |
}, |
| 139 |
{ |
| 140 |
query: 'zhōngwénzì', |
| 141 |
expected: 'zhōngwénzì*' |
| 142 |
} |
| 143 |
], ( assert, data ) => { |
| 144 |
const widget = new ve.ui.MWTemplateTitleInputWidget(); |
| 145 |
const apiParams = widget.getApiParams( data.query ); |
| 146 |
|
| 147 |
assert.strictEqual( |
| 148 |
apiParams.gsrsearch, |
| 149 |
data.expected, |
| 150 |
'Searching for ' + data.query |
| 151 |
); |
| 152 |
} ); |
| 153 |
|
| 154 |
QUnit.test( 'CirrusSearch with prefixsearch fallback', async function ( assert ) { |
| 155 |
const api = new mw.Api(); |
| 156 |
this.sandbox.stub( api, 'get' ) |
| 157 |
.onFirstCall().returns( ve.createDeferred() |
| 158 |
.resolve( { query: { |
| 159 |
pages: [ |
| 160 |
{ pageid: 101, title: 'B' }, |
| 161 |
{ pageid: 102, title: 'A' }, |
| 162 |
|
| 163 |
{ pageid: 103, title: 'A/(templatedata-doc-subpage)', index: 2 } |
| 164 |
], |
| 165 |
redirects: [ |
| 166 |
|
| 167 |
{ from: '', to: 'B', index: 1 }, |
| 168 |
{ from: '', to: 'A', index: 0 } |
| 169 |
] |
| 170 |
} } ) |
| 171 |
.promise( { abort: () => {} } ) |
| 172 |
) |
| 173 |
.onSecondCall().returns( ve.createDeferred() |
| 174 |
.resolve( { query: { pages: [ |
| 175 |
|
| 176 |
{ pageid: 102, title: 'A', index: 2 }, |
| 177 |
|
| 178 |
{ pageid: 201, title: 'D', index: 1 }, |
| 179 |
{ pageid: 202, title: 'C', index: 0 } |
| 180 |
] } } ) |
| 181 |
) |
| 182 |
.onThirdCall().returns( ve.createDeferred() |
| 183 |
.resolve( ) |
| 184 |
); |
| 185 |
|
| 186 |
const widget = new ve.ui.MWTemplateTitleInputWidget( { api, showDescriptions: true } ); |
| 187 |
widget.setValue( 'something' ); |
| 188 |
|
| 189 |
const response = await widget.getLookupRequest(); |
| 190 |
assert.deepEqual( response.query.pages, [ |
| 191 |
{ pageid: 202, title: 'C', index: -10 }, |
| 192 |
{ pageid: 201, title: 'D', index: -9 }, |
| 193 |
{ pageid: 102, title: 'A', index: 0 }, |
| 194 |
{ pageid: 101, title: 'B', index: 1 } |
| 195 |
] ); |
| 196 |
} ); |
| 197 |
|
| 198 |
QUnit.test( 'CirrusSearch: redirect is forwarded to the TitleOptionWidget', ( assert ) => { |
| 199 |
const widget = new ve.ui.MWTemplateTitleInputWidget(); |
| 200 |
const originalData = { redirecttitle: 'Template:From' }; |
| 201 |
const data = widget.getOptionWidgetData( 'Template:To', { originalData } ); |
| 202 |
|
| 203 |
assert.strictEqual( data.redirecttitle, 'Template:From' ); |
| 204 |
} ); |
| 205 |
|
| 206 |
QUnit.test( 'CirrusSearch: redirect appears in the description', ( assert ) => { |
| 207 |
const widget = new ve.ui.MWTemplateTitleInputWidget(); |
| 208 |
|
| 209 |
let option = widget.createOptionWidget( { redirecttitle: 'Template:From' } ); |
| 210 |
assert.strictEqual( |
| 211 |
option.$element.find( '.ve-ui-mwTemplateTitleInputWidget-redirectedfrom' ).text(), |
| 212 |
'(redirectedfrom: From)' |
| 213 |
); |
| 214 |
|
| 215 |
widget.relative = false; |
| 216 |
option = widget.createOptionWidget( { redirecttitle: 'Template:From' } ); |
| 217 |
assert.strictEqual( |
| 218 |
option.$element.find( '.ve-ui-mwTemplateTitleInputWidget-redirectedfrom' ).text(), |
| 219 |
'(redirectedfrom: Template:From)' |
| 220 |
); |
| 221 |
} ); |
| 222 |
} |