Skip to content

Commit

Permalink
docs(test): Improve marble testing docs (#3991)
Browse files Browse the repository at this point in the history
* docs(test): Added  import statement for TestScheduler
* docs(test): Make the first marble testing example more complete.
  • Loading branch information
tanduong authored and benlesh committed Aug 6, 2018
1 parent 4ec4ff1 commit 37c5c09
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions doc/marble-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@ We can test our _asynchronous_ RxJS code _synchronously_ and deterministically b
> At this time the TestScheduler can only be used to test code that uses timers, like delay/debounceTime/etc (i.e. it uses AsyncScheduler with delays > 1). If the code consumes a Promise or does scheduling with AsapScheduler/AnimationFrameScheduler/etc it cannot be reliably tested with TestScheduler, but instead should be tested more traditionally. See the [Known Issues](#known-issues) section for more details.
```ts
const testScheduler = new TestScheduler((actual, expected) => {
// some how assert the two objects are equal
// e.g. with chai `expect(actual).deep.equal(expected)`
import { TestScheduler } from 'rxjs/testing';

const scheduler = new TestScheduler((actual, expected) => {
// asserting the two objects are equal
// e.g. using chai.
expect(actual).deep.equal(expected);
});

// This test will actually run *synchronously*
testScheduler.run(({ cold }) => {
const input = cold('-a-b-c--------|');
const output = input.pipe(
debounceTime(5)
);
const expected = ' ----------c---|';
expectObservable(output).toBe(expected);
it('generate the stream correctly', () => {
scheduler.run(helpers => {
const { cold, expectObservable, expectSubscriptions } = helpers;
const e1 = cold('-a--b--c---|');
const subs = '^----------!';
const expected = '-a-----c---|';

expectObservable(e1.pipe(throttleTime(3, scheduler))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
```

## API

The callback function you provide to `testScheduler.run(callback)` is called with an object that contains the helper functions you'll use to write your tests.
The callback function you provide to `testScheduler.run(callback)` is called with `helpers` object that contains functions you'll use to write your tests.

> When the code inside this callback is being executed, any operator that uses timers/AsyncScheduler (like delay, debounceTime, etc) will **automatically** use the TestScheduler instead, so that we have "virtual time". You do not need to pass the TestScheduler to them, like in the past.
Expand Down

0 comments on commit 37c5c09

Please sign in to comment.