Skip to content

Commit

Permalink
Merge pull request #10 from DuncanMcPherson/test-subscription-counter
Browse files Browse the repository at this point in the history
add test subscription counter
  • Loading branch information
DuncanMcPherson authored Jan 23, 2024
2 parents b1d5e3c + 21ce767 commit b382298
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ez-budgets/karma-test-utils",
"version": "0.1.6",
"version": "0.1.7",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
[![codecov](https://codecov.io/gh/DuncanMcPherson/karma-test-utils/graph/badge.svg?token=7ANvQV0UIg)](https://codecov.io/gh/DuncanMcPherson/karma-test-utils)
31 changes: 31 additions & 0 deletions spec/test-subscription-counter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TestSubscriptionCounter } from '../src';
import { BehaviorSubject } from "rxjs";

describe("TestSubscriptionCounter", () => {
let counter: TestSubscriptionCounter<number>;
let observable: BehaviorSubject<number>

beforeEach(() => {
observable = new BehaviorSubject<number>(0);
counter = new TestSubscriptionCounter<number>(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);
})
})
})
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export { IMockClassOptions } from "./autoMocker";
export {
readObservableErrorSynchronously,
readObservableSynchronouslyAfterAction,
readObservableSynchronously,
readObservableCompletionSynchronously
} from './read-observable-synchronously';
export { readObservableSynchronously, readObservableCompletionSynchronously, readObservableErrorSynchronously, readObservableSynchronouslyAfterAction } from './read-observable-synchronously';
export { TestSubscriptionCounter } from './test-subscription-counter';
39 changes: 39 additions & 0 deletions src/test-subscription-counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Observable, finalize, Subscriber } from 'rxjs';

export class TestSubscriptionCounter<T> {
private _lifetimeSubscriptionCount: number = 0;
private _activeSubscriptionCount: number = 0;

public readonly countedObservable$: Observable<T> = new Observable<T>((observer: Subscriber<T>) => {
this._lifetimeSubscriptionCount++;
this._activeSubscriptionCount++;

const subscription = this.observable$
.pipe(finalize(() => this._activeSubscriptionCount--))
.subscribe(observer);

return () => subscription.unsubscribe();
});

constructor(private readonly observable$: Observable<T>) {}

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;
}
}

0 comments on commit b382298

Please sign in to comment.