|
1 | 1 | import { rerender, settled } from '@ember/test-helpers';
|
2 | 2 |
|
3 |
| -import { createDeferred, setPromiseResult } from '@ember-data/request'; |
| 3 | +import { createDeferred, setPromiseResult, type Awaitable } from '@ember-data/request'; |
4 | 4 | import type { RenderingTestContext } from '@warp-drive/diagnostic/ember';
|
5 | 5 | import { module, setupRenderingTest, test } from '@warp-drive/diagnostic/ember';
|
6 | 6 | import { getPromiseState } from '@warp-drive/ember';
|
7 | 7 |
|
8 | 8 | type PromiseState<T, E> = ReturnType<typeof getPromiseState<T, E>>;
|
| 9 | +const SecretSymbol = Symbol.for('LegacyPromiseProxy'); |
| 10 | + |
| 11 | +class PromiseProxy<T, E> { |
| 12 | + [SecretSymbol]: true; |
| 13 | + promise: Awaitable<T, E>; |
| 14 | + |
| 15 | + constructor(promise: Awaitable<T, E>) { |
| 16 | + this[SecretSymbol] = true; |
| 17 | + this.promise = promise; |
| 18 | + } |
| 19 | + |
| 20 | + then(onFulfilled: (value: T) => T, onRejected: (error: E) => void): Promise<T> { |
| 21 | + return this.promise.then(onFulfilled, onRejected) as Promise<T>; |
| 22 | + } |
| 23 | + |
| 24 | + catch(onRejected: (error: E) => void): Promise<unknown> { |
| 25 | + return this.promise.catch(onRejected) as Promise<unknown>; |
| 26 | + } |
| 27 | + |
| 28 | + finally(onFinally: () => void): Promise<unknown> { |
| 29 | + return this.promise.finally(onFinally) as Promise<unknown>; |
| 30 | + } |
| 31 | +} |
9 | 32 |
|
10 | 33 | module('Integration | get-promise-state', function (hooks) {
|
11 | 34 | setupRenderingTest(hooks);
|
@@ -196,4 +219,119 @@ module('Integration | get-promise-state', function (hooks) {
|
196 | 219 | assert.equal(counter, 1);
|
197 | 220 | assert.equal(this.element.textContent?.trim(), 'Our Error\n Count:\n 1');
|
198 | 221 | });
|
| 222 | + |
| 223 | + test('it renders only once when the promise already has a result cached', async function (this: RenderingTestContext, assert) { |
| 224 | + const promise = Promise.resolve().then(() => 'Our Data'); |
| 225 | + |
| 226 | + const result = await promise; |
| 227 | + setPromiseResult(promise, { result, isError: false }); |
| 228 | + |
| 229 | + let state: PromiseState<string, Error>; |
| 230 | + function _getPromiseState<T>(p: Promise<T>): PromiseState<T, Error> { |
| 231 | + state = getPromiseState(p) as PromiseState<string, Error>; |
| 232 | + return state as PromiseState<T, Error>; |
| 233 | + } |
| 234 | + let counter = 0; |
| 235 | + function countFor(_result: unknown) { |
| 236 | + return ++counter; |
| 237 | + } |
| 238 | + |
| 239 | + await this.render( |
| 240 | + <template> |
| 241 | + {{#let (_getPromiseState promise) as |state|}} |
| 242 | + {{state.result}}<br />Count: |
| 243 | + {{countFor state.result}} |
| 244 | + {{/let}} |
| 245 | + </template> |
| 246 | + ); |
| 247 | + |
| 248 | + assert.equal(this.element.textContent?.trim(), 'Our DataCount:\n 1'); |
| 249 | + await settled(); |
| 250 | + |
| 251 | + assert.equal(this.element.textContent?.trim(), 'Our DataCount:\n 1'); |
| 252 | + }); |
| 253 | + |
| 254 | + test('it unwraps promise-proxies that utilize the secret symbol for error states', async function (this: RenderingTestContext, assert) { |
| 255 | + const _promise = Promise.resolve().then(() => { |
| 256 | + throw new Error('Our Error'); |
| 257 | + }); |
| 258 | + const promise = new PromiseProxy(_promise); |
| 259 | + |
| 260 | + try { |
| 261 | + getPromiseState(promise); |
| 262 | + await promise; |
| 263 | + } catch { |
| 264 | + // do nothing |
| 265 | + } |
| 266 | + |
| 267 | + let state: PromiseState<string, Error>; |
| 268 | + function _getPromiseState<T>(p: Promise<T>): PromiseState<T, Error> { |
| 269 | + state = getPromiseState(p) as PromiseState<string, Error>; |
| 270 | + return state as PromiseState<T, Error>; |
| 271 | + } |
| 272 | + let counter = 0; |
| 273 | + function countFor(_result: unknown, _error: unknown) { |
| 274 | + return ++counter; |
| 275 | + } |
| 276 | + |
| 277 | + await this.render( |
| 278 | + <template> |
| 279 | + {{#let (_getPromiseState promise) as |state|}} |
| 280 | + {{#if state.isPending}} |
| 281 | + Pending |
| 282 | + {{else if state.isError}} |
| 283 | + {{state.error.message}} |
| 284 | + {{else if state.isSuccess}} |
| 285 | + Invalid Success Reached |
| 286 | + {{/if}} |
| 287 | + <br />Count: |
| 288 | + {{countFor state.result state.error}}{{/let}} |
| 289 | + </template> |
| 290 | + ); |
| 291 | + |
| 292 | + assert.equal(state!.result, null); |
| 293 | + assert.true(state!.error instanceof Error); |
| 294 | + assert.equal((state!.error as Error | undefined)?.message, 'Our Error'); |
| 295 | + assert.equal(counter, 1); |
| 296 | + assert.equal(this.element.textContent?.trim(), 'Our Error\n Count:\n 1'); |
| 297 | + await rerender(); |
| 298 | + assert.equal(state!.result, null); |
| 299 | + assert.true(state!.error instanceof Error); |
| 300 | + assert.equal((state!.error as Error | undefined)?.message, 'Our Error'); |
| 301 | + assert.equal(counter, 1); |
| 302 | + assert.equal(this.element.textContent?.trim(), 'Our Error\n Count:\n 1'); |
| 303 | + assert.strictEqual(state, getPromiseState(_promise)); |
| 304 | + }); |
| 305 | + |
| 306 | + test('it unwraps promise-proxies that utilize the secret symbol for success states', async function (this: RenderingTestContext, assert) { |
| 307 | + const _promise = Promise.resolve().then(() => 'Our Data'); |
| 308 | + const promise = new PromiseProxy(_promise); |
| 309 | + getPromiseState(promise); |
| 310 | + await promise; |
| 311 | + |
| 312 | + let state: PromiseState<string, Error>; |
| 313 | + function _getPromiseState<T>(p: Promise<T>): PromiseState<T, Error> { |
| 314 | + state = getPromiseState(p) as PromiseState<string, Error>; |
| 315 | + return state as PromiseState<T, Error>; |
| 316 | + } |
| 317 | + let counter = 0; |
| 318 | + function countFor(_result: unknown) { |
| 319 | + return ++counter; |
| 320 | + } |
| 321 | + |
| 322 | + await this.render( |
| 323 | + <template> |
| 324 | + {{#let (_getPromiseState promise) as |state|}} |
| 325 | + {{state.result}}<br />Count: |
| 326 | + {{countFor state.result}} |
| 327 | + {{/let}} |
| 328 | + </template> |
| 329 | + ); |
| 330 | + |
| 331 | + assert.equal(this.element.textContent?.trim(), 'Our DataCount:\n 1'); |
| 332 | + await settled(); |
| 333 | + |
| 334 | + assert.equal(this.element.textContent?.trim(), 'Our DataCount:\n 1'); |
| 335 | + assert.strictEqual(state, getPromiseState(_promise)); |
| 336 | + }); |
199 | 337 | });
|
0 commit comments