-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjest.setup.js
163 lines (139 loc) · 3.56 KB
/
jest.setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*!
* Rigging for the WikiLambda browser jest UX unit testing suite
*
* @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
* @license MIT
*/
'use strict';
/* global jest, mockLocalStorage, $ */
// Assign things to "global" here if you want them to be globally available during tests
const fs = require( 'fs' ),
path = require( 'path' ),
Constants = require( './resources/ext.wikilambda.app/Constants.js' ),
vueTestUtils = require( '@vue/test-utils' ),
vuex = require( 'vuex' );
// Mocking window.location.href
Object.defineProperty( global.window, 'location', {
writable: true,
value: { href: jest.fn(), protocol: 'http:' }
} );
global.$ = require( 'jquery' );
global.mockLocalStorage = {};
global.toQueryParam = function ( param ) {
return Object.keys( param )
.map( ( key ) => key + '=' + param[ key ] )
.join( '&' );
};
function Api() {}
Api.prototype.get = jest.fn().mockReturnValue( $.Deferred().resolve().promise() );
class Title {
constructor( page ) {
this.page = page;
}
getUrl( param ) {
if ( param && Object.keys( param ).length > 0 ) {
param.title = this.page;
return Constants.PATHS.ROUTE_FORMAT_ONE + '?' + global.toQueryParam( param );
}
return Constants.PATHS.ROUTE_FORMAT_TWO + this.page;
}
}
const englishMessages = JSON.parse( fs.readFileSync( path.join( __dirname, './i18n/en.json' ) ) );
class Mocki18n {
constructor( string ) {
this.string = string;
}
text() {
return englishMessages[ this.string ];
}
toString() {
return this.text();
}
params() {
return this;
}
parse() {
return englishMessages[ this.string ];
}
}
// Mock MW object
global.mw = {
Api: Api,
config: {
get: jest.fn( ( endpoint ) => {
switch ( endpoint ) {
case 'wgWikiLambda':
return {
createNewPage: true,
viewmode: true,
zobject: { Z1K1: 'Z2', Z2K1: 'Z0' },
zlangZid: 'Z1002',
zlang: 'en'
};
default:
return {};
}
} ),
values: {
wgUserName: 'username'
}
},
user: {
isAnon: jest.fn().mockReturnValue( true ),
getRights: jest.fn().mockReturnValue( {
then: jest.fn().mockReturnValue( [] )
} )
},
language: {
getFallbackLanguageChain: function () {
return [ 'en' ];
},
listToText: function () {
return 'list';
}
},
storage: {
get: jest.fn( ( key ) => mockLocalStorage[ key ] ),
set: jest.fn( ( key, value ) => {
mockLocalStorage[ key ] = value;
} )
},
track: jest.fn( ( trackkey, trackmessage ) => {
console.log( 'Log emitted: ' + trackkey + ' - ' + trackmessage );
} ),
eventLog: {
dispatch: jest.fn( ( eventName, customData ) => {
console.log( 'Metrics Platform event emitted: ' + eventName + ' - ' + JSON.stringify( customData ) );
} ),
submitInteraction: jest.fn( ( streamName, schemaID, action, interactionData ) => {
console.log( 'Metrics Platform event emitted using submitInteraction: ' + action + ' - ' + JSON.stringify( interactionData ) );
} )
},
message: jest.fn( ( str ) => new Mocki18n( str ) ),
Uri: jest.fn().mockReturnValue( {
path: jest.fn(),
query: jest.fn()
} ),
Title: Title
// other mw properties as needed...
};
// Mock i18n & store for all tests
global.$i18n = jest.fn( ( str ) => new Mocki18n( str ) );
global.getters = {};
global.state = {};
global.mutations = {};
global.actions = {};
global.modules = {};
global.store = vuex.createStore( {
state() {
return global.state;
},
getters: global.getters,
mutations: global.mutations,
actions: global.actions,
modules: global.modules
} );
vueTestUtils.config.global.mocks = {
$i18n: global.$i18n
};
vueTestUtils.config.global.plugins = [ global.store ];