forked from jenkinsci/pipeline-graph-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-steps-poller.spec.ts
More file actions
161 lines (124 loc) · 4.85 KB
/
use-steps-poller.spec.ts
File metadata and controls
161 lines (124 loc) · 4.85 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
/** * @vitest-environment jsdom */
import { act, renderHook, waitFor } from "@testing-library/react";
import { Mock, vi } from "vitest";
import * as model from "../PipelineConsoleModel.tsx";
import { useStepsPoller } from "./use-steps-poller.ts";
const mockSteps = [
{
id: "step-1",
title: "Step 1",
stageId: "stage-1",
state: "success",
},
{
id: "step-2",
title: "Step 2",
stageId: "stage-2",
state: "running",
},
];
const mockStages = [
{ id: "stage-1", name: "Stage 1", children: [] },
{ id: "stage-2", name: "Stage 2", children: [] },
];
vi.mock("../../../../common/tree-api.ts", () => ({
__esModule: true,
default: vi.fn(() => ({ run: { stages: mockStages, complete: false } })),
}));
vi.mock("../PipelineConsoleModel.tsx", async () => ({
...(await vi.importActual("../PipelineConsoleModel.tsx")),
getRunSteps: vi.fn(),
getConsoleTextOffset: vi.fn().mockResolvedValue({
text: "log line",
startByte: 0,
endByte: 100,
}),
POLL_INTERVAL: 50,
}));
beforeEach(() => {
(model.getRunSteps as Mock).mockResolvedValue({ steps: mockSteps });
window.history.pushState({}, "", "/");
});
afterEach(() => {
vi.clearAllMocks();
});
it("selects default step if URL param is missing", async () => {
const { result, unmount } = renderHook(() =>
useStepsPoller({ currentRunPath: "/run/1", previousRunPath: undefined }),
);
await waitFor(() => expect(result.current.expandedSteps).toContain("step-2"));
const { openStage, expandedSteps } = result.current;
expect(openStage?.id).toBe("stage-2");
expect(expandedSteps).toContain("step-2");
unmount();
});
it("selects the step from URL on initial load", async () => {
window.history.pushState({}, "", "/?selected-node=step-1&start-byte=0");
const { result, unmount } = renderHook(() =>
useStepsPoller({ currentRunPath: "/run/1", previousRunPath: undefined }),
);
await waitFor(() => expect(result.current.expandedSteps).toContain("step-1"));
const { openStage, expandedSteps } = result.current;
expect(openStage?.id).toBe("stage-1");
expect(expandedSteps).toContain("step-1");
unmount();
});
it("selected steps can be collapsed and remain collapsed", async () => {
window.history.pushState({}, "", "/?selected-node=step-1&start-byte=0");
let currentSteps = [
{ id: "step-1", title: "Step 1", stageId: "stage-1", state: "running" },
{ id: "step-2", title: "Step 2", stageId: "stage-1", state: "queued" },
];
(model.getRunSteps as Mock).mockResolvedValue({ steps: currentSteps });
const props = { currentRunPath: "/run/1" };
const { result, unmount } = renderHook(() => useStepsPoller(props));
await waitFor(() => expect(result.current.expandedSteps).toContain("step-1"));
expect(result.current.expandedSteps).toContain("step-1");
act(() => result.current.onStepToggle("step-1"));
expect(result.current.expandedSteps).not.toContain("step-1");
// Simulate step-1 finishing and step-2 becoming active
currentSteps = [
{ id: "step-1", title: "Step 1", stageId: "stage-1", state: "success" },
{ id: "step-2", title: "Step 2", stageId: "stage-1", state: "running" },
];
(model.getRunSteps as Mock).mockResolvedValue({ steps: currentSteps });
await waitFor(() =>
expect(result.current.openStageSteps).to.deep.equal(currentSteps),
);
expect(result.current.expandedSteps).not.toContain("step-1");
unmount();
});
it("switches to next stage when current one finishes", async () => {
let currentSteps = [
{ id: "s1", title: "Step 1", stageId: "stage-1", state: "running" },
{ id: "s2", title: "Step 2", stageId: "stage-2", state: "queued" },
];
(model.getRunSteps as Mock).mockImplementation(() =>
Promise.resolve({ steps: currentSteps }),
);
const { result, unmount } = renderHook(() =>
useStepsPoller({ currentRunPath: "/run/1" }),
);
await waitFor(() => expect(result.current.openStage?.id).toBe("stage-1"));
expect(result.current.expandedSteps).toContain("s1");
// Simulate stage-1 finishing and stage-2 becoming active
currentSteps = [
{ id: "s1", title: "Step 1", stageId: "stage-1", state: "success" },
{ id: "s2", title: "Step 2", stageId: "stage-2", state: "running" },
];
(model.getRunSteps as Mock).mockResolvedValue({ steps: currentSteps });
await waitFor(() => expect(result.current.openStage?.id).toBe("stage-2"));
expect(result.current.expandedSteps).toContain("s2");
unmount();
});
it("expands and collapses step when toggled", async () => {
const { result, unmount } = renderHook(() =>
useStepsPoller({ currentRunPath: "/run/1" }),
);
await waitFor(() => expect(result.current.expandedSteps).toContain("step-2"));
act(() => result.current.onStepToggle("step-2"));
expect(result.current.expandedSteps).not.toContain("step-2");
act(() => result.current.onStepToggle("step-2"));
expect(result.current.expandedSteps).toContain("step-2");
unmount();
});