Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions libs/ngxtension/signal-slice/src/signal-slice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DestroyRef } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { TestBed, fakeAsync, flush, tick } from '@angular/core/testing';
import { Observable, Subject, of, timer } from 'rxjs';
Expand Down Expand Up @@ -245,6 +246,32 @@ describe(signalSlice.name, () => {
});
});

it('should not register a new permanent subscription teardown on every call with a plain value', () => {
TestBed.runInInjectionContext(() => {
const destroyRef = TestBed.inject(DestroyRef);
const onDestroySpy = jest.spyOn(destroyRef, 'onDestroy');

const state = signalSlice({
initialState,
actionSources: {
increaseAge: (state, $: Observable<number>) =>
$.pipe(map((amount) => ({ age: state().age + amount }))),
},
});

// one permanent subscription is made at slice-creation time by
// the internal `connect(state, sharedObservable)` call
const baseline = onDestroySpy.mock.calls.length;
expect(baseline).toBeGreaterThan(0);

state.increaseAge(1);
state.increaseAge(1);
state.increaseAge(1);

expect(onDestroySpy.mock.calls.length).toEqual(baseline);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is a good practice to restore Jest spies at the end of the test to prevent potential mock pollution or side effects in other tests within the same suite.

				expect(onDestroySpy.mock.calls.length).toEqual(baseline);
				onDestroySpy.mockRestore();

});
});

it('should resolve the updated state as a promise after reducer is invoked', (done) => {
TestBed.runInInjectionContext(() => {
const state = signalSlice({
Expand Down
8 changes: 0 additions & 8 deletions libs/ngxtension/signal-slice/src/signal-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ export function signalSlice<
destroyRef,
subject,
subs,
sharedObservable,
);
Comment on lines 209 to 212

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since sharedObservable is no longer passed to addReducerProperties, it is only subscribed to once by connect(state, sharedObservable) at line 204. This makes the share() operator on line 203 redundant. You can clean this up by removing const sharedObservable = observable.pipe(share()); and passing observable directly to connect(state, observable).

}
}
Expand Down Expand Up @@ -293,7 +292,6 @@ function addReducerProperties(
destroyRef: DestroyRef,
subject: Subject<unknown>,
subs: Subject<unknown>[],
observableFromActionSource?: Observable<any>,
) {
const version = createNotifier();
Object.defineProperties(readonlyState, {
Expand All @@ -319,12 +317,6 @@ function addReducerProperties(
});
}

if (observableFromActionSource) {
observableFromActionSource
.pipe(takeUntilDestroyed(destroyRef))
.subscribe();
}

return new Promise((res) => {
state$.pipe(take(1)).subscribe((val) => {
res(val);
Expand Down
Loading