Skip to content

Commit

Permalink
feat(context): Implement bind for schedulers.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Nov 30, 2017
1 parent 19d870b commit 3a7963c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
15 changes: 15 additions & 0 deletions fixtures/mocha/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { cases, marbles } from "../../dist/mocha";

import "rxjs/add/operator/delay";
import "rxjs/add/operator/map";

describe("rxjs-marbles", () => {
Expand Down Expand Up @@ -158,6 +159,20 @@ describe("rxjs-marbles", () => {
m.expect(source).toHaveSubscriptions(subs);
}));

it("should support binding non-test schedulers", marbles((m) => {

m.bind();

const source = m.hot("--^-a-b-c-|");
const subs = "^--------!";
const expected = "---a-b-c-|";

const destination = source.delay(m.time("-|"));

m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));

cases("should support cases", (m, c) => {

const values = {
Expand Down
10 changes: 10 additions & 0 deletions rollup-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ export default {
banner: "/*MIT license https://github.com/cartant/rxjs-marbles/blob/master/LICENSE*/",
external: [
"rxjs/Observable",
"rxjs/Scheduler",
"rxjs/scheduler/animationFrame",
"rxjs/scheduler/asap",
"rxjs/scheduler/async",
"rxjs/scheduler/queue",
"rxjs/testing/ColdObservable",
"rxjs/testing/HotObservable",
"rxjs/testing/TestScheduler"
],
globals: {
"rxjs/Observable": "Rx",
"rxjs/Scheduler": "Rx",
"rxjs/scheduler/animationFrame": "Rx.Scheduler",
"rxjs/scheduler/asap": "Rx.Scheduler",
"rxjs/scheduler/async": "Rx.Scheduler",
"rxjs/scheduler/queue": "Rx.Scheduler",
"rxjs/testing/TestScheduler": "Rx"
},
input: "dist/index.js",
Expand Down
49 changes: 49 additions & 0 deletions source/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/

import { Observable } from "rxjs/Observable";
import { IScheduler } from "rxjs/Scheduler";
import { animationFrame } from "rxjs/scheduler/animationFrame";
import { asap } from "rxjs/scheduler/asap";
import { async } from "rxjs/scheduler/async";
import { queue } from "rxjs/scheduler/queue";
import { ColdObservable } from "rxjs/testing/ColdObservable";
import { HotObservable } from "rxjs/testing/HotObservable";
import { TestScheduler } from "rxjs/testing/TestScheduler";
Expand All @@ -18,8 +23,32 @@ export class Context {
public autoFlush = true;
public configure = configure;

private bindings_: {
instance: IScheduler,
now?: IScheduler["now"],
schedule?: IScheduler["schedule"]
}[] = [];

constructor(public readonly scheduler: TestScheduler) {}

bind(...schedulers: IScheduler[]): void {

if (this.bindings_.length !== 0) {
throw new Error("Schedulers already bound.");
}
if (schedulers.length === 0) {
schedulers = [animationFrame, asap, async, queue];
}

this.bindings_ = schedulers.map(instance => {
const now = instance.hasOwnProperty("now") ? instance.now : undefined;
instance.now = () => this.scheduler.now();
const schedule = instance.hasOwnProperty("schedule") ? instance.schedule : undefined;
instance.schedule = (work, delay, state) => this.scheduler.schedule(work, delay, state);
return { instance, now, schedule };
});
}

cold<T = any>(marbles: string, values?: { [key: string]: T }, error?: any): ColdObservable<T> {

const { scheduler } = this;
Expand Down Expand Up @@ -89,6 +118,26 @@ export class Context {
return observable;
}

teardown(): void {

if (this.autoFlush) {
this.scheduler.flush();
}

this.bindings_.forEach(({ instance, now, schedule }) => {
if (now) {
instance.now = now;
} else {
delete instance.now;
}
if (schedule) {
instance.schedule = schedule;
} else {
delete instance.schedule;
}
});
}

time(marbles: string): number {

const { scheduler } = this;
Expand Down
8 changes: 2 additions & 6 deletions source/marbles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ export function marbles(func: (context: Context, ...rest: any[]) => any): (...re
const scheduler = new TestScheduler((a, b) => observableMatcher(a, b, get("assert"), get("assertDeepEqual")));
const context = new Context(scheduler);
func(context, first, ...rest);
if (context.autoFlush) {
scheduler.flush();
}
context.teardown();
};
}
return (...rest: any[]) => {

const scheduler = new TestScheduler((a, b) => observableMatcher(a, b, get("assert"), get("assertDeepEqual")));
const context = new Context(scheduler);
func(context, ...rest);
if (context.autoFlush) {
scheduler.flush();
}
context.teardown();
};
}

0 comments on commit 3a7963c

Please sign in to comment.