From 839613e47632fbe117ede74befe636d4b66c899e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mladen=20Jakovljevi=C4=87?= Date: Fri, 9 Sep 2022 22:48:08 +0200 Subject: [PATCH] test(delayWhen): support Promises in delayWhen --- spec-dtslint/operators/delayWhen-spec.ts | 10 ++++-- spec/operators/delayWhen-spec.ts | 41 +++++++++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/spec-dtslint/operators/delayWhen-spec.ts b/spec-dtslint/operators/delayWhen-spec.ts index 2924fd36072..b03de4d103d 100644 --- a/spec-dtslint/operators/delayWhen-spec.ts +++ b/spec-dtslint/operators/delayWhen-spec.ts @@ -10,7 +10,7 @@ it('should support an empty notifier', () => { const o = of(1, 2, 3).pipe(delayWhen(() => NEVER)); // $ExpectType Observable }); -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 }); @@ -18,12 +18,16 @@ 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 +}); diff --git a/spec/operators/delayWhen-spec.ts b/spec/operators/delayWhen-spec.ts index 711e6b1e772..7e5db970d2b 100644 --- a/spec/operators/delayWhen-spec.ts +++ b/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'; @@ -338,4 +338,43 @@ 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: () => { + console.log('??'); + done(new Error('should not be called')); + }, + }); + }); });