From 6fcbcf8e7795bfd7809a2730ef3611eafb12a4b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mladen=20Jakovljevi=C4=87?= Date: Thu, 14 Jan 2021 12:30:02 +0100 Subject: [PATCH] chore: update isEmpty tests to run mode --- spec/operators/isEmpty-spec.ts | 120 +++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 49 deletions(-) diff --git a/spec/operators/isEmpty-spec.ts b/spec/operators/isEmpty-spec.ts index 88d864551a..151992ec05 100644 --- a/spec/operators/isEmpty-spec.ts +++ b/spec/operators/isEmpty-spec.ts @@ -1,77 +1,99 @@ +/** @prettier */ import { isEmpty, mergeMap } from 'rxjs/operators'; -import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { TestScheduler } from 'rxjs/testing'; import { of } from 'rxjs'; +import { observableMatcher } from '../helpers/observableMatcher'; /** @test {isEmpty} */ -describe('isEmpty operator', () => { +describe('isEmpty', () => { + let testScheduler: TestScheduler; + + beforeEach(() => { + testScheduler = new TestScheduler(observableMatcher); + }); + it('should return true if source is empty', () => { - const source = hot('-----|'); - const subs = '^ !'; - const expected = '-----(T|)'; + testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => { + const e1 = hot(' -----| '); + const e1subs = ' ^----! '; + const expected = '-----(T|)'; - expectObservable((source).pipe(isEmpty())).toBe(expected, { T: true }); - expectSubscriptions(source.subscriptions).toBe(subs); + expectObservable(e1.pipe(isEmpty())).toBe(expected, { T: true }); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + }); }); it('should return false if source emits element', () => { - const source = hot('--a--^--b--|'); - const subs = '^ !'; - const expected = '---(F|)'; + testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => { + const e1 = hot('--a--^--b--|'); + const e1subs = ' ^--! '; + const expected = ' ---(F|)'; - expectObservable((source).pipe(isEmpty())).toBe(expected, { F: false }); - expectSubscriptions(source.subscriptions).toBe(subs); + expectObservable(e1.pipe(isEmpty())).toBe(expected, { F: false }); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + }); }); - it('should raise error if source raise error', () => { - const source = hot('--#'); - const subs = '^ !'; - const expected = '--#'; + it('should raise error if source raises error', () => { + testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => { + const e1 = hot(' --#'); + const e1subs = ' ^-!'; + const expected = '--#'; - expectObservable((source).pipe(isEmpty())).toBe(expected); - expectSubscriptions(source.subscriptions).toBe(subs); + expectObservable(e1.pipe(isEmpty())).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + }); }); - it('should not completes if source never emits', () => { - const source = cold('-'); - const subs = '^'; - const expected = '-'; + it('should not complete if source never emits', () => { + testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => { + const e1 = cold(' -'); + const e1subs = ' ^'; + const expected = '-'; - expectObservable((source).pipe(isEmpty())).toBe(expected); - expectSubscriptions(source.subscriptions).toBe(subs); + expectObservable(e1.pipe(isEmpty())).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + }); }); - it('should return true if source is Observable.empty()', () => { - const source = cold('|'); - const subs = '(^!)'; - const expected = '(T|)'; + it('should return true if source is empty', () => { + testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => { + const e1 = cold(' | '); + const e1subs = ' (^!)'; + const expected = '(T|)'; - expectObservable((source).pipe(isEmpty())).toBe(expected, { T: true }); - expectSubscriptions(source.subscriptions).toBe(subs); + expectObservable(e1.pipe(isEmpty())).toBe(expected, { T: true }); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + }); }); it('should allow unsubscribing explicitly and early', () => { - const source = cold('-----------a--b--|'); - const unsub = ' ! '; - const subs = '^ ! '; - const expected = '------- '; + testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => { + const e1 = cold(' -----------a--b--|'); + const e1subs = ' ^-----! '; + const expected = '------- '; + const unsub = ' ------! '; - expectObservable((source).pipe(isEmpty()), unsub).toBe(expected); - expectSubscriptions(source.subscriptions).toBe(subs); + expectObservable(e1.pipe(isEmpty()), unsub).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + }); }); it('should not break unsubscription chains when result is unsubscribed explicitly', () => { - const source = cold('-----------a--b--|'); - const subs = '^ ! '; - const expected = '------- '; - const unsub = ' ! '; - - const result = (source).pipe( - mergeMap((x: string) => of(x)), - isEmpty(), - mergeMap((x: string) => of(x)) - ); - - expectObservable(result, unsub).toBe(expected); - expectSubscriptions(source.subscriptions).toBe(subs); + testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => { + const e1 = cold(' -----------a--b--|'); + const e1subs = ' ^-----! '; + const expected = '------- '; + const unsub = ' ------! '; + + const result = e1.pipe( + mergeMap((x) => of(x)), + isEmpty(), + mergeMap((x) => of(x)) + ); + + expectObservable(result, unsub).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + }); }); });