Skip to content

test(bind): supports observables of functions #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/core/src/bind/connectFactoryObservable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
map,
switchMapTo,
first,
startWith,
} from "rxjs/operators"
import { FC, useState } from "react"
import React from "react"
Expand Down Expand Up @@ -497,6 +498,27 @@ describe("connectFactoryObservable", () => {
expect(errorCallback).not.toHaveBeenCalled()
})

it("supports streams that emit functions", () => {
const values$ = new Subject<number>()

const [useFunction, function$] = bind(() =>
values$.pipe(
startWith(0),
map((value) => () => value),
),
)
const subscription = function$().subscribe()

const { result } = renderHook(() => useFunction())

expect(result.current()).toBe(0)

values$.next(1)
expect(result.current()).toBe(1)

subscription.unsubscribe()
})

it("if the observable hasn't emitted and a defaultValue is provided, it does not start suspense", () => {
const number$ = new Subject<number>()
const [useNumber] = bind(
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/bind/connectObservable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,27 @@ describe("connectObservable", () => {
expect(errorCallback).not.toHaveBeenCalled()
})

it("supports streams that emit functions", () => {
const values$ = new Subject<number>()

const [useFunction, function$] = bind(
values$.pipe(
startWith(0),
map((value) => () => value),
),
)
const subscription = function$.subscribe()

const { result } = renderHook(() => useFunction())

expect(result.current()).toBe(0)

values$.next(1)
expect(result.current()).toBe(1)

subscription.unsubscribe()
})

it("should throw an error when the stream does not have a subscription", () => {
const [useValue] = bind(of("Hello"))
const errorCallback = jest.fn()
Expand Down