Skip to content

Commit 615e469

Browse files
volivajosepot
andcommitted
test(bind): supports observables of functions
Co-authored-by: Josep M Sobrepere <josepot@gmail.com>
1 parent 31aba85 commit 615e469

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

packages/core/src/bind/connectFactoryObservable.test.tsx

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import {
2-
from,
3-
of,
4-
defer,
5-
concat,
2+
act as componentAct,
3+
fireEvent,
4+
render,
5+
screen,
6+
} from "@testing-library/react"
7+
import { act as actHook, renderHook } from "@testing-library/react-hooks"
8+
import React, { FC, Suspense, useState } from "react"
9+
import {
610
BehaviorSubject,
7-
throwError,
11+
concat,
12+
defer,
13+
from,
814
Observable,
15+
of,
916
Subject,
17+
throwError,
1018
} from "rxjs"
11-
import { renderHook, act as actHook } from "@testing-library/react-hooks"
12-
import { switchMap, delay, take, catchError, map } from "rxjs/operators"
13-
import { FC, Suspense, useState } from "react"
14-
import React from "react"
1519
import {
16-
act as componentAct,
17-
fireEvent,
18-
screen,
19-
render,
20-
} from "@testing-library/react"
20+
catchError,
21+
delay,
22+
map,
23+
startWith,
24+
switchMap,
25+
take,
26+
} from "rxjs/operators"
2127
import { bind } from "../"
2228
import { TestErrorBoundary } from "../test-helpers/TestErrorBoundary"
2329

@@ -427,6 +433,27 @@ describe("connectFactoryObservable", () => {
427433
unmount()
428434
})
429435

436+
it("supports streams that emit functions", () => {
437+
const values$ = new Subject<number>()
438+
439+
const [useFunction, function$] = bind(() =>
440+
values$.pipe(
441+
startWith(0),
442+
map((value) => () => value),
443+
),
444+
)
445+
const subscription = function$().subscribe()
446+
447+
const { result } = renderHook(() => useFunction())
448+
449+
expect(result.current()).toBe(0)
450+
451+
values$.next(1)
452+
expect(result.current()).toBe(1)
453+
454+
subscription.unsubscribe()
455+
})
456+
430457
it("if the observable hasn't emitted and a defaultValue is provided, it does not start suspense", () => {
431458
const number$ = new Subject<number>()
432459
const [useNumber] = bind(

packages/core/src/bind/connectObservable.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,27 @@ describe("connectObservable", () => {
493493
expect(errorCallback).not.toHaveBeenCalled()
494494
})
495495

496+
it("supports streams that emit functions", () => {
497+
const values$ = new Subject<number>()
498+
499+
const [useFunction, function$] = bind(
500+
values$.pipe(
501+
startWith(0),
502+
map((value) => () => value),
503+
),
504+
)
505+
const subscription = function$.subscribe()
506+
507+
const { result } = renderHook(() => useFunction())
508+
509+
expect(result.current()).toBe(0)
510+
511+
values$.next(1)
512+
expect(result.current()).toBe(1)
513+
514+
subscription.unsubscribe()
515+
})
516+
496517
it("should not trigger suspense when the stream emits synchronously", () => {
497518
const [useValue] = bind(NEVER.pipe(startWith("Hello")))
498519

0 commit comments

Comments
 (0)