|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {of, Observable, BehaviorSubject, throwError} from 'rxjs'; |
10 | | -import {TestBed} from '../../testing'; |
11 | 9 | import {timeout} from '@angular/private/testing'; |
12 | | -import {ApplicationRef, Injector, signal} from '../../src/core'; |
| 10 | +import {BehaviorSubject, Observable, of, throwError} from 'rxjs'; |
| 11 | +import {ApplicationRef, Injector, ResourceRef, signal} from '../../src/core'; |
| 12 | +import {TestBed} from '../../testing'; |
13 | 13 | import {rxResource} from '../src'; |
14 | 14 |
|
15 | 15 | describe('rxResource()', () => { |
@@ -116,6 +116,128 @@ describe('rxResource()', () => { |
116 | 116 | }); |
117 | 117 | }); |
118 | 118 |
|
| 119 | +describe('types', () => { |
| 120 | + it('should type stream params as null when params option is omitted', () => { |
| 121 | + rxResource({ |
| 122 | + stream: ({params}) => { |
| 123 | + const _null: null = params; |
| 124 | + return of(''); |
| 125 | + }, |
| 126 | + injector: TestBed.inject(Injector), |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should type stream params correctly when params is provided', () => { |
| 131 | + rxResource({ |
| 132 | + params: () => 'foo', |
| 133 | + stream: ({params}) => { |
| 134 | + const _str: string = params; |
| 135 | + return of(''); |
| 136 | + }, |
| 137 | + injector: TestBed.inject(Injector), |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should type stream params as null with explicit single generic', () => { |
| 142 | + rxResource<string>({ |
| 143 | + stream: ({params}) => { |
| 144 | + const _null: null = params; |
| 145 | + return of(''); |
| 146 | + }, |
| 147 | + injector: TestBed.inject(Injector), |
| 148 | + }); |
| 149 | + }); |
| 150 | + it('should exclude undefined from stream params when params can return undefined', () => { |
| 151 | + const condition = signal(true); |
| 152 | + rxResource({ |
| 153 | + params: () => (condition() ? 'foo' : undefined), |
| 154 | + stream: ({params}) => { |
| 155 | + const _str: string = params; |
| 156 | + return of(''); |
| 157 | + }, |
| 158 | + injector: TestBed.inject(Injector), |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should error when two explicit generics are provided but params is absent', () => { |
| 163 | + // @ts-expect-error: params is required when the second generic is not null |
| 164 | + rxResource<string, string>({ |
| 165 | + stream: ({params}) => { |
| 166 | + const _str: string = params; |
| 167 | + return of(''); |
| 168 | + }, |
| 169 | + injector: TestBed.inject(Injector), |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should narrow hasValue() when the value can be undefined', () => { |
| 174 | + const result: ResourceRef<number | undefined> = rxResource({ |
| 175 | + params: () => 1, |
| 176 | + stream: ({params}) => of(params), |
| 177 | + injector: TestBed.inject(Injector), |
| 178 | + }); |
| 179 | + if (result.hasValue()) { |
| 180 | + const _value: number = result.value(); |
| 181 | + } else if (result.isLoading()) { |
| 182 | + // @ts-expect-error |
| 183 | + const _value: number = result.value(); |
| 184 | + } else if (result.error()) { |
| 185 | + } |
| 186 | + const readonly = result.asReadonly(); |
| 187 | + if (readonly.hasValue()) { |
| 188 | + const _value: number = readonly.value(); |
| 189 | + } else if (readonly.isLoading()) { |
| 190 | + // @ts-expect-error |
| 191 | + const _value: number = readonly.value(); |
| 192 | + } else if (readonly.error()) { |
| 193 | + } |
| 194 | + }); |
| 195 | + |
| 196 | + it('should not narrow hasValue() when a default value is provided', () => { |
| 197 | + const result: ResourceRef<number> = rxResource({ |
| 198 | + params: () => 1, |
| 199 | + stream: ({params}) => of(params), |
| 200 | + injector: TestBed.inject(Injector), |
| 201 | + defaultValue: 0, |
| 202 | + }); |
| 203 | + if (result.hasValue()) { |
| 204 | + const _value: number = result.value(); |
| 205 | + } else if (result.isLoading()) { |
| 206 | + const _value: number = result.value(); |
| 207 | + } else if (result.error()) { |
| 208 | + } |
| 209 | + const readonly = result.asReadonly(); |
| 210 | + if (readonly.hasValue()) { |
| 211 | + const _value: number = readonly.value(); |
| 212 | + } else if (readonly.isLoading()) { |
| 213 | + const _value: number = readonly.value(); |
| 214 | + } else if (readonly.error()) { |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + it('should not narrow hasValue() when the resource type is unknown', () => { |
| 219 | + const result: ResourceRef<unknown> = rxResource({ |
| 220 | + params: () => 1 as unknown, |
| 221 | + stream: ({params}) => of(params), |
| 222 | + injector: TestBed.inject(Injector), |
| 223 | + defaultValue: 0, |
| 224 | + }); |
| 225 | + if (result.hasValue()) { |
| 226 | + const _value: unknown = result.value(); |
| 227 | + } else if (result.isLoading()) { |
| 228 | + const _value: unknown = result.value(); |
| 229 | + } else if (result.error()) { |
| 230 | + } |
| 231 | + const readonly = result.asReadonly(); |
| 232 | + if (readonly.hasValue()) { |
| 233 | + const _value: unknown = readonly.value(); |
| 234 | + } else if (readonly.isLoading()) { |
| 235 | + const _value: unknown = readonly.value(); |
| 236 | + } else if (readonly.error()) { |
| 237 | + } |
| 238 | + }); |
| 239 | +}); |
| 240 | + |
119 | 241 | async function waitFor(fn: () => boolean): Promise<void> { |
120 | 242 | while (!fn()) { |
121 | 243 | await timeout(1); |
|
0 commit comments