|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ResourceSnapshot} from '../../src/resource/api'; |
| 10 | +import {resourceFromSnapshots} from '../../src/resource/from_snapshots'; |
| 11 | +import {resource} from '../../src/resource/resource'; |
| 12 | +import {signal} from '../../src/render3/reactivity/signal'; |
| 13 | +import {Injector} from '../../src/di/injector'; |
| 14 | +import {ApplicationRef} from '../../src/application/application_ref'; |
| 15 | +import {TestBed} from '../../testing/src/test_bed'; |
| 16 | + |
| 17 | +describe('resource snapshots', () => { |
| 18 | + describe('resourceFromSnapshots', () => { |
| 19 | + it('should represent all stages of a resource', () => { |
| 20 | + const source = signal<ResourceSnapshot<string>>({status: 'idle', value: ''}); |
| 21 | + const res = resourceFromSnapshots(source); |
| 22 | + |
| 23 | + expect(res.status()).toEqual('idle'); |
| 24 | + expect(res.value()).toEqual(''); |
| 25 | + expect(res.isLoading()).toBeFalse(); |
| 26 | + expect(res.hasValue()).toBeTrue(); |
| 27 | + |
| 28 | + source.set({status: 'loading', value: 'alpha'}); |
| 29 | + expect(res.status()).toEqual('loading'); |
| 30 | + expect(res.value()).toEqual('alpha'); |
| 31 | + expect(res.isLoading()).toBeTrue(); |
| 32 | + expect(res.hasValue()).toBeTrue(); |
| 33 | + |
| 34 | + source.set({status: 'resolved', value: 'beta'}); |
| 35 | + expect(res.status()).toEqual('resolved'); |
| 36 | + expect(res.value()).toEqual('beta'); |
| 37 | + expect(res.isLoading()).toBeFalse(); |
| 38 | + expect(res.hasValue()).toBeTrue(); |
| 39 | + |
| 40 | + source.set({status: 'reloading', value: 'gamma'}); |
| 41 | + expect(res.status()).toEqual('reloading'); |
| 42 | + expect(res.value()).toEqual('gamma'); |
| 43 | + expect(res.isLoading()).toBeTrue(); |
| 44 | + expect(res.hasValue()).toBeTrue(); |
| 45 | + |
| 46 | + source.set({status: 'local', value: 'delta'}); |
| 47 | + expect(res.status()).toEqual('local'); |
| 48 | + expect(res.value()).toEqual('delta'); |
| 49 | + expect(res.isLoading()).toBeFalse(); |
| 50 | + expect(res.hasValue()).toBeTrue(); |
| 51 | + |
| 52 | + const error = new Error(); |
| 53 | + source.set({status: 'error', error}); |
| 54 | + expect(res.status()).toEqual('error'); |
| 55 | + expect(res.error()).toBe(error); |
| 56 | + expect(res.isLoading()).toBeFalse(); |
| 57 | + expect(res.hasValue()).toBeFalse(); |
| 58 | + expect(res.value).toThrowMatching((err: Error) => err.cause === error); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should return `false` for hasValue() when the value is undefined', () => { |
| 62 | + const source = signal<ResourceSnapshot<string | undefined>>({ |
| 63 | + status: 'loading', |
| 64 | + value: undefined, |
| 65 | + }); |
| 66 | + const res = resourceFromSnapshots(source); |
| 67 | + |
| 68 | + expect(res.hasValue()).toBeFalse(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should memoize the snapshot function', () => { |
| 72 | + let readCount = 0; |
| 73 | + function source(): ResourceSnapshot<string> { |
| 74 | + readCount++; |
| 75 | + return { |
| 76 | + status: 'resolved', |
| 77 | + value: 'test', |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + const res = resourceFromSnapshots(source); |
| 82 | + |
| 83 | + // Access multiple computeds that depend on the snapshot. |
| 84 | + res.status(); |
| 85 | + res.value(); |
| 86 | + res.error(); |
| 87 | + |
| 88 | + // The `source` function should only have been called once. |
| 89 | + expect(readCount).toBe(1); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('Resource.snapshot', () => { |
| 94 | + it('should represent idle, loading and resolved states', async () => { |
| 95 | + const injector = TestBed.inject(Injector); |
| 96 | + const params = signal<number | undefined>(undefined); |
| 97 | + const res = resource({ |
| 98 | + params, |
| 99 | + loader: () => Promise.resolve('test'), |
| 100 | + injector, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(res.snapshot()).toEqual({status: 'idle', value: undefined}); |
| 104 | + |
| 105 | + params.set(3); |
| 106 | + expect(res.snapshot()).toEqual({status: 'loading', value: undefined}); |
| 107 | + |
| 108 | + await injector.get(ApplicationRef).whenStable(); |
| 109 | + expect(res.snapshot()).toEqual({status: 'resolved', value: 'test'}); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should represent the error state', async () => { |
| 113 | + const injector = TestBed.inject(Injector); |
| 114 | + const res = resource({ |
| 115 | + loader: () => { |
| 116 | + throw new Error('test'); |
| 117 | + }, |
| 118 | + injector, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(res.snapshot()).toEqual({status: 'loading', value: undefined}); |
| 122 | + |
| 123 | + await injector.get(ApplicationRef).whenStable(); |
| 124 | + const snap = res.snapshot(); |
| 125 | + if (snap.status !== 'error') { |
| 126 | + return fail(`Expected resource to be in error state`); |
| 127 | + } |
| 128 | + expect(res.error).toBeDefined(); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments