Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Last active April 11, 2020 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryuheechul/65e4b573167d6b93158597161a08c777 to your computer and use it in GitHub Desktop.
Save ryuheechul/65e4b573167d6b93158597161a08c777 to your computer and use it in GitHub Desktop.
/* After learning how to do testing for rxjs6 from here, https://medium.com/@kevinkreuzer/marble-testing-with-rxjs-testing-utils-3ae36ac3346a.
* I'm saving/sharing this gist to remember and understand again quickly in the future.
* I've altered some parts in the code to work with Jest and Javascript and CommonJS.
* In the post originally it was for Typescript and ES modules and Other(?) testing framework.
*/
const { TestScheduler } = require("rxjs/testing");
const { Observable } = require("rxjs");
const { filter, map } = require("rxjs/operators");
describe("Scheduler examples", () => {
// this is the simplified version of the test from the post
test("rxjs test simple", () => {
const scheduler = new TestScheduler((actual, expected) =>
// `expect` and `toEqual` is from [jest](https://jestjs.io/)
// but you can use any other testing library instead
expect(actual).toEqual(expected)
);
// basically doing nothing
const identity = (source$) => {
return source$.pipe(
map((n) => n)
);
};
scheduler.run(({ cold, expectObservable }) => {
const values = { a: 1, b: 2 };
const source$ = cold("a-b|", values);
const expectedMarble = "a-b|";
const expectedValues = { a: 1, b: 2 };
const result$ = identity(source$);
expectObservable(result$).toBe(expectedMarble, expectedValues);
});
});
// https://medium.com/@kevinkreuzer/marble-testing-with-rxjs-testing-utils-3ae36ac3346a
test("rxjs test from the post above", () => {
const scheduler = new TestScheduler((actual, expected) =>
expect(actual).toEqual(expected)
);
const evenTimesTen = (source$) => {
return source$.pipe(
filter((n) => n % 2 === 0),
map((n) => n * 10)
);
};
describe("should filter out odd numbers and multiply them by ten", () => {
scheduler.run(({ cold, expectObservable }) => {
const values = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, };
// I've altered this chunk from the original post for it to look slighly easier to grasp
const source$ = cold("a-b-c-d-e-f-g-h-i-j|", values);
const expectedMarble = "--b---d---f---h---j|";
const expectedValues = { b: 20, d: 40, f: 60, h: 80, j: 100 };
const result$ = evenTimesTen(source$);
expectObservable(result$).toBe(expectedMarble, expectedValues);
});
});
});
});
@ryuheechul
Copy link
Author

When I "copied" the code, I've used tesseract which is an OCR CLI, since the code in the post was provided as an image format not a text format.

It was first time using an OCR tool as an CLI for me and tesseract worked pretty well 👍🏼 even though it wasn't perfect (but almost).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment