-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
72 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { type Observable } from "rxjs" | ||
|
||
export type SubscribeSource<Data> = | ||
export type SubscribeSourceFactory<Data> = | ||
| (() => Observable<Data>) | ||
| Promise<Data> | ||
| Observable<Data> | ||
| (() => Observable<Data> | undefined) | ||
| (() => Promise<Data>) | ||
| (() => Data) | ||
|
||
export type SubscribeSource<Data> = | ||
| SubscribeSourceFactory<Data> | ||
| Promise<Data> | ||
| Observable<Data> | ||
| Data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { afterEach, describe, expect, it } from "vitest" | ||
import { type Observable, of, tap } from "rxjs" | ||
import { renderHook, cleanup } from "@testing-library/react" | ||
import { useEffect, useState } from "react" | ||
import { waitForTimeout } from "../../tests/utils" | ||
import { useSubscribe } from "./useSubscribe" | ||
|
||
afterEach(() => { | ||
cleanup() | ||
}) | ||
|
||
describe("Given a function that returns undefined", () => { | ||
it("should subscribe and does nothing", async () => { | ||
const maybeObservableFn = () => undefined as Observable<number> | undefined | ||
|
||
renderHook(() => { | ||
useSubscribe(maybeObservableFn, []) | ||
}, {}) | ||
|
||
expect(true).toBe(true) | ||
}) | ||
|
||
describe("when the function returns an observable later", () => { | ||
it("should update and subscribe", async () => { | ||
let testValue = 0 | ||
|
||
renderHook(() => { | ||
const [value, setValue] = useState<Observable<number> | undefined>( | ||
undefined | ||
) | ||
|
||
useSubscribe( | ||
() => | ||
value?.pipe( | ||
tap((v) => { | ||
testValue = v | ||
}) | ||
), | ||
[value] | ||
) | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setValue(of(5)) | ||
}, 10) | ||
}, []) | ||
}, {}) | ||
|
||
await waitForTimeout(15) | ||
|
||
expect(testValue).toBe(5) | ||
Check failure on line 51 in src/lib/binding/useSubscribe.test.tsx
|
||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,5 @@ export default defineConfig({ | |
}, | ||
// handled by consumer | ||
minify: true | ||
}, | ||
} | ||
}) |