Skip to content

Commit

Permalink
test(delayWhen): support Promises in delayWhen
Browse files Browse the repository at this point in the history
  • Loading branch information
jakovljevic-mladen committed Sep 12, 2022
1 parent 0560d15 commit fad3e6a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
10 changes: 7 additions & 3 deletions spec-dtslint/operators/delayWhen-spec.ts
Expand Up @@ -10,20 +10,24 @@ it('should support an empty notifier', () => {
const o = of(1, 2, 3).pipe(delayWhen(() => NEVER)); // $ExpectType Observable<number>
});

it('should support a subscriptiondelayWhen parameter', () => {
it('should support a subscriptionDelay parameter', () => {
const o = of(1, 2, 3).pipe(delayWhen(() => of('a', 'b', 'c'), of(new Date()))); // $ExpectType Observable<number>
});

it('should enforce types', () => {
const o = of(1, 2, 3).pipe(delayWhen()); // $ExpectError
});

it('should enforce types of delayWhenDurationSelector', () => {
it('should enforce types of delayDurationSelector', () => {
const o = of(1, 2, 3).pipe(delayWhen(of('a', 'b', 'c'))); // $ExpectError
const p = of(1, 2, 3).pipe(delayWhen((value: string, index) => of('a', 'b', 'c'))); // $ExpectError
const q = of(1, 2, 3).pipe(delayWhen((value, index: string) => of('a', 'b', 'c'))); // $ExpectError
});

it('should enforce types of subscriptiondelayWhen', () => {
it('should enforce types of subscriptionDelay', () => {
const o = of(1, 2, 3).pipe(delayWhen(() => of('a', 'b', 'c'), 'a')); // $ExpectError
});

it('should support Promises', () => {
const o = of(1, 2, 3).pipe(delayWhen(() => Promise.resolve('a'))); // $ExpectType Observable<number>
});
40 changes: 39 additions & 1 deletion spec/operators/delayWhen-spec.ts
@@ -1,4 +1,4 @@
import { of, EMPTY } from 'rxjs';
import { of, EMPTY, interval, take } from 'rxjs';
import { delayWhen, tap } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
Expand Down Expand Up @@ -338,4 +338,42 @@ describe('delayWhen', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should delayWhen Promise resolves', (done) => {
const e1 = interval(10).pipe(take(5));
const expected = [0, 1, 2, 3, 4];

e1.pipe(delayWhen(() => Promise.resolve(42))).subscribe({
next: (x: number) => {
expect(x).to.equal(expected.shift());
},
error: () => {
done(new Error('should not be called'));
},
complete: () => {
expect(expected.length).to.equal(0);
done();
},
});
});

it('should raise error when Promise rejects', (done) => {
const e1 = interval(10).pipe(take(10));
const expected = [0, 1, 2];
const error = new Error('err');

e1.pipe(delayWhen((x) => (x === 3 ? Promise.reject(error) : Promise.resolve(42)))).subscribe({
next: (x: number) => {
expect(x).to.equal(expected.shift());
},
error: (err: any) => {
expect(err).to.be.an('error');
expect(expected.length).to.equal(0);
done();
},
complete: () => {
done(new Error('should not be called'));
},
});
});
});

0 comments on commit fad3e6a

Please sign in to comment.