|
9 | 9 | Subject, |
10 | 10 | } from "rxjs" |
11 | 11 | import { renderHook, act as actHook } from "@testing-library/react-hooks" |
12 | | -import { switchMap, delay, take, catchError } from "rxjs/operators" |
| 12 | +import { switchMap, delay, take, catchError, map } from "rxjs/operators" |
13 | 13 | import { FC, Suspense, useState } from "react" |
14 | 14 | import React from "react" |
15 | 15 | import { |
@@ -426,6 +426,53 @@ describe("connectFactoryObservable", () => { |
426 | 426 |
|
427 | 427 | unmount() |
428 | 428 | }) |
| 429 | + |
| 430 | + it("if the observable hasn't emitted and a defaultValue is provided, it does not start suspense", () => { |
| 431 | + const number$ = new Subject<number>() |
| 432 | + const [useNumber] = bind( |
| 433 | + (id: number) => number$.pipe(map((x) => x + id)), |
| 434 | + 0, |
| 435 | + ) |
| 436 | + |
| 437 | + const { result, unmount } = renderHook(() => useNumber(5)) |
| 438 | + |
| 439 | + expect(result.current).toBe(0) |
| 440 | + |
| 441 | + actHook(() => { |
| 442 | + number$.next(5) |
| 443 | + }) |
| 444 | + |
| 445 | + expect(result.current).toBe(10) |
| 446 | + |
| 447 | + unmount() |
| 448 | + }) |
| 449 | + |
| 450 | + it("when a defaultValue is provided, the first subscription happens once the component is mounted", () => { |
| 451 | + let nTopSubscriptions = 0 |
| 452 | + |
| 453 | + const [useNTopSubscriptions] = bind( |
| 454 | + (id: number) => |
| 455 | + defer(() => { |
| 456 | + return of(++nTopSubscriptions + id) |
| 457 | + }), |
| 458 | + 1, |
| 459 | + ) |
| 460 | + |
| 461 | + const { result, rerender, unmount } = renderHook(() => |
| 462 | + useNTopSubscriptions(0), |
| 463 | + ) |
| 464 | + |
| 465 | + expect(result.current).toBe(1) |
| 466 | + |
| 467 | + actHook(() => { |
| 468 | + rerender() |
| 469 | + }) |
| 470 | + |
| 471 | + expect(result.current).toBe(1) |
| 472 | + expect(nTopSubscriptions).toBe(1) |
| 473 | + |
| 474 | + unmount() |
| 475 | + }) |
429 | 476 | }) |
430 | 477 |
|
431 | 478 | describe("observable", () => { |
|
0 commit comments