forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestScheduler-ui.ts
284 lines (237 loc) · 8.39 KB
/
testScheduler-ui.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import * as _ from 'lodash';
//import * as commonInterface from 'mocha/lib/interfaces/common';
//import * as escapeRe from 'escape-string-regexp';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
import * as Rx from 'rxjs/Rx';
import * as marble from './marble-testing';
//tslint:disable:no-var-requires no-require-imports
const commonInterface = require('mocha/lib/interfaces/common');
const escapeRe = require('escape-string-regexp');
//tslint:enable:no-var-requires no-require-imports
/** Polyfill requestAnimationFrame for testing animationFrame scheduler in Node */
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function(this: any, window: any) {
window = window || this;
let lastTime = 0;
const vendors = ['ms', 'moz', 'webkit', 'o'];
for (let x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']
|| window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (callback: Function, element: any) => {
const currTime = new Date().getTime();
const timeToCall = Math.max(0, 16 - (currTime - lastTime));
const id = window.setTimeout(() => { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = (id: number) => {
clearTimeout(id);
};
}
}(global));
//setup sinon-chai
chai.use(sinonChai);
declare const module: any, global: any, Suite: any, Test: any;
if (global && !(typeof window !== 'undefined')) {
global.mocha = require('mocha'); // tslint:disable-line:no-require-imports no-var-requires
global.Suite = global.mocha.Suite;
global.Test = global.mocha.Test;
}
if (!global.Promise) {
global.Promise = require('promise'); // tslint:disable-line:no-require-imports no-var-requires
}
const diagramFunction = global.asDiagram;
//mocha creates own global context per each test suite, simple patching to global won't deliver its context into test cases.
//this custom interface is just mimic of existing one amending test scheduler behavior previously test-helper does via global patching.
module.exports = function(suite: any) {
const suites = [suite];
suite.on('pre-require', function(context: any, file: any, mocha: any) {
const common = (<any>commonInterface)(suites, context);
context.before = common.before;
context.after = common.after;
context.beforeEach = common.beforeEach;
context.afterEach = common.afterEach;
context.run = mocha.options.delay && common.runWithSuite(suite);
//setting up per-context test scheduler
context.rxTestScheduler = null;
//setting up assertion, helper for marble testing
context.hot = marble.hot;
context.cold = marble.cold;
context.expectObservable = marble.expectObservable;
context.expectSubscriptions = marble.expectSubscriptions;
context.time = marble.time;
/**
* Describe a "suite" with the given `title`
* and callback `fn` containing nested suites
* and/or tests.
*/
context.describe = context.context = function(title: any, fn: any) {
const suite = (<any>Suite).create(suites[0], title);
suite.file = file;
suites.unshift(suite);
fn.call(suite);
suites.shift();
return suite;
};
/**
* Pending describe.
*/
context.xdescribe = context.xcontext = context.describe.skip = function(title: any, fn: any) {
const suite = (<any>Suite).create(suites[0], title);
suite.pending = true;
suites.unshift(suite);
fn.call(suite);
suites.shift();
};
/**
* Exclusive suite.
*/
context.describe.only = function(title: any, fn: any) {
const suite = context.describe(title, fn);
mocha.grep(suite.fullTitle());
return suite;
};
/**
* Describe a test case to test type definition
* sanity on build time. Recommended only for
* exceptional type definition won't be used in test cases.
*/
context.type = function(title: any, fn: any) {
//intentionally does not execute to avoid unexpected side effect occurs by subscription,
//or infinite source. Suffecient to check build time only.
};
function stringify(x: any): string {
return JSON.stringify(x, function (key: string, value: any) {
if (Array.isArray(value)) {
return '[' + value
.map(function (i) {
return '\n\t' + stringify(i);
}) + '\n]';
}
return value;
})
.replace(/\\"/g, '"')
.replace(/\\t/g, '\t')
.replace(/\\n/g, '\n');
}
function deleteErrorNotificationStack(marble: any) {
const { notification } = marble;
if (notification) {
const { kind, error } = notification;
if (kind === 'E' && error instanceof Error) {
notification.error = { name: error.name, message: error.message };
}
}
return marble;
}
/**
* custom assertion formatter for expectObservable test
*/
function observableMatcher(actual: any, expected: any) {
if (Array.isArray(actual) && Array.isArray(expected)) {
actual = actual.map(deleteErrorNotificationStack);
expected = expected.map(deleteErrorNotificationStack);
const passed = _.isEqual(actual, expected);
if (passed) {
return;
}
let message = '\nExpected \n';
actual.forEach((x: any) => message += `\t${stringify(x)}\n`);
message += '\t\nto deep equal \n';
expected.forEach((x: any) => message += `\t${stringify(x)}\n`);
chai.assert(passed, message);
} else {
chai.assert.deepEqual(actual, expected);
}
}
/**
* Describe a specification or test-case
* with the given `title` and callback `fn`
* acting as a thunk.
*/
const it = context.it = context.specify = function(title: any, fn: any) {
context.rxTestScheduler = null;
let modified = fn;
if (fn && fn.length === 0) {
modified = function () {
context.rxTestScheduler = new Rx.TestScheduler(observableMatcher);
try {
fn();
context.rxTestScheduler.flush();
} finally {
context.rxTestScheduler = null;
}
};
}
const suite = suites[0];
if (suite.pending) {
modified = null;
}
const test = new (<any>Test)(title, modified);
test.file = file;
suite.addTest(test);
return test;
};
/**
* Describe a specification or test-case
* to be represented as marble diagram png.
* It will still serve as normal test cases as well.
*/
context.asDiagram = function (label: any) {
if (diagramFunction) {
return diagramFunction(label, it);
}
return it;
};
/**
* Exclusive test-case.
*/
context.it.only = function(title: any, fn: any) {
const test = it(title, fn);
const reString = '^' + (<any>escapeRe)(test.fullTitle()) + '$';
mocha.grep(new RegExp(reString));
return test;
};
/**
* Pending test case.
*/
context.xit = context.xspecify = context.it.skip = function(title: string) {
context.it(title);
};
/**
* Number of attempts to retry.
*/
context.it.retries = function(n: number) {
context.retries(n);
};
});
};
//register into global instnace if browser test page injects mocha globally
if (global.Mocha) {
(<any>window).Mocha.interfaces['testschedulerui'] = module.exports;
} else {
(<any>mocha).interfaces['testschedulerui'] = module.exports;
}
//overrides JSON.toStringfy to serialize error object
Object.defineProperty(Error.prototype, 'toJSON', {
value: function (this: any) {
const alt = {};
Object.getOwnPropertyNames(this).forEach(function (this: any, key: string) {
if (key !== 'stack') {
alt[key] = this[key];
}
}, this);
return alt;
},
configurable: true
});