From 21ce767b899c2a686af84d40f5e5b1561a2de309 Mon Sep 17 00:00:00 2001 From: Duncan McPherson Date: Mon, 22 Jan 2024 20:24:32 -0700 Subject: [PATCH] add test subscription counter --- package.json | 2 +- readme.md | 2 +- spec/test-subscription-counter.spec.ts | 31 ++++++++++++++++++++ src/index.ts | 8 ++---- src/test-subscription-counter.ts | 39 ++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 spec/test-subscription-counter.spec.ts create mode 100644 src/test-subscription-counter.ts diff --git a/package.json b/package.json index bbc7b20..b78f01a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ez-budgets/karma-test-utils", - "version": "0.1.6", + "version": "0.1.7", "description": "", "main": "dist/index.js", "files": [ diff --git a/readme.md b/readme.md index f051721..1095199 100644 --- a/readme.md +++ b/readme.md @@ -2,4 +2,4 @@ ![Tests](https://github.com/duncanmcpherson/karma-test-utils/actions/workflows/test.yml/badge.svg) ![Build](https://github.com/duncanmcpherson/karma-test-utils/actions/workflows/merge.yml/badge.svg) -[![codecov](https://codecov.io/gh/DuncanMcPherson/karma-test-utils/graph/badge.svg?token=B7FL6TPHO8)](https://codecov.io/gh/DuncanMcPherson/karma-test-utils) \ No newline at end of file +[![codecov](https://codecov.io/gh/DuncanMcPherson/karma-test-utils/graph/badge.svg?token=7ANvQV0UIg)](https://codecov.io/gh/DuncanMcPherson/karma-test-utils) \ No newline at end of file diff --git a/spec/test-subscription-counter.spec.ts b/spec/test-subscription-counter.spec.ts new file mode 100644 index 0000000..1681b6c --- /dev/null +++ b/spec/test-subscription-counter.spec.ts @@ -0,0 +1,31 @@ +import { TestSubscriptionCounter } from '../src'; +import { BehaviorSubject } from "rxjs"; + +describe("TestSubscriptionCounter", () => { + let counter: TestSubscriptionCounter; + let observable: BehaviorSubject + + beforeEach(() => { + observable = new BehaviorSubject(0); + counter = new TestSubscriptionCounter(observable); + }); + + describe("countedObservable$", () => { + it("should return a counted observable", () => { + expect(counter.countedObservable$).toBeTruthy(); + const tempNumber = Math.floor(Math.random() * 10); + observable.next(tempNumber); + const subscription = counter.countedObservable$.subscribe((value) => { + expect(value).toEqual(tempNumber) + }); + expect(counter.activeSubscriptionCount).toEqual(1); + expect(counter.lifetimeSubscriptionCount).toEqual(1); + expect(counter.allSubscriptionsFinalized).toEqual(false); + expect(counter.hadSubscribers).toEqual(true); + expect(counter.hasSubscribers).toEqual(true); + subscription.unsubscribe(); + expect(counter.activeSubscriptionCount).toEqual(0); + expect(counter.allSubscriptionsFinalized).toEqual(true); + }) + }) +}) \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 0beda6d..d04d847 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,3 @@ export { IMockClassOptions } from "./autoMocker"; -export { - readObservableErrorSynchronously, - readObservableSynchronouslyAfterAction, - readObservableSynchronously, - readObservableCompletionSynchronously -} from './read-observable-synchronously'; \ No newline at end of file +export { readObservableSynchronously, readObservableCompletionSynchronously, readObservableErrorSynchronously, readObservableSynchronouslyAfterAction } from './read-observable-synchronously'; +export { TestSubscriptionCounter } from './test-subscription-counter'; \ No newline at end of file diff --git a/src/test-subscription-counter.ts b/src/test-subscription-counter.ts new file mode 100644 index 0000000..5be240b --- /dev/null +++ b/src/test-subscription-counter.ts @@ -0,0 +1,39 @@ +import { Observable, finalize, Subscriber } from 'rxjs'; + +export class TestSubscriptionCounter { + private _lifetimeSubscriptionCount: number = 0; + private _activeSubscriptionCount: number = 0; + + public readonly countedObservable$: Observable = new Observable((observer: Subscriber) => { + this._lifetimeSubscriptionCount++; + this._activeSubscriptionCount++; + + const subscription = this.observable$ + .pipe(finalize(() => this._activeSubscriptionCount--)) + .subscribe(observer); + + return () => subscription.unsubscribe(); + }); + + constructor(private readonly observable$: Observable) {} + + public get lifetimeSubscriptionCount(): number { + return this._lifetimeSubscriptionCount; + } + + public get activeSubscriptionCount(): number { + return this._activeSubscriptionCount; + } + + public get hadSubscribers(): boolean { + return this.lifetimeSubscriptionCount > 0; + } + + public get hasSubscribers(): boolean { + return this.activeSubscriptionCount > 0; + } + + public get allSubscriptionsFinalized(): boolean { + return this.activeSubscriptionCount === 0; + } +} \ No newline at end of file