This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathLoadingView.spec.tsx
More file actions
100 lines (81 loc) · 2.7 KB
/
Copy pathLoadingView.spec.tsx
File metadata and controls
100 lines (81 loc) · 2.7 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
// npx vitest run src/components/__tests__/LoadingView.spec.tsx
import React from "react"
import { render, screen, act } from "@testing-library/react"
import LoadingView from "../LoadingView"
vi.mock("@src/utils/vscode", () => ({
vscode: {
postMessage: vi.fn(),
},
}))
vi.mock("@src/i18n/TranslationContext", () => ({
useAppTranslation: () => ({
t: (key: string) => {
const translations: Record<string, string> = {
"common:ui.initializing": "Initializing...",
"common:ui.retry_connection": "Retry Connection",
"common:ui.connection_failed":
"Unable to connect to the extension host. Click the button below to retry.",
}
return translations[key] ?? key
},
}),
}))
describe("LoadingView", () => {
beforeEach(() => {
vi.clearAllMocks()
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
it("renders a spinner and initializing text", () => {
render(<LoadingView />)
expect(screen.getByText("Initializing...")).toBeInTheDocument()
})
it("does not show retry button initially", () => {
render(<LoadingView />)
expect(screen.queryByText("Retry Connection")).not.toBeInTheDocument()
})
it("retries webviewDidLaunch after timeout", async () => {
const { vscode } = await import("@src/utils/vscode")
render(<LoadingView />)
// Advance past the first retry interval (5s)
act(() => {
vi.advanceTimersByTime(5_000)
})
expect(vscode.postMessage).toHaveBeenCalledWith({ type: "webviewDidLaunch" })
})
it("shows retry button after max retries", async () => {
render(<LoadingView />)
// Advance through all 3 retries (5s each) + one more to trigger the button
for (let i = 0; i < 4; i++) {
act(() => {
vi.advanceTimersByTime(5_000)
})
}
expect(screen.getByText("Retry Connection")).toBeInTheDocument()
expect(
screen.getByText("Unable to connect to the extension host. Click the button below to retry."),
).toBeInTheDocument()
})
it("allows manual retry when button is clicked", async () => {
const { vscode } = await import("@src/utils/vscode")
render(<LoadingView />)
// Advance through all retries to show the button
for (let i = 0; i < 4; i++) {
act(() => {
vi.advanceTimersByTime(5_000)
})
}
const calls = (vscode.postMessage as ReturnType<typeof vi.fn>).mock.calls.length
const retryButton = screen.getByText("Retry Connection")
act(() => {
retryButton.click()
})
// Should have sent another webviewDidLaunch
expect((vscode.postMessage as ReturnType<typeof vi.fn>).mock.calls.length).toBeGreaterThan(calls)
expect(vscode.postMessage).toHaveBeenCalledWith({ type: "webviewDidLaunch" })
// Should go back to showing the spinner
expect(screen.getByText("Initializing...")).toBeInTheDocument()
})
})