|
| 1 | +import { computed, isSignal, signal } from '@angular/core'; |
| 2 | +import { delegatedSignal } from '../src'; |
| 3 | + |
| 4 | +describe('delegatedSignal', () => { |
| 5 | + it('updates from source to delegated signal', () => { |
| 6 | + const query = signal(''); |
| 7 | + const limit = signal(10); |
| 8 | + |
| 9 | + const delegated = delegatedSignal({ |
| 10 | + source: () => ({ query: query(), limit: limit() }), |
| 11 | + update: ({ query: q, limit: l }) => { |
| 12 | + query.set(q); |
| 13 | + limit.set(l); |
| 14 | + }, |
| 15 | + }); |
| 16 | + |
| 17 | + expect(delegated()).toEqual({ query: '', limit: 10 }); |
| 18 | + |
| 19 | + query.set('ngrx'); |
| 20 | + expect(delegated()).toEqual({ query: 'ngrx', limit: 10 }); |
| 21 | + |
| 22 | + limit.set(25); |
| 23 | + expect(delegated()).toEqual({ query: 'ngrx', limit: 25 }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('updates from delegated signal to source', () => { |
| 27 | + const query = signal(''); |
| 28 | + const limit = signal(10); |
| 29 | + |
| 30 | + const delegated = delegatedSignal({ |
| 31 | + source: () => ({ query: query(), limit: limit() }), |
| 32 | + update: ({ query: q, limit: l }) => { |
| 33 | + query.set(q); |
| 34 | + limit.set(l); |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + delegated.set({ query: 'ngrx', limit: 25 }); |
| 39 | + expect(query()).toBe('ngrx'); |
| 40 | + expect(limit()).toBe(25); |
| 41 | + |
| 42 | + delegated.update((current) => ({ ...current, query: 'signals' })); |
| 43 | + expect(query()).toBe('signals'); |
| 44 | + expect(limit()).toBe(25); |
| 45 | + }); |
| 46 | + |
| 47 | + it('asReadonly returns a readonly signal backed by the source computation', () => { |
| 48 | + const query = signal(''); |
| 49 | + const limit = signal(10); |
| 50 | + |
| 51 | + const delegated = delegatedSignal({ |
| 52 | + source: () => ({ query: query(), limit: limit() }), |
| 53 | + update: ({ query: q, limit: l }) => { |
| 54 | + query.set(q); |
| 55 | + limit.set(l); |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + const readonly = delegated.asReadonly(); |
| 60 | + expect(isSignal(readonly)).toBe(true); |
| 61 | + expect('set' in readonly).toBe(false); |
| 62 | + expect('update' in readonly).toBe(false); |
| 63 | + expect(readonly()).toEqual({ query: '', limit: 10 }); |
| 64 | + |
| 65 | + query.set('ngrx'); |
| 66 | + expect(readonly()).toEqual({ query: 'ngrx', limit: 10 }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('uses the provided equality function', () => { |
| 70 | + const query = signal(''); |
| 71 | + const limit = signal(10); |
| 72 | + let recomputeCount = 0; |
| 73 | + |
| 74 | + const delegated = delegatedSignal({ |
| 75 | + source: () => ({ query: query(), limit: limit() }), |
| 76 | + update: ({ query: q, limit: l }) => { |
| 77 | + query.set(q); |
| 78 | + limit.set(l); |
| 79 | + }, |
| 80 | + equal: (a, b) => a.query === b.query, |
| 81 | + }); |
| 82 | + |
| 83 | + const downstream = computed(() => { |
| 84 | + recomputeCount++; |
| 85 | + return delegated(); |
| 86 | + }); |
| 87 | + |
| 88 | + downstream(); |
| 89 | + expect(recomputeCount).toBe(1); |
| 90 | + |
| 91 | + limit.set(25); |
| 92 | + downstream(); |
| 93 | + expect(recomputeCount).toBe(1); |
| 94 | + |
| 95 | + query.set('ngrx'); |
| 96 | + downstream(); |
| 97 | + expect(recomputeCount).toBe(2); |
| 98 | + }); |
| 99 | +}); |
0 commit comments