Debian Codegraph @

tests/jest/mediawiki.special.block/SpecialBlock.setup.js

raw symbols SCIP
1 'use strict';
2
3 const { mount, VueWrapper } = require( '@vue/test-utils' );
4 const { createTestingPinia } = require( '@pinia/testing' );
5
6 const SpecialBlock = require( '../../../resources/src/mediawiki.special.block/SpecialBlock.vue' );
7
8 /**
9 * Mount the SpecialBlock component with the default configuration,
10 * wrapping it in a form element and appending it to the document body.
11 * This is needed because the <form> element is created server-side.
12 *
13 * @param {Object} [config] Configuration to override the defaults.
14 * @param {Array<Object>} [apiMocks] Additional API mocks to add to the default list.
15 * @return {VueWrapper} The mounted component.
16 */
17 function getSpecialBlock( config = {}, apiMocks = [] ) {
18 // Other various mocks that may be needed across the test suite.
19 HTMLElement.prototype.scrollIntoView = jest.fn();
20
21 // Mock calls to mw.config.get() and mw.Api.prototype.get().
22 mockMwConfigGet( config );
23 mockMwApiGet( apiMocks );
24
25 // Create a form element and append it to the document body.
26 const form = document.createElement( 'form' );
27 document.body.appendChild( form );
28
29 // Mount the SpecialBlock component inside the form element.
30 return mount( SpecialBlock, {
31 global: {
32 plugins: [ createTestingPinia( { stubActions: false } ) ]
33 },
34 attachTo: form
35 } );
36 }
37
38 /**
39 * Mock calls to mw.config.get().
40 * The default implementation correlates to the SpecialBlock::codexFormData property in PHP.
41 *
42 * @param {Object} [config] Will be merged with the defaults.
43 */
44 function mockMwConfigGet( config = {} ) {
45 const mockConfig = Object.assign( {
46 wgFormattedNamespaces: {
47 0: '(Main)',
48 1: 'Talk',
49 2: 'User',
50 3: 'User talk'
51 },
52 wgUserLanguage: 'en',
53 blockAlreadyBlocked: false,
54 blockTargetUser: null,
55 blockAdditionalDetailsPreset: [ 'wpAutoBlock' ],
56 blockAllowsEmailBan: true,
57 blockAllowsUTEdit: true,
58 blockAutoblockExpiry: '1 day',
59 blockDetailsPreset: [],
60 blockExpiryDefault: '',
61 blockExpiryPreset: null,
62 blockHideUser: true,
63 blockExpiryOptions: {
64 infinite: 'infinite',
65 '31 horas': '31 hours'
66 },
67 blockNamespaceRestrictions: '',
68 blockPageRestrictions: '',
69 blockPreErrors: [],
70 blockReasonOptions: [
71 { label: 'block-reason-1', value: 'block-reason-1' },
72 { label: 'block-reason-2', value: 'block-reason-2' }
73 ],
74 blockSuccessMsg: '[[Special:Contributions/ExampleUser|ExampleUser]] has been blocked.',
75 blockTypePreset: 'sitewide'
76 }, config );
77 mw.config.get.mockImplementation( ( key ) => mockConfig[ key ] );
78 }
79
80 /**
81 * Mock calls to mw.Api.prototype.get() based on given parameters and response.
82 * The default implementation mocks API GET requests used across the test suite.
83 *
84 * @param {Array<Object>} [additionalMocks] Additional mocks to add to the default list.
85 * Each object should contain the keys `params` and `response`.
86 * `params` is an Object with sufficient set of parameters to identify the request
87 * (e.g. `{ list: 'logevents', letype: 'block' }`). The `response` is an Object
88 * with the expected response data (e.g. `{ query: { logevents: [ ... ] } }`).
89 */
90 function mockMwApiGet( additionalMocks = [] ) {
91 /**
92 * This is intended to encapsulate any API requests that
93 * consistently need to be mocked across the test suite.
94 *
95 * @type {Object}
96 */
97 const mocks = [
98 // Used in TargetBlockLog
99 {
100 params: {
101 list: 'logevents',
102 letype: 'block',
103 letitle: 'User:ExampleUser'
104 },
105 response: {
106 query: {
107 logevents: [
108 {
109 logid: 980,
110 title: 'User:ExampleUser',
111 params: {
112 duration: '1 year',
113 flags: [
114 'noautoblock'
115 ],
116 sitewide: true,
117 expiry: '2029-09-17T14:30:51Z'
118 },
119 type: 'block',
120 user: 'Admin',
121 timestamp: '2024-09-17T14:30:51Z',
122 comment: 'A reason'
123 }
124 ]
125 }
126 }
127 },
128 {
129 params: {
130 list: 'logevents',
131 letype: 'block',
132 letitle: 'User:NeverBlocked'
133 },
134 response: {
135 query: {
136 logevents: []
137 }
138 }
139 },
140 {
141 params: {
142 list: 'logevents',
143 letype: 'block',
144 letitle: 'User:BlockedALot'
145 },
146 response: {
147 continue: {
148 lecontinue: '20240909144407|979',
149 continue: '-||'
150 },
151 query: {
152 logevents: [
153 {
154 logid: 980,
155 title: 'User:BlockedALot',
156 params: {
157 duration: '1 year',
158 flags: [
159 'noautoblock'
160 ],
161 sitewide: true,
162 expiry: '2029-09-17T14:30:51Z'
163 },
164 type: 'block',
165 user: 'Admin',
166 timestamp: '2024-09-17T14:30:51Z',
167 comment: 'A reason'
168 },
169 {
170 logid: 981,
171 title: 'User:BlockedALot',
172 params: {
173 duration: '1 year',
174 flags: [
175 'noautoblock'
176 ],
177 sitewide: true,
178 expiry: '2029-09-18T14:30:51Z'
179 },
180 type: 'block',
181 user: 'Admin',
182 timestamp: '2024-09-18T14:30:51Z',
183 comment: 'A reason'
184 },
185 {
186 logid: 982,
187 title: 'User:BlockedALot',
188 params: {
189 duration: '1 year',
190 flags: [
191 'noautoblock'
192 ],
193 sitewide: true,
194 expiry: '2029-09-19T14:30:51Z'
195 },
196 type: 'block',
197 user: 'Admin',
198 timestamp: '2024-09-19T14:30:51Z',
199 comment: 'A reason'
200 },
201 {
202 logid: 983,
203 title: 'User:BlockedALot',
204 params: {
205 duration: '1 year',
206 flags: [
207 'noautoblock'
208 ],
209 sitewide: true,
210 expiry: '2029-09-20T14:30:51Z'
211 },
212 type: 'block',
213 user: 'Admin',
214 timestamp: '2024-09-20T14:30:51Z',
215 comment: 'A reason'
216 },
217 {
218 logid: 984,
219 title: 'User:BlockedALot',
220 params: {
221 duration: '1 year',
222 flags: [
223 'noautoblock'
224 ],
225 sitewide: true,
226 expiry: '2029-09-21T14:30:51Z'
227 },
228 type: 'block',
229 user: 'Admin',
230 timestamp: '2024-09-21T14:30:51Z',
231 comment: 'A reason'
232 },
233 {
234 logid: 985,
235 title: 'User:BlockedALot',
236 params: {
237 duration: '1 year',
238 flags: [
239 'noautoblock'
240 ],
241 sitewide: true,
242 expiry: '2029-09-22T14:30:51Z'
243 },
244 type: 'block',
245 user: 'Admin',
246 timestamp: '2024-09-22T14:30:51Z',
247 comment: 'A reason'
248 },
249 {
250 logid: 986,
251 title: 'User:BlockedALot',
252 params: {
253 duration: '1 year',
254 flags: [
255 'noautoblock'
256 ],
257 sitewide: true,
258 expiry: '2029-09-23T14:30:51Z'
259 },
260 type: 'block',
261 user: 'Admin',
262 timestamp: '2024-09-23T14:30:51Z',
263 comment: 'A reason'
264 },
265 {
266 logid: 987,
267 title: 'User:BlockedALot',
268 params: {
269 duration: '1 year',
270 flags: [
271 'noautoblock'
272 ],
273 sitewide: true,
274 expiry: '2029-09-24T14:30:51Z'
275 },
276 type: 'block',
277 user: 'Admin',
278 timestamp: '2024-09-24T14:30:51Z',
279 comment: 'A reason'
280 },
281 {
282 logid: 988,
283 title: 'User:BlockedALot',
284 params: {
285 duration: '1 year',
286 flags: [
287 'noautoblock'
288 ],
289 sitewide: true,
290 expiry: '2029-09-25T14:30:51Z'
291 },
292 type: 'block',
293 user: 'Admin',
294 timestamp: '2024-09-25T14:30:51Z',
295 comment: 'A reason'
296 },
297 {
298 logid: 989,
299 title: 'User:BlockedALot',
300 params: {
301 duration: '1 year',
302 flags: [
303 'noautoblock'
304 ],
305 sitewide: true,
306 expiry: '2029-09-26T14:30:51Z'
307 },
308 type: 'block',
309 user: 'Admin',
310 timestamp: '2024-09-26T14:30:51Z',
311 comment: 'A reason'
312 }
313 ]
314 }
315 }
316 },
317 // Used in UserLookup
318 {
319 params: {
320 list: 'allusers'
321 },
322 response: {
323 query: {
324 allusers: [
325 { name: 'UserLookup1' },
326 { name: 'UserLookup2' }
327 ]
328 }
329 }
330 },
331 // Add more mocks as needed above this line.
332 ...additionalMocks
333 ];
334 mw.Api.prototype.get.mockImplementation( ( params ) => {
335 if ( !params ) {
336 // eslint-disable-next-line no-console
337 console.warn( 'No params provided to mw.Api.get()' );
338 return Promise.resolve( jest.fn() );
339 }
340 // Find the appropriate mock from the list based on the expected parameters.
341 const mock = mocks.find( ( m ) => Object.entries( m.params )
342 .every( ( [ key, value ] ) => params[ key ] === value )
343 );
344 if ( !mock ) {
345 // eslint-disable-next-line no-console
346 console.warn( 'No mock found for:', params );
347 return Promise.resolve( jest.fn() );
348 }
349 return Promise.resolve( mock.response );
350 } );
351 }
352
353 module.exports = {
354 getSpecialBlock,
355 mockMwApiGet,
356 mockMwConfigGet
357 };