/
async-test.js
121 lines (95 loc) · 2.91 KB
/
async-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
// ==========================================================================
// Project: Spade - CommonJS Runtime
// Copyright: ©2010 Strobe Inc. All rights reserved.
// License: Licened under MIT license (see __preamble__.js)
// ==========================================================================
/*globals spade */
(function() {
var t = {}, Spade = spade.Spade;
// ..........................................................
// BASIC REQUIRE
//
module('spade: async require', {
setup: function() {
t.spade = new Spade();
// preload a module
t.spade.register('foo/baz', function(require,e) {
e.id = 'foo/baz';
e.async = require.async; // export for testing
});
// dummy loader loads only foo/bar on demand after delay
t.spade.loader = {
requests: 0,
loadFactory: function(spade, id, done) {
this.requests++;
if (id === 'foo/bar') {
setTimeout(function() {
spade.register(id, function(r,e) { e.id='foo/bar'; });
done();
}, 10);
} else {
done('Not Found'); // immediately
}
}
};
},
teardown: function() {
delete t.spade;
}
});
test('should not talk to loader if registered', function() {
var spade = t.spade;
stop(1000);
spade.async('foo/baz', function(err) {
start();
equal(err, null);
equal(spade.loader.requests, 0, 'loader should not have been called');
equal(spade.require('foo/baz').id, 'foo/baz', 'should find foo');
});
});
test('should let loader register', function() {
var spade = t.spade;
stop(1000);
spade.async('foo/bar', function(err) {
start();
equal(err, null);
equal(spade.loader.requests, 1, 'loader should have been called');
equal(spade.require('foo/bar').id, 'foo/bar', 'should find foo');
});
});
test('should normalize id', function() {
var spade = t.spade;
stop(1000);
spade.async('/./foo/baz/../bar', function(err) {
start();
equal(err, null);
equal(spade.loader.requests, 1, 'loader should have been called');
equal(spade.require('foo/bar').id, 'foo/bar', 'should find foo');
});
});
test('should expose async inside of module', function() {
var spade = t.spade;
stop(1000);
var async = spade.require('foo/baz').async;
ok(async, 'should have an async function');
// normalize relative to async
async('./bar', function(err) {
start();
equal(err, null);
equal(spade.loader.requests, 1, 'loader should have been called');
equal(spade.require('foo/bar').id, 'foo/bar', 'should find foo');
});
});
test('should return err if loader does not register', function() {
var spade = t.spade;
stop(1000);
spade.async('imaginary/bar', function(err) {
start();
equal(err, 'Not Found');
equal(spade.loader.requests, 1, 'loader should have been called');
raises(function() {
spade.require('imaginary/bar');
});
});
});
})();