-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
251 lines (182 loc) · 6.26 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<title>Test loom.js</title>
<link rel="stylesheet" href="//code.jquery.com/qunit/qunit-1.15.0.css">
<script src="loom.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/qunit/qunit-1.15.0.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script>
loom.debug(true);
QUnit.module("define test");
QUnit.test("test that modules can be defined", function (assert) {
define('testModule1', function () {
return 'data'
});
assert.ok(loom.modules.hasOwnProperty('testModule1'), "'testModule1' is defined");
define('testModule2', function () {
return 'data'
});
assert.ok(loom.modules.hasOwnProperty('testModule2'), "'testModule2' is defined");
});
QUnit.test("test that modules can objects and functions", function (assert) {
define('testModule4', {
data: 'some data',
func: function (e) {
alert(e);
}
});
define('testModule5', function () {
return 'other data'
});
var m4, m5;
require(['testModule4', 'testModule5'], function (tm4, tm5) {
m4 = tm4;
m5 = tm5;
});
assert.equal(m4.data, 'some data', "module as Object");
assert.equal(m5, 'other data', "module as anonymous function");
});
QUnit.module("require test");
QUnit.test("test that jquery registers itself as a module", function (assert) {
var jq;
require('jquery', function ($jq) {
jq = $jq;
});
assert.ok(jq === $, "'jquery' is available");
});
QUnit.test("test that requires can be nested", function (assert) {
assert.ok(loom.modules.hasOwnProperty('testModule1') && loom.modules.hasOwnProperty('testModule2'), "assert that 'testModule1' and 'testModule2' are available");
var m2 = false;
require('testModule1', function () {
require('testModule2', function (tm2) {
m2 = tm2;
});
});
assert.ok(m2 === 'data', "testModule2 resturns `data`");
});
QUnit.test("test that modules can be required", function (assert) {
assert.ok(loom.modules.hasOwnProperty('testModule1') && loom.modules.hasOwnProperty('testModule2'), "assert that 'testModule1' and 'testModule2' are available");
var m1, m2 = false;
require('testModule1', function ($) {
m1 = true;
});
require('testModule1', function ($) {
m2 = true;
});
assert.ok(m1 && m2, "modules can be required");
});
QUnit.module("general tests");
QUnit.test("context of `this`", function (assert) {
define('testModule6', {
data: 'some data',
self: function () {
return this;
}
});
var m6;
require(['testModule6'], function (tm6) {
m6 = tm6;
});
assert.deepEqual(loom.modules['testModule6'].module, m6.self(), '`this` refers to the module itself');
});
QUnit.test("test resolving order and other logic", function (assert) {
define('a', ['c', 'b'], function (c, b) {
console.log('processing a');
return {
c: c,
logVal: function () {
console.log(c.val);
},
init: function () {
console.log('initiated a');
b.init();
c.add();
c.add();
}
};
});
assert.ok(loom.modules.hasOwnProperty('a'), "module 'a' is in the container");
assert.ok(!loom.modules['a'].resolved, "module 'a' is not resolved, yet");
assert.ok(!loom.queue.hasOwnProperty('c') && !loom.queue.hasOwnProperty('b'), "modules 'c' and 'b' not in the queue, yet");
define('c', [], function () {
console.log('processing c');
return {
val: 0,
add: function () {
this.val++;
console.log("val c: " + this.val);
},
init: function () {
console.log('initiated c');
}
};
});
assert.ok(loom.modules.hasOwnProperty('c'), "module 'c' is in the container");
assert.ok(!loom.modules['c'].resolved, "module 'c' is not resolved, yet");
assert.ok(!loom.queue.hasOwnProperty('c') && !loom.queue.hasOwnProperty('b'), "modules 'c' and 'b' not in the queue, yet");
require(['a'], function (a) {
a.init();
a.logVal();
a.c.add();
a.logVal();
console.log('initiated main');
});
assert.ok(loom.modules['c'].resolved, "module 'c' is resolved.");
assert.ok(!loom.modules['a'].resolved, "module 'a' is not resolved.");
assert.ok(loom.queue.hasOwnProperty('b') && loom.queue.hasOwnProperty('a'), "modules 'b' and 'a' are in the queue.");
assert.ok(!loom.queue.hasOwnProperty('c'), "modules 'c' is not in the queue.");
define('d', ['e'], function () {
console.log('processing d');
console.log('this should not be logged, since d is not used');
return {
init: function () {
console.log('initiated e');
}
};
});
assert.ok(!loom.modules['d'].resolved, "module 'd' is not resolved.");
assert.ok(!loom.queue.hasOwnProperty('e'), "module 'e' is not in the queue.");
define('b', ['c'], function (c) {
c.init();
console.log('processing b');
return {
init: function () {
console.log('initiated b');
c.add();
}
};
});
assert.ok(Object.keys(loom.queue).length === 0, "queue is empty");
assert.ok(loom.modules['a'].resolved, "module 'a' is resolved.");
});
QUnit.test("calculator test", function (assert) {
expect(3);
define('Square', ['Multiply'], function (multi) {
return function (num) {
return multi(num, num);
}
});
require(['Square', 'Multiply', 'Add'], function (sq, mul, add) {
assert.equal(sq(3), 9, 'square works');
assert.equal(mul(2, 3), 6, 'multiply works');
assert.equal(add(3, 4), 7, 'adding works');
});
define('Multiply', function () {
return function (num1, num2) {
return num1 * num2;
}
});
define('Add', function () {
return function (num1, num2) {
return num1 + num2;
}
});
});
</script>
</body>
</html>