test(mobile): cover ota provider helpers

This commit is contained in:
DIYgod
2026-04-10 21:11:44 +08:00
parent 3190f158b1
commit d6258b94e2
2 changed files with 165 additions and 19 deletions
@@ -0,0 +1,113 @@
import { beforeAll, describe, expect, it, vi } from "vitest"
vi.mock("expo-application", () => ({
nativeApplicationVersion: "0.4.1",
}))
vi.mock("expo-updates", () => ({
default: {},
channel: "production",
runtimeVersion: "0.4.1",
isEnabled: true,
reloadAsync: vi.fn(),
useUpdates: vi.fn(),
UpdateInfoType: {
NEW: "new",
},
}))
vi.mock("react-native", () => ({
InteractionManager: {
runAfterInteractions: (callback: () => void) => {
callback()
return {
cancel: vi.fn(),
}
},
},
}))
let runSafeReloadUpdate: typeof import("../provider").runSafeReloadUpdate
let runWithSingleInFlight: typeof import("../provider").runWithSingleInFlight
beforeAll(async () => {
;({ runSafeReloadUpdate, runWithSingleInFlight } = await import("../provider"))
})
describe("runWithSingleInFlight", () => {
it("deduplicates concurrent calls and clears after resolution", async () => {
let resolveRun: ((value: { kind: "idle" }) => void) | null = null
const inFlightRef: { current: Promise<{ kind: "idle" }> | null } = { current: null }
const run = vi.fn(
() =>
new Promise<{ kind: "idle" }>((resolve) => {
resolveRun = resolve
}),
)
const first = runWithSingleInFlight({
inFlightRef,
run,
})
const second = runWithSingleInFlight({
inFlightRef,
run,
})
expect(run).toHaveBeenCalledTimes(1)
expect(second).toBe(first)
expect(resolveRun).not.toBeNull()
resolveRun!({ kind: "idle" })
await expect(first).resolves.toEqual({ kind: "idle" })
expect(inFlightRef.current).toBeNull()
const nextRun = vi.fn().mockResolvedValue({ kind: "idle" as const })
const third = runWithSingleInFlight({
inFlightRef,
run: nextRun,
})
await expect(third).resolves.toEqual({ kind: "idle" })
expect(nextRun).toHaveBeenCalledTimes(1)
})
})
describe("runSafeReloadUpdate", () => {
it("does not reload when updates are disabled", async () => {
const reload = vi.fn().mockResolvedValue()
await runSafeReloadUpdate({
isEnabled: false,
pendingVersion: "0.4.3",
reload,
})
expect(reload).not.toHaveBeenCalled()
})
it("does not reload when no pending update exists", async () => {
const reload = vi.fn().mockResolvedValue()
await runSafeReloadUpdate({
isEnabled: true,
pendingVersion: null,
reload,
})
expect(reload).not.toHaveBeenCalled()
})
it("reloads when updates are enabled and a pending version exists", async () => {
const reload = vi.fn().mockResolvedValue()
await runSafeReloadUpdate({
isEnabled: true,
pendingVersion: "0.4.3",
reload,
})
expect(reload).toHaveBeenCalledTimes(1)
})
})
+52 -19
View File
@@ -62,6 +62,43 @@ const getErrorMessage = (error: unknown, fallback: string) => {
return error instanceof Error ? error.message : fallback
}
export function runWithSingleInFlight({
inFlightRef,
run,
}: {
inFlightRef: { current: Promise<OtaCheckForUpdatesResult> | null }
run: () => Promise<OtaCheckForUpdatesResult>
}) {
if (inFlightRef.current) {
return inFlightRef.current
}
const promise = run().finally(() => {
if (inFlightRef.current === promise) {
inFlightRef.current = null
}
})
inFlightRef.current = promise
return promise
}
export const runSafeReloadUpdate = async ({
isEnabled,
pendingVersion,
reload,
}: {
isEnabled: boolean
pendingVersion: string | null
reload: () => Promise<void>
}) => {
if (!isEnabled || !pendingVersion) {
return
}
await reload()
}
const runOtaCheck = async ({
dispatch,
currentPendingVersion,
@@ -188,19 +225,16 @@ const runGuardedOtaCheck = ({
shouldCancel,
})
const promise = runOtaCheck({
dispatch,
currentPendingVersion,
isUpdatePending,
shouldCancel,
}).finally(() => {
if (inFlightRef.current === promise) {
inFlightRef.current = null
}
return runWithSingleInFlight({
inFlightRef,
run: () =>
runOtaCheck({
dispatch,
currentPendingVersion,
isUpdatePending,
shouldCancel,
}),
})
inFlightRef.current = promise
return promise
}
export const OtaProvider = ({ children }: PropsWithChildren) => {
@@ -289,13 +323,12 @@ export const OtaProvider = ({ children }: PropsWithChildren) => {
isUpdatePending,
shouldCancel: () => isUnmountedRef.current,
}),
reloadUpdate: async () => {
if (!Updates.isEnabled || !currentPendingVersion) {
return
}
await Updates.reloadAsync()
},
reloadUpdate: async () =>
runSafeReloadUpdate({
isEnabled: Updates.isEnabled,
pendingVersion: currentPendingVersion,
reload: () => Updates.reloadAsync(),
}),
}
}, [currentPendingVersion, isUpdatePending])