-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathCustomImageBadge.test.tsx
More file actions
147 lines (132 loc) · 4.48 KB
/
Copy pathCustomImageBadge.test.tsx
File metadata and controls
147 lines (132 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { createElement } from "react";
import { act, create } from "react-test-renderer";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Task, TaskRun } from "../types";
const { mockUseAuthStore, mockGetImages, mockGetEnvironments } = vi.hoisted(
() => ({
mockUseAuthStore: vi.fn(),
mockGetImages: vi.fn(),
mockGetEnvironments: vi.fn(),
}),
);
vi.mock("@/features/auth", () => ({ useAuthStore: mockUseAuthStore }));
vi.mock("../api", () => ({
getSandboxCustomImages: mockGetImages,
getSandboxEnvironments: mockGetEnvironments,
}));
vi.mock("phosphor-react-native", () => ({
Cube: (props: Record<string, unknown>) => createElement("Cube", props),
}));
vi.mock("@/lib/theme", () => ({
toRgba: (hex: string, alpha: number) => `${hex}/${alpha}`,
}));
import { CustomImageBadge } from "./CustomImageBadge";
function makeTask(run: Partial<TaskRun> | undefined): Task {
return {
id: "task-1",
task_number: 1,
slug: "task-1",
title: "Task",
description: "",
created_at: "2026-01-01T00:00:00Z",
updated_at: "2026-01-01T00:00:00Z",
origin_product: "user_created",
latest_run: run
? ({
id: "run-1",
task: "task-1",
team: 1,
branch: null,
status: "completed",
log_url: "",
error_message: null,
output: null,
state: {},
created_at: "2026-01-01T00:00:00Z",
updated_at: "2026-01-01T00:00:00Z",
completed_at: null,
...run,
} as TaskRun)
: undefined,
};
}
async function render(task: Task) {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, gcTime: 0 } },
});
let renderer: ReturnType<typeof create> | null = null;
await act(async () => {
renderer = create(
createElement(
QueryClientProvider,
{ client: queryClient },
createElement(CustomImageBadge, { task }),
),
);
});
// Flush pending react-query resolutions so a resolved image name renders.
await act(async () => {
await Promise.resolve();
await new Promise((resolve) => setTimeout(resolve, 0));
});
if (!renderer) throw new Error("Renderer not created");
return renderer as ReturnType<typeof create>;
}
function label(renderer: ReturnType<typeof create>): string | undefined {
const node = renderer.root.findAll(
(n) => typeof n.props?.accessibilityLabel === "string",
)[0];
return node?.props.accessibilityLabel as string | undefined;
}
describe("CustomImageBadge", () => {
beforeEach(() => {
mockUseAuthStore.mockReturnValue({
projectId: 1,
oauthAccessToken: "token",
});
mockGetImages.mockReset();
mockGetEnvironments.mockReset();
mockGetImages.mockResolvedValue([]);
mockGetEnvironments.mockResolvedValue([]);
});
it("renders nothing for a local run", async () => {
const r = await render(
makeTask({ environment: "local", state: { custom_image_id: "img-1" } }),
);
expect(r.toJSON()).toBeNull();
expect(mockGetImages).not.toHaveBeenCalled();
});
it("renders nothing for a cloud run without a custom image id", async () => {
const r = await render(makeTask({ environment: "cloud", state: {} }));
expect(r.toJSON()).toBeNull();
expect(mockGetImages).not.toHaveBeenCalled();
});
it("resolves the image name from a direct custom_image_id", async () => {
mockGetImages.mockResolvedValue([{ id: "img-1", name: "My Image" }]);
const r = await render(
makeTask({ environment: "cloud", state: { custom_image_id: "img-1" } }),
);
expect(label(r)).toBe('Runs on custom base image "My Image"');
});
it("resolves the image name via sandbox_environment_id", async () => {
mockGetEnvironments.mockResolvedValue([
{ id: "env-1", custom_image_id: "img-2", custom_image_name: null },
]);
mockGetImages.mockResolvedValue([{ id: "img-2", name: "Env Image" }]);
const r = await render(
makeTask({
environment: "cloud",
state: { sandbox_environment_id: "env-1" },
}),
);
expect(label(r)).toBe('Runs on custom base image "Env Image"');
});
it("renders nothing when the image cannot be resolved", async () => {
mockGetImages.mockResolvedValue([{ id: "other", name: "Other" }]);
const r = await render(
makeTask({ environment: "cloud", state: { custom_image_id: "missing" } }),
);
expect(r.toJSON()).toBeNull();
});
});