|
9 | 9 | Subject,
|
10 | 10 | } from "rxjs"
|
11 | 11 | import { renderHook, act as actHook } from "@testing-library/react-hooks"
|
12 |
| -import { switchMap, delay } from "rxjs/operators" |
| 12 | +import { switchMap, delay, take } from "rxjs/operators" |
13 | 13 | import { FC, Suspense, useState } from "react"
|
14 | 14 | import React from "react"
|
15 | 15 | import {
|
@@ -444,5 +444,80 @@ describe("connectFactoryObservable", () => {
|
444 | 444 | expect(sub4.closed).toBe(false)
|
445 | 445 | sub4.unsubscribe()
|
446 | 446 | })
|
| 447 | + |
| 448 | + describe("re-subscriptions on disposed observables", () => { |
| 449 | + it("registers itself when no other observable has been registered for that key", () => { |
| 450 | + const key = 0 |
| 451 | + let sideEffects = 0 |
| 452 | + |
| 453 | + const [, getShared] = bind((_: number) => |
| 454 | + defer(() => { |
| 455 | + return of(++sideEffects) |
| 456 | + }), |
| 457 | + ) |
| 458 | + |
| 459 | + const stream = getShared(key) |
| 460 | + |
| 461 | + let val |
| 462 | + stream.pipe(take(1)).subscribe((x) => { |
| 463 | + val = x |
| 464 | + }) |
| 465 | + expect(val).toBe(1) |
| 466 | + |
| 467 | + stream.pipe(take(1)).subscribe((x) => { |
| 468 | + val = x |
| 469 | + }) |
| 470 | + expect(val).toBe(2) |
| 471 | + |
| 472 | + const subscription = stream.subscribe((x) => { |
| 473 | + val = x |
| 474 | + }) |
| 475 | + expect(val).toBe(3) |
| 476 | + |
| 477 | + getShared(key) |
| 478 | + .pipe(take(1)) |
| 479 | + .subscribe((x) => { |
| 480 | + val = x |
| 481 | + }) |
| 482 | + expect(val).toBe(3) |
| 483 | + subscription.unsubscribe() |
| 484 | + }) |
| 485 | + |
| 486 | + it("subscribes to the currently registered observable if a new observalbe has been registered for that key", () => { |
| 487 | + const key = 0 |
| 488 | + let sideEffects = 0 |
| 489 | + |
| 490 | + const [, getShared] = bind((_: number) => |
| 491 | + defer(() => { |
| 492 | + return of(++sideEffects) |
| 493 | + }), |
| 494 | + ) |
| 495 | + |
| 496 | + const stream = getShared(key) |
| 497 | + |
| 498 | + let val |
| 499 | + stream.pipe(take(1)).subscribe((x) => { |
| 500 | + val = x |
| 501 | + }) |
| 502 | + expect(val).toBe(1) |
| 503 | + |
| 504 | + const subscription = getShared(key).subscribe((x) => { |
| 505 | + val = x |
| 506 | + }) |
| 507 | + expect(val).toBe(2) |
| 508 | + |
| 509 | + stream.pipe(take(1)).subscribe((x) => { |
| 510 | + val = x |
| 511 | + }) |
| 512 | + expect(val).toBe(2) |
| 513 | + |
| 514 | + stream.pipe(take(1)).subscribe((x) => { |
| 515 | + val = x |
| 516 | + }) |
| 517 | + expect(val).toBe(2) |
| 518 | + |
| 519 | + subscription.unsubscribe() |
| 520 | + }) |
| 521 | + }) |
447 | 522 | })
|
448 | 523 | })
|
0 commit comments