Debian Codegraph @

tests/jest/jest.setup.js

raw symbols SCIP
1 'use strict';
2
3 const { config } = require( '@vue/test-utils' );
4
5 /**
6 * Mock for the calls to Core's $i18n plugin which returns a mw.Message object.
7 *
8 * @param {string} key The key of the message to parse.
9 * @param {...*} args Arbitrary number of arguments to be parsed.
10 * @return {Object} mw.Message-like object with .text() and .parse() methods.
11 */
12 function $i18nMock( key, ...args ) {
13 function serializeArgs() {
14 return args.length ? `${ key }:[${ args.join( ',' ) }]` : key;
15 }
16 return {
17 text: () => serializeArgs(),
18 parse: () => serializeArgs()
19 };
20 }
21 // Mock Vue plugins in test suites.
22 config.global.provide = {
23 i18n: $i18nMock
24 };
25 config.global.mocks = {
26 $i18n: $i18nMock
27 };
28 config.global.directives = {
29 'i18n-html': ( el, binding ) => {
30 el.innerHTML = `${ binding.arg } (${ binding.value })`;
31 }
32 };
33
34 function ApiMock() {}
35 ApiMock.prototype.get = jest.fn();
36 ApiMock.prototype.post = jest.fn();
37 ApiMock.prototype.postWithEditToken = jest.fn();
38 ApiMock.prototype.postWithToken = jest.fn();
39
40 function RestMock() {}
41 RestMock.prototype.get = jest.fn();
42
43 function TitleMock() {}
44 TitleMock.prototype.getMainText = jest.fn();
45 TitleMock.prototype.getNameText = jest.fn();
46 TitleMock.prototype.getUrl = jest.fn();
47
48 // Mock the mw global object.
49 const mw = {
50 Api: ApiMock,
51 log: {
52 error: jest.fn(),
53 warn: jest.fn()
54 },
55 config: {
56 get: jest.fn()
57 },
58 message: jest.fn( ( key ) => ( {
59 text: jest.fn( () => key ),
60 parse: jest.fn()
61 } ) ),
62 msg: jest.fn( ( key ) => key ),
63 user: {
64 getId: jest.fn(),
65 getName: jest.fn(),
66 isAnon: jest.fn().mockReturnValue( true ),
67 options: {
68 get: jest.fn()
69 }
70 },
71 language: {
72 convertNumber: jest.fn( ( x ) => x ),
73 getFallbackLanguageChain: () => [ 'en' ]
74 },
75 Title: TitleMock,
76 util: {
77 getUrl: jest.fn(),
78 isInfinity: jest.fn()
79 },
80 Rest: RestMock
81 // Add more mw properties as needed…
82 };
83
84 // Assign things to "global" here if you want them to be globally available during Jest tests.
85 global.mw = mw;