Skip to content

Commit 079adf6

Browse files
committed
improve handling of async unit tests
1 parent a91c8ed commit 079adf6

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

test/org/osflash/signals/PriorityListenersTest.test.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ import { assert } from "chai";
1111

1212
import { AsyncUtil } from "../../../util/AsyncUtil";
1313
import { DeluxeSignal } from "../../../../src/org/osflash/signals/DeluxeSignal";
14+
1415
describe("PriorityListenersTest", () => {
1516
let async: AsyncUtil = new AsyncUtil();
1617

1718
let completed: DeluxeSignal;
19+
let listenersToCall: number;
1820
let listenersCalled: any[];
21+
let callback: Function;
1922

2023
beforeEach(() => {
2124
completed = new DeluxeSignal(this);
@@ -26,34 +29,61 @@ describe("PriorityListenersTest", () => {
2629
completed.removeAll();
2730
completed = null;
2831
listenersCalled = null;
29-
});
30-
31-
it("listener_added_second_with_higher_priority_should_be_called_first()", done => {
32-
completed.addWithPriority(async.add(listener1, 5));
33-
completed.addWithPriority(async.add(listener0, 5, done), 10);
34-
35-
completed.dispatch();
32+
callback = null;
3633
});
3734

3835
function listener0(): void {
3936
listenersCalled.push(listener0);
4037
assert.equal(listener0, listenersCalled[0], "this should be the first listener called");
38+
checkCallback();
4139
}
4240

4341
function listener1(): void {
4442
listenersCalled.push(listener1);
4543
assert.equal(listener1, listenersCalled[1], "this should be the second listener called");
44+
checkCallback();
4645
}
4746

4847
function listener2(): void {
4948
listenersCalled.push(listener2);
5049
assert.equal(listener2, listenersCalled[2], "this should be the third listener called");
50+
checkCallback();
51+
}
52+
53+
function checkCallback(): void {
54+
if (listenersCalled.length >= listenersToCall) {
55+
callback();
56+
}
5157
}
5258

59+
it("listener_added_second_with_higher_priority_should_be_called_first()", done => {
60+
listenersToCall = 2;
61+
callback = done;
62+
63+
completed.addWithPriority(async.add(listener1, 5));
64+
completed.addWithPriority(async.add(listener0, 5), 10);
65+
66+
completed.dispatch();
67+
});
68+
5369
it("listeners_added_with_same_priority_should_be_called_in_order_added()", done => {
70+
listenersToCall = 3;
71+
callback = done;
72+
5473
completed.addWithPriority(async.add(listener0, 5), 10);
5574
completed.addWithPriority(async.add(listener1, 5), 10);
56-
completed.addWithPriority(async.add(listener2, 5, done), 10);
75+
completed.addWithPriority(async.add(listener2, 5), 10);
76+
77+
completed.dispatch();
78+
});
79+
80+
it("listeners_with_high_priority_should_be_called_first", done => {
81+
listenersToCall = 3;
82+
callback = done;
83+
84+
completed.addWithPriority(async.add(listener2, 5), 0);
85+
completed.addWithPriority(async.add(listener1, 5), 5);
86+
completed.addWithPriority(async.add(listener0, 5), 10);
5787

5888
completed.dispatch();
5989
});

0 commit comments

Comments
 (0)