|
1 |
| -import { inject, InjectionToken, isSignal, signal } from '@angular/core'; |
| 1 | +import { |
| 2 | + computed, |
| 3 | + inject, |
| 4 | + InjectionToken, |
| 5 | + isSignal, |
| 6 | + signal, |
| 7 | +} from '@angular/core'; |
2 | 8 | import { TestBed } from '@angular/core/testing';
|
3 | 9 | import {
|
4 | 10 | patchState,
|
@@ -145,6 +151,14 @@ describe('signalStore', () => {
|
145 | 151 |
|
146 | 152 | expect(store.foo()).toBe('foo');
|
147 | 153 | });
|
| 154 | + |
| 155 | + it('allows symbols as state keys', () => { |
| 156 | + const SECRET = Symbol('SECRET'); |
| 157 | + const Store = signalStore(withState({ [SECRET]: 'bar' })); |
| 158 | + const store = new Store(); |
| 159 | + |
| 160 | + expect(store[SECRET]()).toBe('bar'); |
| 161 | + }); |
148 | 162 | });
|
149 | 163 |
|
150 | 164 | describe('withProps', () => {
|
@@ -183,6 +197,26 @@ describe('signalStore', () => {
|
183 | 197 |
|
184 | 198 | expect(store.foo).toBe('bar');
|
185 | 199 | });
|
| 200 | + |
| 201 | + it('allows symbols as property keys', () => { |
| 202 | + const SECRET = Symbol('SECRET'); |
| 203 | + |
| 204 | + const Store = signalStore(withProps(() => ({ [SECRET]: 'secret' }))); |
| 205 | + const store = TestBed.configureTestingModule({ |
| 206 | + providers: [Store], |
| 207 | + }).inject(Store); |
| 208 | + |
| 209 | + expect(store[SECRET]).toBe('secret'); |
| 210 | + }); |
| 211 | + |
| 212 | + it('allows numbers as property keys', () => { |
| 213 | + const Store = signalStore(withProps(() => ({ 1: 'Number One' }))); |
| 214 | + const store = TestBed.configureTestingModule({ |
| 215 | + providers: [Store], |
| 216 | + }).inject(Store); |
| 217 | + |
| 218 | + expect(store[1]).toBe('Number One'); |
| 219 | + }); |
186 | 220 | });
|
187 | 221 |
|
188 | 222 | describe('withComputed', () => {
|
@@ -221,6 +255,20 @@ describe('signalStore', () => {
|
221 | 255 |
|
222 | 256 | expect(store.bar()).toBe('bar');
|
223 | 257 | });
|
| 258 | + |
| 259 | + it('allows symbols as computed keys', () => { |
| 260 | + const SECRET = Symbol('SECRET'); |
| 261 | + const SecretStore = signalStore( |
| 262 | + { providedIn: 'root' }, |
| 263 | + withComputed(() => ({ |
| 264 | + [SECRET]: computed(() => 'secret'), |
| 265 | + })) |
| 266 | + ); |
| 267 | + |
| 268 | + const secretStore = TestBed.inject(SecretStore); |
| 269 | + |
| 270 | + expect(secretStore[SECRET]()).toBe('secret'); |
| 271 | + }); |
224 | 272 | });
|
225 | 273 |
|
226 | 274 | describe('withMethods', () => {
|
@@ -263,6 +311,19 @@ describe('signalStore', () => {
|
263 | 311 |
|
264 | 312 | expect(store.baz()).toBe('baz');
|
265 | 313 | });
|
| 314 | + |
| 315 | + it('allows symbols as method keys', () => { |
| 316 | + const SECRET = Symbol('SECRET'); |
| 317 | + const SecretStore = signalStore( |
| 318 | + { providedIn: 'root' }, |
| 319 | + withMethods(() => ({ |
| 320 | + [SECRET]: () => 'my secret', |
| 321 | + })) |
| 322 | + ); |
| 323 | + const secretStore = TestBed.inject(SecretStore); |
| 324 | + |
| 325 | + expect(secretStore[SECRET]()).toBe('my secret'); |
| 326 | + }); |
266 | 327 | });
|
267 | 328 |
|
268 | 329 | describe('withHooks', () => {
|
@@ -455,5 +516,28 @@ describe('signalStore', () => {
|
455 | 516 | ],
|
456 | 517 | ]);
|
457 | 518 | });
|
| 519 | + |
| 520 | + it('passes on a symbol key to the features', () => { |
| 521 | + const SECRET = Symbol('SECRET'); |
| 522 | + const SecretStore = signalStore( |
| 523 | + withProps(() => ({ |
| 524 | + [SECRET]: 'not your business', |
| 525 | + })), |
| 526 | + withComputed((store) => ({ |
| 527 | + secret: computed(() => store[SECRET]), |
| 528 | + })), |
| 529 | + withMethods((store) => ({ |
| 530 | + reveil() { |
| 531 | + return store[SECRET]; |
| 532 | + }, |
| 533 | + })) |
| 534 | + ); |
| 535 | + |
| 536 | + const secretStore = new SecretStore(); |
| 537 | + |
| 538 | + expect(secretStore.reveil()).toBe('not your business'); |
| 539 | + expect(secretStore.secret()).toBe('not your business'); |
| 540 | + expect(secretStore[SECRET]).toBe('not your business'); |
| 541 | + }); |
458 | 542 | });
|
459 | 543 | });
|
0 commit comments