Skip to content

Commit

Permalink
feat: expose schedule() method (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
MillerSvt authored Jul 4, 2024
1 parent 37f7d54 commit f7725f7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ npm i jest-marbles@1 -D
In the test file:

```js
import {cold, hot, time} from 'jest-marbles';
import {cold, hot, time, schedule} from 'jest-marbles';
```

Inside the test:
Expand Down Expand Up @@ -164,6 +164,20 @@ Allows you to assert on certain side effects/conditions that should be satisfied
})
```

## schedule
Allows you to schedule task on specified frame
```js
it('should verify subject values', () => {
const source = new Subject();
const expected = cold('ab');

schedule(() => source.next('a'), 1);
schedule(() => source.next('b'), 2);

expect(source).toBeObservable(expected);
});
```




5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ColdObservable } from './src/rxjs/cold-observable';
import { HotObservable } from './src/rxjs/hot-observable';
import { Scheduler } from './src/rxjs/scheduler';
import { stripAlignmentChars } from './src/rxjs/strip-alignment-chars';
import { Subscription } from 'rxjs';

export type ObservableWithSubscriptions = ColdObservable | HotObservable;

Expand Down Expand Up @@ -46,6 +47,10 @@ export function time(marbles: string): number {
return Scheduler.get().createTime(stripAlignmentChars(marbles));
}

export function schedule(work: () => void, delay: number): Subscription {
return Scheduler.get().schedule(work, delay);
}

const dummyResult = {
message: () => '',
pass: true,
Expand Down
14 changes: 12 additions & 2 deletions spec/to-be-observable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { delay, merge, timer } from 'rxjs';
import { delay, merge, Subject, timer } from 'rxjs';
import { concat, mapTo } from 'rxjs/operators';
import { cold, hot, Scheduler, time } from '../index';
import { cold, hot, schedule, Scheduler, time } from '../index';

describe('toBeObservable matcher test', () => {
it('Should concatenate two cold observables into single cold observable', () => {
Expand Down Expand Up @@ -84,6 +84,16 @@ describe('toBeObservable matcher test', () => {
);
});

it('Should work with schedules', () => {
const source = new Subject<string>();

schedule(() => source.next('a'), 1);
schedule(() => source.next('b'), 2);
const expected = cold('ab');

expect(source).toBeObservable(expected);
});

it('Should work with delays', () => {
const source = cold('a');
const expected = cold('--a');
Expand Down

0 comments on commit f7725f7

Please sign in to comment.