-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
141 lines (107 loc) · 3.64 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
130
131
132
133
134
135
136
137
138
139
140
141
describe('floppy', function () {
'use strict';
var assume = require('assume')
, Floppy = require('./')
, floppy;
beforeEach(function () {
floppy = new Floppy('//google.com/ga.js');
});
afterEach(function each() {
floppy.destroy();
});
it('is exposed as function', function () {
assume(Floppy).is.a('function');
});
it('can be created without new keyword', function () {
assume(Floppy()).is.instanceOf(Floppy);
});
it('has default readyState of loading', function () {
assume(floppy.readyState).equals(Floppy.LOADING);
});
describe('#add', function () {
it('increments the dependent', function () {
var loaded = false;
assume(floppy.dependent).equals(0);
assume(floppy.add(function () { loaded = true; })).is.true();
assume(floppy.dependent).equals(1);
assume(loaded).is.false();
});
it('executes the supplied directly if we are already loaded', function () {
var loaded = false;
assume(floppy.dependent).equals(0);
floppy.exec();
assume(floppy.add(function () { loaded = true; })).is.true();
assume(floppy.dependent).equals(1);
assume(loaded).is.true();
});
it('does not add anything to dead files', function () {
var loaded = false;
assume(floppy.dependent).equals(0);
floppy.destroy();
assume(floppy.add(function (err) {
assume(err.message).includes('destroyed');
loaded = true;
})).is.false();
assume(floppy.dependent).equals(0);
assume(loaded).is.true();
});
});
describe('#destroy', function () {
it('sets the readyState to dead', function () {
assume(floppy.destroy()).equals(floppy);
assume(floppy.readyState).equals(Floppy.DEAD);
});
it('calls functions that were added using #unload', function (next) {
assume(floppy.unload(next)).equals(floppy);
floppy.destroy();
});
it('calls all queued callbacks with an error argument', function (next) {
floppy.add(function (err) {
assume(err.message).includes('destroyed');
next();
});
floppy.destroy();
});
});
describe('#exec', function () {
it('will destroy the instance when called with an error', function () {
floppy.add(function () {});
floppy.exec(new Error('lol'));
assume(floppy.readyState).equals(Floppy.DEAD);
});
it('will execute all assigned callbacks in order', function () {
var order = [];
floppy.add(function () { order.push(1); });
floppy.add(function () { order.push(2); });
floppy.add(function () { order.push(3); });
assume(floppy.exec()).equals(floppy);
assume(order.join(',')).equals('1,2,3');
});
it('calls the callbacks with the arguments', function (next) {
floppy.add(function (err, foo, bar) {
if (err) return next(err);
assume(foo).equals('foo');
assume(bar).equals('bar');
next();
});
floppy.exec(undefined, 'foo', 'bar');
});
});
describe('#eject', function () {
it('will not destroy when there are still dependent', function () {
var deps = 2;
floppy.add(function () {});
floppy.add(function () {});
assume(floppy.readyState).equals(Floppy.LOADING);
floppy.exec();
assume(floppy.readyState).equals(Floppy.LOADED);
assume(floppy.dependent).equals(2);
assume(floppy.eject()).is.false();
assume(floppy.readyState).equals(Floppy.LOADED);
assume(floppy.dependent).equals(1);
assume(floppy.eject()).is.true();
assume(floppy.readyState).equals(Floppy.DEAD);
assume(floppy.dependent).equals(0);
});
});
});