Skip to content

Commit 54e687e

Browse files
refactor(mobile): reuse the cloud task engine
Generated-By: PostHog Code Task-Id: c1bbe3cf-742b-4b24-bf96-d11a18b4cf22
1 parent b654072 commit 54e687e

3 files changed

Lines changed: 129 additions & 976 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
const mocks = vi.hoisted(() => ({
4+
engine: {
5+
off: vi.fn(),
6+
on: vi.fn(),
7+
reconnectIfDisconnected: vi.fn(),
8+
unwatch: vi.fn(),
9+
watch: vi.fn(),
10+
},
11+
}));
12+
13+
vi.mock("@posthog/core/cloud-task/cloud-task-engine", () => ({
14+
createCloudTaskEngine: () => mocks.engine,
15+
}));
16+
17+
vi.mock("@posthog/core/cloud-task/schemas", () => ({
18+
CloudTaskEvent: { Update: "cloud-task-update" },
19+
}));
20+
21+
vi.mock("expo/fetch", () => ({ fetch: vi.fn() }));
22+
23+
vi.mock("@/lib/api", () => ({
24+
authedFetch: vi.fn(),
25+
getBaseUrl: () => "https://app.posthog.test",
26+
getProjectId: () => 42,
27+
}));
28+
29+
vi.mock("@/lib/logger", () => ({
30+
logger: { scope: vi.fn() },
31+
}));
32+
33+
import { watchCloudTask } from "./cloudTaskStream";
34+
35+
describe("watchCloudTask", () => {
36+
beforeEach(() => {
37+
vi.clearAllMocks();
38+
});
39+
40+
it("asks the shared engine to reconnect only when disconnected", () => {
41+
const handle = watchCloudTask({
42+
taskId: "task-1",
43+
runId: "run-1",
44+
onUpdate: vi.fn(),
45+
});
46+
47+
handle.reconnectIfDisconnected();
48+
49+
expect(mocks.engine.reconnectIfDisconnected).toHaveBeenCalledWith(
50+
"task-1",
51+
"run-1",
52+
);
53+
});
54+
});

0 commit comments

Comments
 (0)