Skip to content

Commit

Permalink
feat: added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Aug 23, 2024
1 parent e1d8460 commit f87c1fd
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/lib/binding/types.ts
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
2 changes: 1 addition & 1 deletion src/lib/binding/useObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function useObserve<T>(
!maybeDeps && Array.isArray(optionsOrDeps)
? optionsOrDeps
: typeof source$ === "function"
? maybeDeps ?? []
? (maybeDeps ?? [])
: [source$]
const valueRef = useRef(
"getValue" in source$ && typeof source$.getValue === "function"
Expand Down
54 changes: 54 additions & 0 deletions src/lib/binding/useSubscribe.test.tsx
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

View workflow job for this annotation

GitHub Actions / Release

src/lib/binding/useSubscribe.test.tsx > Given a function that returns undefined > when the function returns an observable later > should update and subscribe

AssertionError: expected +0 to be 5 // Object.is equality - Expected + Received - 5 + 0 ❯ src/lib/binding/useSubscribe.test.tsx:51:25
})
})
})
4 changes: 2 additions & 2 deletions src/lib/binding/useSubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type SubscribeSource } from "./types"
import { catchError, EMPTY } from "rxjs"

export function useSubscribe<T>(
source: SubscribeSource<T>,
source: SubscribeSource<T> | (() => T),
deps: DependencyList = []
) {
const sourceRef = useLiveRef(source)
Expand All @@ -29,4 +29,4 @@ export function useSubscribe<T>(
...deps,
sourceRef
])
}
}
2 changes: 1 addition & 1 deletion src/lib/queries/client/queries/query/getDefaultState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getDefaultState<
return {
data,
dataUpdateCount: 0,
dataUpdatedAt: hasData ? initialDataUpdatedAt ?? Date.now() : 0,
dataUpdatedAt: hasData ? (initialDataUpdatedAt ?? Date.now()) : 0,
error: null,
errorUpdateCount: 0,
errorUpdatedAt: 0,
Expand Down
8 changes: 5 additions & 3 deletions src/lib/queries/client/queries/query/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const reduceState =

if (command === "cancel") {
const status =
acc.status === "pending" ? "pending" : current.status ?? acc.status
acc.status === "pending"
? "pending"
: (current.status ?? acc.status)

return { ...acc, ...current, status }
}
Expand Down Expand Up @@ -61,15 +63,15 @@ export const reduceState =

const dataUpdateCount = weHaveNewData
? acc.dataUpdateCount + 1
: current.dataUpdateCount ?? acc.dataUpdateCount
: (current.dataUpdateCount ?? acc.dataUpdateCount)

const newPendingStatusOnHold =
current.status === "pending" &&
previousStatusIsSuccessOrError &&
// (dataUpdateCount !== 0 || errorUpdateCount !== 0)
(hasData || hasError)

const error = status === "pending" ? null : current.error ?? acc.error
const error = status === "pending" ? null : (current.error ?? acc.error)

return {
...acc,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/operators/retryBackoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function retryBackoff<T, TError>(config: RetryBackoffConfig<T, TError>) {
? 0
: retry === true
? Infinity
: retry ?? Infinity
: (retry ?? Infinity)
: Infinity

const shouldRetry =
Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export default defineConfig({
},
// handled by consumer
minify: true
},
}
})

0 comments on commit f87c1fd

Please sign in to comment.