forked from highcharts/highcharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-utilities.ts
158 lines (134 loc) · 4.02 KB
/
test-utilities.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
/*!*
*
* Copyright (c) Highsoft AS. All rights reserved.
*
*!*/
/**
* Useful functions for test purposes.
*/
class TestUtilities {
/* *
*
* Constants
*
* */
private static readonly timeString: string = new Date().toTimeString();
/**
* A string representation for the current browser. Possible values are
* `Chrome`, `Edge`, `Firefox`, `MSIE`, `Netscape`, `Opera`, `PhantomJS`,
* `Safari`, and an empty string for unknown browsers.
*/
public static readonly browser: string = (function () {
var userAgent = window.navigator.userAgent;
if ((new RegExp('MSIE|Trident', 'i')).test(userAgent) &&
!(new RegExp('Opera', 'i')).test(userAgent)
) {
return 'MSIE';
}
if ((new RegExp('Firefox', 'i')).test(userAgent)) {
return 'Firefox';
}
if ((new RegExp('Edge', 'i')).test(userAgent)) {
return 'Edge';
}
if ((new RegExp('Chrome', 'i')).test(userAgent)) {
return 'Chrome';
}
if ((new RegExp('PhantomJS', 'i')).test(userAgent)) {
return 'PhantomJS';
}
if ((new RegExp('Safari', 'i')).test(userAgent)) {
return 'Safari';
}
if ((new RegExp('Opera', 'i')).test(userAgent)) {
return 'Opera';
}
if ((new RegExp('Netscape', 'i')).test(userAgent)) {
return 'Netscape';
}
return '';
}());
/**
* Indiciates, if system time runs in CET timezone.
*/
private static readonly isCET: boolean = (
TestUtilities.timeString.indexOf('CET') !== -1 ||
TestUtilities.timeString.indexOf('CEST') !== -1 ||
TestUtilities.timeString.indexOf('W. Europe Standard Time') !== -1
);
/* *
*
* Functions
*
* */
/**
* Calls a function only, if the system is set to specific timezones.
*
* @param timezones
* Possible values are CET, CEST, etc.
*
* @param fn
* The function to call. First argument is the matching timezone string.
*/
public static ifTimezone<T>(
timezones: Array<string>, fn: (timezoneString: string) => T
): (T | undefined) {
var timezoneString;
for (var i = 0, ie = timezones.length; i < ie; ++i) {
if (TestUtilities.timeString.indexOf(timezones[i]) >= 0) {
timezoneString = timezones[i];
break;
}
}
if (timezoneString) {
return fn(timezoneString);
}
}
/**
* Convenient wrapper for installing lolex and bypassing
* requestAnimationFrame. Returns a clock object.
*
* @param lolexConfig
* Config supplied to lolex.install
*/
private static lolexInstall (lolexConfig?: any): (LolexClock|undefined) {
if (!lolex) {
return;
}
const win = (window as any);
win.backupRequestAnimationFrame = win.requestAnimationFrame;
win.requestAnimationFrame = null;
// Abort running animations, otherwise they will take over
(Highcharts as any).timers.length = 0;
return lolex.install(lolexConfig);
}
/**
* Convenient wrapper for uninstalling lolex.
*
* @param clock
* The clock object
*/
private static lolexUninstall (clock?: LolexClock) {
if (!clock || !lolex) {
return;
}
const win = (window as any);
clock.uninstall();
// Reset native requestAnimationFrame
win.requestAnimationFrame = win.backupRequestAnimationFrame;
delete win.backupRequestAnimationFrame;
}
/**
* Convenience wrapper for running timeouts and uninstalling lolex.
*
* @param clock
* The clock object
*/
private static lolexRunAndUninstall (clock?: LolexClock) { // eslint-disable-line no-unused-vars
if (!clock || !lolex) {
return;
}
clock.runAll();
TestUtilities.lolexUninstall(clock);
}
}