-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdelete.test.ts
More file actions
178 lines (156 loc) · 5.48 KB
/
delete.test.ts
File metadata and controls
178 lines (156 loc) · 5.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Dashboard Widget Delete Command Tests
*
* Tests for the widget delete command in src/commands/dashboard/widget/delete.ts.
* Uses spyOn pattern to mock API client and resolve-target.
*/
import {
afterEach,
beforeEach,
describe,
expect,
mock,
spyOn,
test,
} from "bun:test";
import { deleteCommand } from "../../../../src/commands/dashboard/widget/delete.js";
// biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking
import * as apiClient from "../../../../src/lib/api-client.js";
import { 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: [
{
title: "Error Count",
displayType: "big_number",
widgetType: "spans",
queries: [
{
name: "",
conditions: "",
columns: [],
aggregates: ["count()"],
fields: ["count()"],
},
],
layout: { x: 0, y: 0, w: 2, h: 1 },
},
{
title: "Slow Spans",
displayType: "table",
widgetType: "spans",
queries: [
{
name: "",
conditions: "",
columns: ["span.description"],
aggregates: ["p95(span.duration)", "count()"],
fields: ["span.description", "p95(span.duration)", "count()"],
},
],
layout: { x: 2, y: 0, w: 4, h: 2 },
},
],
dateCreated: "2026-03-01T10:00:00Z",
};
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
useAuthMock();
describe("dashboard widget delete", () => {
let getDashboardSpy: ReturnType<typeof spyOn>;
let updateDashboardSpy: ReturnType<typeof spyOn>;
let resolveOrgSpy: ReturnType<typeof spyOn>;
beforeEach(() => {
getDashboardSpy = spyOn(apiClient, "getDashboard");
updateDashboardSpy = spyOn(apiClient, "updateDashboard");
resolveOrgSpy = spyOn(resolveTarget, "resolveOrg");
// Default mocks
resolveOrgSpy.mockResolvedValue({ org: "acme-corp" });
getDashboardSpy.mockResolvedValue(sampleDashboard);
updateDashboardSpy.mockImplementation(async (_org, _id, body) => ({
...sampleDashboard,
widgets: body.widgets,
}));
});
afterEach(() => {
getDashboardSpy.mockRestore();
updateDashboardSpy.mockRestore();
resolveOrgSpy.mockRestore();
});
test("deletes widget by index", async () => {
const { context } = createMockContext();
const func = await deleteCommand.loader();
await func.call(context, { json: false, index: 0 }, "123");
expect(getDashboardSpy).toHaveBeenCalledWith("acme-corp", "123");
expect(updateDashboardSpy).toHaveBeenCalledWith(
"acme-corp",
"123",
expect.objectContaining({
widgets: expect.not.arrayContaining([
expect.objectContaining({ title: "Error Count" }),
]),
})
);
// Only one widget should remain after deleting index 0
const body = updateDashboardSpy.mock.calls[0]?.[2];
expect(body.widgets.length).toBe(1);
expect(body.widgets[0].title).toBe("Slow Spans");
});
test("deletes widget by title", async () => {
const { context } = createMockContext();
const func = await deleteCommand.loader();
await func.call(context, { json: false, title: "Slow Spans" }, "123");
const body = updateDashboardSpy.mock.calls[0]?.[2];
expect(body.widgets.length).toBe(1);
expect(body.widgets[0].title).toBe("Error Count");
});
test("throws ValidationError when neither --index nor --title provided", async () => {
const { context } = createMockContext();
const func = await deleteCommand.loader();
const err = await func
.call(context, { json: false }, "123")
.catch((e: Error) => e);
expect(err).toBeInstanceOf(ValidationError);
expect(err.message).toContain("--index or --title");
});
test("throws ValidationError when index is out of range", async () => {
const { context } = createMockContext();
const func = await deleteCommand.loader();
const err = await func
.call(context, { json: false, index: 99 }, "123")
.catch((e: Error) => e);
expect(err).toBeInstanceOf(ValidationError);
expect(err.message).toContain("out of range");
});
test("human output contains 'Removed widget' and title", async () => {
const { context, stdoutWrite } = createMockContext();
const func = await deleteCommand.loader();
await func.call(context, { json: false, index: 0 }, "123");
const output = stdoutWrite.mock.calls.map((c) => c[0]).join("");
expect(output).toContain("Removed widget");
expect(output).toContain("Error Count");
});
});