-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
129 lines (94 loc) · 3.21 KB
/
test.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
const wrun = require('./wrun.min.js');
if (typeof wrun !== 'function') {
throw 'wrun must export a function by default';
}
// ---------------------------------------------------------------
const $w1 = wrun(function(){});
if ($w1.worker) {
throw 'A worker must not be created without a window object available on the scope';
}
if (!$w1.error) {
throw 'An { error } must be returned when there is no window available on the scope';
}
if ($w1.error.code !== 1) {
throw `When there is no window on the scope, the returned error must have { code: 1 }, given { code: ${$w1.error.code} }`;
}
// ---------------------------------------------------------------
global.window = {};
// ---------------------------------------------------------------
const $w2 = wrun(function(){});
if ($w2.error.code !== 2) {
throw `Call wrun without Blob or URL in window must return an error { code: 2 }, given ${JSON.stringify($w2.error)}`;
}
// ---------------------------------------------------------------
window.Blob = class Blob {
constructor() {
return "Blob-Mock";
}
}
window.URL = {
createObjectURL() {
return "URL-Mock"
}
};
const $w3 = wrun(function(){});
if ($w3.error.code !== 3) {
throw `Trying to create a Worker type that is not in window must return error { code: 3 }, given ${JSON.stringify($w3.error)}`;
}
// ---------------------------------------------------------------
window.Blob = class Blob {
constructor(content, btype) {
if (!Array.isArray(content)) {
throw 'Wrong blob content is being passed';
}
if (JSON.stringify(btype) !== '{"type":"application/javascript"}') {
throw `The blob type must be: { type: 'application/javascript' }. Given: ${JSON.stringify(btype)}`;
}
if (content[0] !== '(function(){})()') {
throw 'The blob function must create an IIFE that wraps the wrun argument function';
}
}
check() {
return "Blob-Mock";
}
}
window.URL = {
createObjectURL(obj) {
if (!obj || !obj.check) {
throw 'The blob payload is not being created to the createObjectURL function';
}
return obj.check() + " URL-Mock";
}
};
window.Worker = class {
constructor(content) {
if (content !== 'Blob-Mock URL-Mock') {
throw 'A Blob must be created and passed to a dynamic Object URL that will be used to create the Worker';
}
}
};
// ---------------------------------------------------------------
const $w4 = wrun(function(){});
if ($w4.error && $w4.error.code === 3) {
throw `The default created worker must be type "Worker"`;
}
// ---------------------------------------------------------------
const $w5 = wrun(1);
if ($w5.error && $w5.error.code !== 4) {
throw `The wrun must accept only functions as parameters`;
}
// ---------------------------------------------------------------
const fn = function() {};
fn.toString = null;
const $w6 = wrun(fn);
if ($w6.error && $w6.error.code !== 0) {
throw `Deformed parameters or uncaught behavior must generate an error { code: 0 }`;
}
// ---------------------------------------------------------------
console.log(`
********************************
* *
* DONE, ALL TESTS PASSED! *
* *
********************************
`);