Skip to content

Commit

Permalink
feat(signals): prevent overriding of SignalStore members (#4424)
Browse files Browse the repository at this point in the history
Closes #4144
  • Loading branch information
markostanimirovic authored Jul 16, 2024
1 parent 92a68bc commit e3fbd56
Show file tree
Hide file tree
Showing 25 changed files with 527 additions and 620 deletions.
50 changes: 0 additions & 50 deletions modules/signals/spec/get-state.spec.ts

This file was deleted.

94 changes: 0 additions & 94 deletions modules/signals/spec/patch-state.spec.ts

This file was deleted.

20 changes: 10 additions & 10 deletions modules/signals/spec/signal-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as angular from '@angular/core';
import { effect, isSignal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { patchState, signalState } from '../src';
import { STATE_SIGNAL } from '../src/state-signal';
import { STATE_SOURCE } from '../src/state-source';

describe('signalState', () => {
const initialState = {
Expand All @@ -15,12 +15,12 @@ describe('signalState', () => {
ngrx: 'signals',
};

it('has state signal', () => {
it('has state source', () => {
const state = signalState({});
const stateSignal = state[STATE_SIGNAL];
const stateSource = state[STATE_SOURCE];

expect(isSignal(stateSignal)).toBe(true);
expect(typeof stateSignal.update === 'function').toBe(true);
expect(isSignal(stateSource)).toBe(true);
expect(typeof stateSource.update === 'function').toBe(true);
});

it('creates signals for nested state slices', () => {
Expand Down Expand Up @@ -76,13 +76,13 @@ describe('signalState', () => {
expect((state.user.firstName as any).y).toBe(undefined);
});

it('does not modify STATE_SIGNAL', () => {
it('does not modify STATE_SOURCE', () => {
const state = signalState(initialState);

expect((state[STATE_SIGNAL] as any).user).toBe(undefined);
expect((state[STATE_SIGNAL] as any).foo).toBe(undefined);
expect((state[STATE_SIGNAL] as any).numbers).toBe(undefined);
expect((state[STATE_SIGNAL] as any).ngrx).toBe(undefined);
expect((state[STATE_SOURCE] as any).user).toBe(undefined);
expect((state[STATE_SOURCE] as any).foo).toBe(undefined);
expect((state[STATE_SOURCE] as any).numbers).toBe(undefined);
expect((state[STATE_SOURCE] as any).ngrx).toBe(undefined);
});

it('overrides Function properties if state keys have the same name', () => {
Expand Down
11 changes: 5 additions & 6 deletions modules/signals/spec/signal-store-feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
withMethods,
withState,
} from '../src';
import { STATE_SIGNAL } from '../src/state-signal';
import { STATE_SOURCE } from '../src/state-source';

describe('signalStoreFeature', () => {
function withCustomFeature1() {
Expand All @@ -24,7 +24,6 @@ describe('signalStoreFeature', () => {
return signalStoreFeature(
withCustomFeature1(),
withMethods(({ foo, baz }) => ({
bar: (value: number) => value,
m: () => foo() + baz() + 3,
}))
);
Expand All @@ -51,7 +50,7 @@ describe('signalStoreFeature', () => {

const store = new Store();

expect(store[STATE_SIGNAL]()).toEqual({ foo: 'foo' });
expect(store[STATE_SOURCE]()).toEqual({ foo: 'foo' });
expect(store.foo()).toBe('foo');
expect(store.bar()).toBe('foo1');
expect(store.baz()).toBe('foofoo12');
Expand All @@ -66,9 +65,9 @@ describe('signalStoreFeature', () => {

const store = new Store();

expect(store[STATE_SIGNAL]()).toEqual({ foo: 'foo' });
expect(store[STATE_SOURCE]()).toEqual({ foo: 'foo' });
expect(store.foo()).toBe('foo');
expect(store.bar(10)).toBe(10);
expect(store.bar()).toBe('foo1');
expect(store.m()).toBe('foofoofoo123');
expect(store.m1()).toBe('foo10');
});
Expand All @@ -82,7 +81,7 @@ describe('signalStoreFeature', () => {

const store = new Store();

expect(store[STATE_SIGNAL]()).toEqual({ foo: 'foo', foo1: 1, foo2: 2 });
expect(store[STATE_SOURCE]()).toEqual({ foo: 'foo', foo1: 1, foo2: 2 });
expect(store.foo()).toBe('foo');
expect(store.bar()).toBe('foo1');
expect(store.baz()).toBe('foofoo12');
Expand Down
Loading

0 comments on commit e3fbd56

Please sign in to comment.