Writing your first marble test

Let's create a new file marble-testing.spec.ts. It should look like the following:

// marble-testingMarbleTestingsrcappmarble-testing.spec.ts

import { cold } from "jasmine-marbles";
import "rxjs/add/operator/map";

describe("marble tests", () => {
it("map - should increase by 1", () => {
const one$ = cold("x-x|", { x: 1 });
expect(one$.map(x => x + 1)).toBeObservable(cold("x-x|", { x: 2 }));
});
});

A lot of interesting things are happening here. We import the function cold() from the NPM library marble-testing. Thereafter we set up a test suite by calling describe(), followed by a test specification, by calling it(). Then we call our cold() function and provide it a string. Let's have a close look at that function call:

const stream$ = cold("x-x|", { x: 1 });

The above code set up a stream that expects to values to be emitted followed by the stream ending. How do we know that? It's time to explain what x-x| means. x is just any value, the hyphen - means time has passed. The pipe | means our stream has ended. The second argument in the cold function is a mapping object that tells us what the x means. In this case, it has come to mean the value 1. 

Moving on, let's have a look at the next line:

expect(stream$.map(x => x + 1)).toBeObservable(cold("x-x|", { x: 2 }));

The preceding code applies the operator .map() and increased the value with one for each value emitted in the stream. Thereafter, we call the .toBeObservable() helper method and verify it against an expected condition, 

cold("x-x|", { x: 2 })

The previous condition states that we expect the stream to should emit two values, but that the values should now have the number 2. This makes sense, as our map() function performs just that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset