Skip to content

Commit

Permalink
feat(observe): Add an observe wrapper.
Browse files Browse the repository at this point in the history
To bind the callbacks in Jasmine, Jest and Mocha for non-marble tests.
  • Loading branch information
cartant committed May 19, 2018
1 parent f9d533f commit 9c38cce
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 23 deletions.
14 changes: 11 additions & 3 deletions fixtures/jasmine/failing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { map } from "rxjs/operators";
import { marbles } from "../../dist/jasmine";
import { of } from "rxjs";
import { map, tap } from "rxjs/operators";
import { marbles, observe } from "../../dist/jasmine";

if (process.env.FAILING !== "0") {

describe("rxjs-marbles", () => {
describe("marbles", () => {

it("should fail", marbles((m) => {

Expand All @@ -29,4 +30,11 @@ if (process.env.FAILING !== "0") {
m.expect(source).toHaveSubscriptions(subs);
}));
});

describe("observe", () => {

it("should fail", observe(() => of("fail").pipe(
tap(value => expect(value).not.toEqual("fail"))
)));
});
}
14 changes: 11 additions & 3 deletions fixtures/jasmine/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
*/
/*tslint:disable:object-literal-sort-keys*/

import { map } from "rxjs/operators";
import { cases, marbles } from "../../dist/jasmine";
import { of } from "rxjs";
import { map, tap } from "rxjs/operators";
import { cases, marbles, observe } from "../../dist/jasmine";

interface TestContext {
myVariable: number;
}

describe("rxjs-marbles", () => {
describe("marbles", () => {

beforeEach(function(this: TestContext): void {
this.myVariable = 57;
Expand Down Expand Up @@ -77,3 +78,10 @@ describe("rxjs-marbles", () => {
"unused": {}
});
});

describe("observe", () => {

it("should support observe", observe(() => of("pass").pipe(
tap(value => expect(value).toEqual("pass"))
)));
});
11 changes: 8 additions & 3 deletions fixtures/jest/failing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { map } from "rxjs/operators";
import { marbles } from "../../dist/jest";
import { of } from "rxjs";
import { map, tap } from "rxjs/operators";
import { marbles, observe } from "../../dist/jest";

if (process.env.FAILING !== "0") {

test("it should fail", marbles((m) => {
test("it should fail with marbles", marbles((m) => {

const values = {
a: 1,
Expand All @@ -27,6 +28,10 @@ if (process.env.FAILING !== "0") {
m.expect(source).toHaveSubscriptions(subs);
}));

test("it should fail with observe", observe(() => of("fail").pipe(
tap(value => expect(value).not.toEqual("fail"))
)));

} else {

test("it should pass", () => {});
Expand Down
18 changes: 11 additions & 7 deletions fixtures/jest/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*/
/*tslint:disable:object-literal-sort-keys*/

import { map } from "rxjs/operators";
import { cases, marbles } from "../../dist/jest";
import { of } from "rxjs";
import { map, tap } from "rxjs/operators";
import { cases, marbles, observe } from "../../dist/jest";


test('It should handle white space in marble diagrams correctly', marbles((m) => {
test("it should handle white space in marble diagrams correctly", marbles((m) => {

const values = {
a: 1,
Expand All @@ -17,13 +17,13 @@ test('It should handle white space in marble diagrams correctly', marbles((m) =>
d: 4
};

const source = m.cold(' --a-b-c-|', values)
const source = m.cold(" --a-b-c-|", values);
const expected = m.cold("--b-c-d-|", values);

const destination = source.pipe(map((value) => value + 1));

m.expect(destination).toBeObservable(expected);
}))
}));

test("it should support marble tests", marbles((m) => {

Expand Down Expand Up @@ -71,5 +71,9 @@ cases("should support cases", (m, c) => {

test("it should support promises", marbles((m) => {

return Promise.resolve().then(() => expect(typeof m).toEqual("object"));
return Promise.resolve("pass").then((value) => expect(value).toEqual("pass"));
}));

test("it should support observe", observe(() => of("pass").pipe(
tap(value => expect(value).toEqual("pass"))
)));
15 changes: 12 additions & 3 deletions fixtures/mocha/failing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { map } from "rxjs/operators";
import { marbles } from "../../dist/mocha";
import { expect } from "chai";
import { of } from "rxjs";
import { map, tap } from "rxjs/operators";
import { marbles, observe } from "../../dist/mocha";

if (process.env.FAILING !== "0") {

describe("rxjs-marbles", () => {
describe("marbles", () => {

it("should fail", marbles((m) => {

Expand All @@ -29,4 +31,11 @@ if (process.env.FAILING !== "0") {
m.expect(source).toHaveSubscriptions(subs);
}));
});

describe("observe", () => {

it("should fail", observe(() => of("fail").pipe(
tap(value => expect(value).to.not.equal("fail"))
)));
});
}
16 changes: 12 additions & 4 deletions fixtures/mocha/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
/*tslint:disable:object-literal-sort-keys*/

import { expect } from "chai";
import { delay, map } from "rxjs/operators";
import { configure } from "../../dist/mocha";
import { of } from "rxjs";
import { delay, map, tap } from "rxjs/operators";
import { configure, observe } from "../../dist/mocha";

describe("rxjs-marbles", () => {
describe("marbles", () => {

describe("deprecated", () => {

Expand Down Expand Up @@ -225,7 +226,7 @@ describe("rxjs-marbles", () => {

it("should support promises", marbles((m) => {

return Promise.resolve().then(() => expect(m).to.be.an("object"));
return Promise.resolve("pass").then((value) => expect(value).to.equal("pass"));
}));

it("should support reframing", marbles((m) => {
Expand Down Expand Up @@ -482,3 +483,10 @@ describe("rxjs-marbles", () => {
}));
});
});

describe("observe", () => {

it("should support observe", observe(() => of("pass").pipe(
tap(value => expect(value).to.equal("pass"))
)));
});
1 change: 1 addition & 0 deletions source/jasmine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from "../configuration";
export * from "../context";
export * from "../expect";
export { MarblesFunction } from "../marbles";
export * from "./observe";

declare const describe: Function;
declare const fit: Function;
Expand Down
15 changes: 15 additions & 0 deletions source/jasmine/observe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { Observable } from "rxjs";

export interface DoneFunction {
(): void;
fail: (error: any) => void;
}

export function observe<T>(observableTest: () => Observable<T>): (done: DoneFunction) => void {
return (done: DoneFunction) => observableTest().subscribe(undefined, done.fail, done);
}
1 change: 1 addition & 0 deletions source/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from "../configuration";
export * from "../context";
export * from "../expect";
export { MarblesFunction } from "../marbles";
export * from "./observe";

declare const describe: Function;
declare const expect: Function;
Expand Down
15 changes: 15 additions & 0 deletions source/jest/observe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { Observable } from "rxjs";

export interface DoneFunction {
(): void;
fail: (error: any) => void;
}

export function observe<T>(observableTest: () => Observable<T>): (done: DoneFunction) => void {
return (done: DoneFunction) => observableTest().subscribe(undefined, done.fail, done);
}
1 change: 1 addition & 0 deletions source/mocha/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from "../configuration";
export * from "../context";
export * from "../expect";
export { MarblesFunction } from "../marbles";
export * from "./observe";

declare const describe: Function;
declare const it: any;
Expand Down
12 changes: 12 additions & 0 deletions source/mocha/observe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { Observable } from "rxjs";

export type DoneFunction = (error?: Error) => void;

export function observe<T>(observableTest: () => Observable<T>): (done: DoneFunction) => void {
return (done: DoneFunction) => observableTest().subscribe(undefined, done, done);
}

0 comments on commit 9c38cce

Please sign in to comment.