##// END OF EJS Templates
tests, refactoring, fixes
tests, refactoring, fixes

File last commit:

r40:6559c5b81a19 di-typescript
r40:6559c5b81a19 di-typescript
Show More
ObservableTests.ts
73 lines | 1.9 KiB | video/mp2t | TypeScriptLexer
/ test / ts / ObservableTests.ts
cin
tests, refactoring, fixes
r40 import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource";
import * as tape from "tape";
import { TapeWriter, delay } from "./TestTraits";
import { Observable } from "@implab/core/Observable";
import { IObservable } from "@implab/core/interfaces";
cin
working on Observable tests
r24
cin
tests, refactoring, fixes
r40 const trace = TraceSource.get("ObservableTests");
cin
working on Observable tests
r24
cin
tests, refactoring, fixes
r40 tape("events sequence example", async t => {
cin
working on Observable tests
r24
cin
tests, refactoring, fixes
r40 let events: IObservable<number>;
cin
working on Observable tests
r24
cin
tests, refactoring, fixes
r40 const done = new Promise<void>(resolve => {
events = new Observable<number>(async (notify, fail, finish) => {
cin
working on Observable tests
r24 for (let i = 0; i < 10; i++) {
await delay(0);
notify(i);
}
cin
tests, refactoring, fixes
r40 finish();
cin
working on Observable tests
r24 resolve();
});
});
let count = 0;
cin
the documentation on observables is added...
r26 let complete = false;
events.on(x => count = count + x, null, () => complete = true);
cin
working on Observable tests
r24
cin
tests, refactoring, fixes
r40 const first = await events.next();
cin
working on Observable tests
r24
t.equals(first, 0, "the first event");
cin
the documentation on observables is added...
r26 t.false(complete, "the sequence is not complete");
cin
working on Observable tests
r24
await done;
t.equals(count, 45, "the summ of the evetns");
cin
the documentation on observables is added...
r26 t.true(complete, "the sequence is complete");
t.end();
});
cin
tests, refactoring, fixes
r40 tape("event sequence termination", async t => {
let events: IObservable<number>;
cin
the documentation on observables is added...
r26
cin
tests, refactoring, fixes
r40 const done = new Promise<void>(resolve => {
cin
the documentation on observables is added...
r26 events = new Observable<number>(async (notify, fail, complete) => {
await delay(0);
notify(1);
complete();
notify(2);
complete();
fail("Sequence terminated");
resolve();
});
});
let count = 0;
cin
tests, refactoring, fixes
r40 events.on(() => {}, e => count++, () => count++);
cin
the documentation on observables is added...
r26
cin
tests, refactoring, fixes
r40 const first = await events.next();
cin
the documentation on observables is added...
r26 t.equals(first, 1, "the first message");
try {
await events.next();
t.fail("shoud throw an exception");
cin
tests, refactoring, fixes
r40 } catch (e) {
cin
the documentation on observables is added...
r26 t.pass("the sequence is terminated");
}
await done;
t.equals(count, 1, "the sequence must be terminated once");
cin
working on Observable tests
r24
t.end();
cin
tests, refactoring, fixes
r40 });