Skip to content
Merged
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
36 changes: 36 additions & 0 deletions libs/ngxtension/connect/src/connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,42 @@ describe(connect.name, () => {
});
});

describe('reducer returning a falsy value', () => {
it('should use the reducer result instead of the raw emission when it is a falsy primitive', () => {
const isActive = signal(true);

TestBed.runInInjectionContext(() => {
connect(isActive, of('toggle'), () => false);
});

expect(isActive()).toBe(false);
});

it('should use a reducer result of 0 instead of falling back to the raw emission', () => {
const count = signal(5);

TestBed.runInInjectionContext(() => {
// reducer computes a delta which legitimately evaluates to 0
connect(count, of(5), (prev, next) => next - prev);
});

expect(count()).toBe(0);
});

it('should not silently replace object state with the raw emission when a reducer guard returns a falsy value', () => {
const state = signal({ age: 30 });

TestBed.runInInjectionContext(() => {
// reducer intentionally returns null to skip the update
connect(state, of({ age: 20 }), (prev, next) =>
next.age > prev.age ? { age: next.age } : (null as any),
);
});

expect(state()).toEqual({ age: 30 });
});
});

describe('connects an observable to a signal not in injection context using injector', () => {
@Component({ standalone: true, template: '' })
class TestComponent implements OnInit {
Expand Down
4 changes: 2 additions & 2 deletions libs/ngxtension/connect/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function connect(signal: WritableSignal<unknown>, ...args: any[]) {
const update = () => {
signal.update((prev) => {
if (!isObject(prev)) {
return reducer?.(prev, x) || x;
return reducer ? reducer(prev, x) : x;
}

if (!isObject(x)) {
Expand All @@ -131,7 +131,7 @@ export function connect(signal: WritableSignal<unknown>, ...args: any[]) {
: reducedValue;
}

const curr = reducer?.(prev, x) || x;
const curr = reducer ? reducer(prev, x) : x;

if (isDate(curr)) {
return new Date(curr);
Expand Down
Loading