-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcreate.test.ts
More file actions
166 lines (139 loc) · 5.31 KB
/
create.test.ts
File metadata and controls
166 lines (139 loc) · 5.31 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Dashboard Create Command Tests
*
* Tests for the dashboard create command in src/commands/dashboard/create.ts.
* Uses spyOn pattern to mock API client and resolve-target.
*/
import {
afterEach,
beforeEach,
describe,
expect,
mock,
spyOn,
test,
} from "bun:test";
import { createCommand } from "../../../src/commands/dashboard/create.js";
// biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking
import * as apiClient from "../../../src/lib/api-client.js";
import { ContextError, ValidationError } from "../../../src/lib/errors.js";
// biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking
import * as resolveTarget from "../../../src/lib/resolve-target.js";
import type { DashboardDetail } from "../../../src/types/dashboard.js";
import { useAuthMock } from "../../helpers.js";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function createMockContext(cwd = "/tmp") {
const stdoutWrite = mock(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stderr: { write: mock(() => true) },
cwd,
},
stdoutWrite,
};
}
// ---------------------------------------------------------------------------
// Test data
// ---------------------------------------------------------------------------
const sampleDashboard: DashboardDetail = {
id: "123",
title: "My Dashboard",
widgets: [],
dateCreated: "2026-03-01T10:00:00Z",
};
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
useAuthMock();
describe("dashboard create", () => {
let createDashboardSpy: ReturnType<typeof spyOn>;
let resolveOrgSpy: ReturnType<typeof spyOn>;
let resolveAllTargetsSpy: ReturnType<typeof spyOn>;
let fetchProjectIdSpy: ReturnType<typeof spyOn>;
beforeEach(() => {
createDashboardSpy = spyOn(apiClient, "createDashboard");
resolveOrgSpy = spyOn(resolveTarget, "resolveOrg");
resolveAllTargetsSpy = spyOn(resolveTarget, "resolveAllTargets");
fetchProjectIdSpy = spyOn(resolveTarget, "fetchProjectId");
// Default mocks
resolveOrgSpy.mockResolvedValue({ org: "acme-corp" });
resolveAllTargetsSpy.mockResolvedValue({ targets: [] });
createDashboardSpy.mockResolvedValue(sampleDashboard);
fetchProjectIdSpy.mockResolvedValue(999);
});
afterEach(() => {
createDashboardSpy.mockRestore();
resolveOrgSpy.mockRestore();
resolveAllTargetsSpy.mockRestore();
fetchProjectIdSpy.mockRestore();
});
test("creates dashboard with title and verifies API args", async () => {
const { context } = createMockContext();
const func = await createCommand.loader();
await func.call(context, { json: false }, "My Dashboard");
expect(createDashboardSpy).toHaveBeenCalledWith("acme-corp", {
title: "My Dashboard",
widgets: [],
projects: undefined,
});
});
test("JSON output contains dashboard data and url", async () => {
const { context, stdoutWrite } = createMockContext();
const func = await createCommand.loader();
await func.call(context, { json: true }, "My Dashboard");
const output = stdoutWrite.mock.calls.map((c) => c[0]).join("");
const parsed = JSON.parse(output);
expect(parsed.id).toBe("123");
expect(parsed.title).toBe("My Dashboard");
expect(parsed.url).toContain("dashboard/123");
});
test("human output contains 'Created dashboard' and title", async () => {
const { context, stdoutWrite } = createMockContext();
const func = await createCommand.loader();
await func.call(context, { json: false }, "My Dashboard");
const output = stdoutWrite.mock.calls.map((c) => c[0]).join("");
expect(output).toContain("Created dashboard");
expect(output).toContain("My Dashboard");
});
test("throws ValidationError when title is missing", async () => {
const { context } = createMockContext();
const func = await createCommand.loader();
const err = await func
.call(context, { json: false })
.catch((e: Error) => e);
expect(err).toBeInstanceOf(ValidationError);
expect(err.message).toContain("Dashboard title is required");
});
test("two args parses target + title correctly", async () => {
const { context } = createMockContext();
const func = await createCommand.loader();
await func.call(context, { json: false }, "my-org/", "My Dashboard");
expect(createDashboardSpy).toHaveBeenCalledWith("my-org", {
title: "My Dashboard",
widgets: [],
projects: undefined,
});
});
test("throws ContextError when org cannot be resolved", async () => {
resolveOrgSpy.mockResolvedValue(null);
const { context } = createMockContext();
const func = await createCommand.loader();
await expect(
func.call(context, { json: false }, "My Dashboard")
).rejects.toThrow(ContextError);
});
test("explicit org/project target calls fetchProjectId", async () => {
const { context } = createMockContext();
const func = await createCommand.loader();
await func.call(
context,
{ json: false },
"my-org/my-project",
"My Dashboard"
);
expect(fetchProjectIdSpy).toHaveBeenCalledWith("my-org", "my-project");
});
});