-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
195 lines (148 loc) · 5.49 KB
/
test.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
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
import { describe, it } from 'mocha';
import { LoaderQueue, QueueEventType } from './index';
import * as assert from 'assert';
describe('LoaderQueue', () => {
it('Should add LoaderQueueEvent', async () => {
const loaderQueue = new LoaderQueue();
const newEvent = await loaderQueue.add('TestEvent');
assert.equal(loaderQueue.length, 1, 'Loader queue length not as expected');
assert.equal(newEvent.common_id, 'TestEvent', 'LoaderQueueEvent common_id not as expected');
assert.notEqual(newEvent.unique_id, 'TestEvent', 'LoaderQueueEvent unique_id not as expected');
return;
});
it('Should reference same queueInstance', async () => {
const loaderQueue = new LoaderQueue();
const event = await loaderQueue.add('TestEvent');
assert.equal((event as any)._queueInstance, loaderQueue, 'Loader queue length not as expected');
return;
});
it('Should create unique identifiers', async () => {
const loaderQueue = new LoaderQueue();
const events = [
await loaderQueue.add('TestEvent'),
await loaderQueue.add('TestEvent')
];
assert.equal(events[0].common_id, events[1].common_id, 'LoaderQueueEvent common_ids should match');
assert.notEqual(events[0].unique_id, events[1].unique_id, 'LoaderQueueEvent unique_ids should not collide');
return;
});
it('Should remove single instance via string', async () => {
const loaderQueue = new LoaderQueue();
const events = [
await loaderQueue.add('TestEvent'),
await loaderQueue.add('TestEvent')
];
await loaderQueue.remove('TestEvent', false);
assert.equal(loaderQueue.length, 1, 'LoaderQueue length not as expected following remove() event');
return;
});
it('Should remove all instances via string', async () => {
const loaderQueue = new LoaderQueue();
const events = [
await loaderQueue.add('TestEvent'),
await loaderQueue.add('TestEvent'),
await loaderQueue.add('TestEvent2')
];
await loaderQueue.remove('TestEvent');
assert.equal(loaderQueue.length, 1, 'LoaderQueue length not as expected following remove() event');
return;
});
it('Should remove localized instance of event', async () => {
const loaderQueue = new LoaderQueue();
await loaderQueue.add('TestEvent');
await loaderQueue.add('TestEvent2');
const event = await loaderQueue.add('TestEvent3');
event.remove();
assert.equal(loaderQueue.length, 2, 'LoaderQueue length not as expected following remove() event');
return;
});
it('Should clear queue', async () => {
const loaderQueue = new LoaderQueue();
await loaderQueue.add('TestEvent');
await loaderQueue.add('TestEvent2');
await loaderQueue.add('TestEvent3');
await loaderQueue.clear();
assert.equal(0, 0, 'LoaderQueue length not as expected following clear() event');
return;
});
it('Should auto-remove event if passed as a promise (resolve)', (done) => {
const loaderQueue = new LoaderQueue();
const promiseFn = () => {
return new Promise<any>((resolve, reject) => {
setTimeout(() => {
resolve();
}, 10);
});
};
loaderQueue.add(promiseFn()).then(() => {
setTimeout(() => {
assert.equal(0, loaderQueue.length, 'LoaderQueue length not as expected following add() event');
done();
}, 20);
});
});
it('Should auto-remove event if passed as a promise (reject)', (done) => {
const loaderQueue = new LoaderQueue();
const promiseFn = () => {
return new Promise<any>((resolve, reject) => {
setTimeout(() => {
reject();
}, 10);
});
};
loaderQueue.add(promiseFn()).then(() => {
setTimeout(() => {
assert.equal(0, loaderQueue.length, 'LoaderQueue length not as expected following add() event');
done();
}, 20);
});
});
it('Should create unique identifiers for events passed as promises', async () => {
const loaderQueue = new LoaderQueue();
const promiseFn = () => {
return new Promise<any>((resolve) => {
setTimeout(() => {
resolve();
}, 10);
});
};
const events = [
await loaderQueue.add(promiseFn()),
await loaderQueue.add(promiseFn())
];
assert.notEqual(events[0].unique_id, events[1].unique_id, 'LoaderQueueEvent unique_ids should be unique');
});
});
describe('LoaderQueue.events', () => {
it(`Should emit ${QueueEventType.Add} event upon add`, (done) => {
const loaderQueue = new LoaderQueue();
const sub = loaderQueue.events.subscribe((ev) => {
sub.unsubscribe();
assert.equal(ev.type, QueueEventType.Add, 'Event type fired not as expected');
done();
});
loaderQueue.add('TestEvent');
});
it(`Should emit ${QueueEventType.Remove} event upon remove`, (done) => {
const loaderQueue = new LoaderQueue();
loaderQueue.add('TestEvent').then((event) => {
const sub = loaderQueue.events.subscribe((ev) => {
sub.unsubscribe();
assert.equal(ev.type, QueueEventType.Remove, 'Event type fired not as expected');
done();
});
event.remove();
});
});
it(`Should emit ${QueueEventType.Clear} event upon clear`, (done) => {
const loaderQueue = new LoaderQueue();
loaderQueue.add('TestEvent').then((event) => {
const sub = loaderQueue.events.subscribe((ev) => {
sub.unsubscribe();
assert.equal(ev.type, QueueEventType.Clear, 'Event type fired not as expected');
done();
});
loaderQueue.clear();
});
});
});