|
9 | 9 | import {of, Observable, BehaviorSubject, throwError} from 'rxjs'; |
10 | 10 | import {TestBed} from '../../testing'; |
11 | 11 | import {timeout} from '@angular/private/testing'; |
12 | | -import {ApplicationRef, Injector, signal} from '../../src/core'; |
| 12 | +import {ApplicationRef, Injector, ResourceRef, signal} from '../../src/core'; |
13 | 13 | import {rxResource} from '../src'; |
14 | 14 |
|
15 | 15 | describe('rxResource()', () => { |
@@ -116,6 +116,163 @@ 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 | + // @ts-expect-error - params is null, not string |
| 125 | + const _str: string = params; |
| 126 | + return of(''); |
| 127 | + }, |
| 128 | + injector: TestBed.inject(Injector), |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should type stream params correctly when params is provided', () => { |
| 133 | + rxResource({ |
| 134 | + params: () => 'foo', |
| 135 | + stream: ({params}) => { |
| 136 | + const _str: string = params; |
| 137 | + // @ts-expect-error - params is string, not null |
| 138 | + const _null: null = params; |
| 139 | + return of(''); |
| 140 | + }, |
| 141 | + injector: TestBed.inject(Injector), |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should type stream params as null with explicit single generic', () => { |
| 146 | + rxResource<string>({ |
| 147 | + stream: ({params}) => { |
| 148 | + const _null: null = params; |
| 149 | + // @ts-expect-error - params is null, not string |
| 150 | + const _str: string = params; |
| 151 | + return of(''); |
| 152 | + }, |
| 153 | + injector: TestBed.inject(Injector), |
| 154 | + }); |
| 155 | + }); |
| 156 | + it('should exclude undefined from stream params when params can return undefined', () => { |
| 157 | + const condition = signal(true); |
| 158 | + rxResource({ |
| 159 | + params: () => (condition() ? 'foo' : undefined), |
| 160 | + stream: ({params}) => { |
| 161 | + // params should be string, not string | undefined |
| 162 | + const _str: string = params; |
| 163 | + return of(''); |
| 164 | + }, |
| 165 | + injector: TestBed.inject(Injector), |
| 166 | + }); |
| 167 | + }); |
| 168 | + it('should type stream params as nullable when params option is potentially undefined', () => { |
| 169 | + function getParams(): (() => string) | undefined { |
| 170 | + return undefined; |
| 171 | + } |
| 172 | + rxResource({ |
| 173 | + params: getParams(), |
| 174 | + stream: ({params}) => { |
| 175 | + const _nullable: string | null = params; |
| 176 | + // @ts-expect-error - params could be null |
| 177 | + const _str: string = params; |
| 178 | + return of(''); |
| 179 | + }, |
| 180 | + injector: TestBed.inject(Injector), |
| 181 | + }); |
| 182 | + }); |
| 183 | + it('should type stream params as nullable with two explicit generics and no params', () => { |
| 184 | + rxResource<string, string>({ |
| 185 | + stream: ({params}) => { |
| 186 | + const _nullable: string | null = params; |
| 187 | + // @ts-expect-error - params could be null since params option is not provided |
| 188 | + const _str: string = params; |
| 189 | + return of(''); |
| 190 | + }, |
| 191 | + injector: TestBed.inject(Injector), |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should type stream params as nullable with two explicit generics and params: undefined', () => { |
| 196 | + rxResource<string, string>({ |
| 197 | + params: undefined, |
| 198 | + stream: ({params}) => { |
| 199 | + const _nullable: string | null = params; |
| 200 | + // @ts-expect-error - params could be null since params is undefined |
| 201 | + const _str: string = params; |
| 202 | + return of(''); |
| 203 | + }, |
| 204 | + injector: TestBed.inject(Injector), |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should narrow hasValue() when the value can be undefined', () => { |
| 209 | + const result: ResourceRef<number | undefined> = rxResource({ |
| 210 | + params: () => 1, |
| 211 | + stream: ({params}) => of(params), |
| 212 | + injector: TestBed.inject(Injector), |
| 213 | + }); |
| 214 | + if (result.hasValue()) { |
| 215 | + const _value: number = result.value(); |
| 216 | + } else if (result.isLoading()) { |
| 217 | + // @ts-expect-error |
| 218 | + const _value: number = result.value(); |
| 219 | + } else if (result.error()) { |
| 220 | + } |
| 221 | + const readonly = result.asReadonly(); |
| 222 | + if (readonly.hasValue()) { |
| 223 | + const _value: number = readonly.value(); |
| 224 | + } else if (readonly.isLoading()) { |
| 225 | + // @ts-expect-error |
| 226 | + const _value: number = readonly.value(); |
| 227 | + } else if (readonly.error()) { |
| 228 | + } |
| 229 | + }); |
| 230 | + |
| 231 | + it('should not narrow hasValue() when a default value is provided', () => { |
| 232 | + const result: ResourceRef<number> = rxResource({ |
| 233 | + params: () => 1, |
| 234 | + stream: ({params}) => of(params), |
| 235 | + injector: TestBed.inject(Injector), |
| 236 | + defaultValue: 0, |
| 237 | + }); |
| 238 | + if (result.hasValue()) { |
| 239 | + const _value: number = result.value(); |
| 240 | + } else if (result.isLoading()) { |
| 241 | + const _value: number = result.value(); |
| 242 | + } else if (result.error()) { |
| 243 | + } |
| 244 | + const readonly = result.asReadonly(); |
| 245 | + if (readonly.hasValue()) { |
| 246 | + const _value: number = readonly.value(); |
| 247 | + } else if (readonly.isLoading()) { |
| 248 | + const _value: number = readonly.value(); |
| 249 | + } else if (readonly.error()) { |
| 250 | + } |
| 251 | + }); |
| 252 | + |
| 253 | + it('should not narrow hasValue() when the resource type is unknown', () => { |
| 254 | + const result: ResourceRef<unknown> = rxResource({ |
| 255 | + params: () => 1 as unknown, |
| 256 | + stream: ({params}) => of(params), |
| 257 | + injector: TestBed.inject(Injector), |
| 258 | + defaultValue: 0, |
| 259 | + }); |
| 260 | + if (result.hasValue()) { |
| 261 | + const _value: unknown = result.value(); |
| 262 | + } else if (result.isLoading()) { |
| 263 | + const _value: unknown = result.value(); |
| 264 | + } else if (result.error()) { |
| 265 | + } |
| 266 | + const readonly = result.asReadonly(); |
| 267 | + if (readonly.hasValue()) { |
| 268 | + const _value: unknown = readonly.value(); |
| 269 | + } else if (readonly.isLoading()) { |
| 270 | + const _value: unknown = readonly.value(); |
| 271 | + } else if (readonly.error()) { |
| 272 | + } |
| 273 | + }); |
| 274 | +}); |
| 275 | + |
119 | 276 | async function waitFor(fn: () => boolean): Promise<void> { |
120 | 277 | while (!fn()) { |
121 | 278 | await timeout(1); |
|
0 commit comments