Skip to content

Commit 2c6ab3b

Browse files
committed
test: add React test files and update vitest setup
1 parent bdd3d70 commit 2c6ab3b

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { describe, it, expect, beforeEach, vi } from "vitest"
2+
import { render, screen, fireEvent, waitFor } from "@testing-library/react"
3+
import { renderHook } from "@testing-library/react"
4+
import { ReactNode } from "react"
5+
import React from "react"
6+
7+
// Mock des composants VSCode
8+
vi.mock("@vscode/webview-ui-toolkit/react", () => ({
9+
VSCodeDropdown: ({ children, ...props }: any) => (
10+
<select data-testid="vscode-dropdown" {...props}>
11+
{children}
12+
</select>
13+
),
14+
VSCodeOption: ({ children, ...props }: any) => (
15+
<option data-testid="vscode-option" {...props}>
16+
{children}
17+
</option>
18+
),
19+
VSCodeButton: ({ children, onClick, ...props }: any) => (
20+
<button data-testid="vscode-button" onClick={onClick} {...props}>
21+
{children}
22+
</button>
23+
),
24+
VSCodeTextArea: ({ value, onChange, ...props }: any) => (
25+
<textarea data-testid="vscode-textarea" value={value} onChange={(e) => onChange(e.target.value)} {...props} />
26+
),
27+
VSCodeCheckbox: ({ checked, onChange, ...props }: any) => (
28+
<input
29+
type="checkbox"
30+
data-testid="vscode-checkbox"
31+
checked={checked}
32+
onChange={(e) => onChange(e.target.checked)}
33+
{...props}
34+
/>
35+
),
36+
useVSCodeState: vi.fn(() => [{}, vi.fn()]),
37+
}))
38+
39+
// Provider de test pour le contexte
40+
const TestProvider = ({ children }: { children: ReactNode }) => {
41+
return <div data-testid="test-provider">{children}</div>
42+
}
43+
44+
describe("React Context and Component Testing", () => {
45+
beforeEach(() => {
46+
vi.clearAllMocks()
47+
})
48+
49+
it("should render components with proper context initialization", () => {
50+
const TestComponent = () => (
51+
<TestProvider>
52+
<div data-testid="test-content">Test Content</div>
53+
</TestProvider>
54+
)
55+
56+
render(<TestComponent />)
57+
58+
expect(screen.getByTestId("test-provider")).toBeInTheDocument()
59+
expect(screen.getByTestId("test-content")).toBeInTheDocument()
60+
expect(screen.getByText("Test Content")).toBeInTheDocument()
61+
})
62+
63+
it("should handle VSCode component interactions", async () => {
64+
const mockOnClick = vi.fn()
65+
const mockOnChange = vi.fn()
66+
67+
// Importer les composants mockés
68+
const { VSCodeButton, VSCodeTextArea, VSCodeCheckbox } = await import("@vscode/webview-ui-toolkit/react")
69+
70+
const TestComponent = () => (
71+
<div>
72+
<VSCodeButton onClick={mockOnClick}>Click me</VSCodeButton>
73+
<VSCodeTextArea value="test" onChange={mockOnChange} />
74+
<VSCodeCheckbox checked={false} onChange={mockOnChange} />
75+
</div>
76+
)
77+
78+
render(<TestComponent />)
79+
80+
// Test button click
81+
const button = screen.getByTestId("vscode-button")
82+
fireEvent.click(button)
83+
expect(mockOnClick).toHaveBeenCalled()
84+
85+
// Test textarea change
86+
const textarea = screen.getByTestId("vscode-textarea")
87+
fireEvent.change(textarea, { target: { value: "new value" } })
88+
expect(mockOnChange).toHaveBeenCalledWith("new value")
89+
90+
// Test checkbox change
91+
const checkbox = screen.getByTestId("vscode-checkbox")
92+
fireEvent.click(checkbox)
93+
expect(mockOnChange).toHaveBeenCalledWith(true)
94+
})
95+
96+
it("should handle async operations and state updates", async () => {
97+
const mockAsyncFn = vi.fn().mockResolvedValue("async result")
98+
99+
const TestComponent = () => {
100+
const [data, setData] = React.useState<string>("")
101+
102+
React.useEffect(() => {
103+
mockAsyncFn().then(setData)
104+
}, [])
105+
106+
return <div data-testid="async-result">{data}</div>
107+
}
108+
109+
render(<TestComponent />)
110+
111+
// Wait for async operation
112+
await waitFor(() => {
113+
expect(screen.getByTestId("async-result")).toHaveTextContent("async result")
114+
})
115+
116+
expect(mockAsyncFn).toHaveBeenCalled()
117+
})
118+
119+
it("should test custom hooks with renderHook", () => {
120+
const useCustomHook = (initialValue: string) => {
121+
const [value, setValue] = React.useState(initialValue)
122+
const updateValue = React.useCallback((newValue: string) => {
123+
setValue(newValue.toUpperCase())
124+
}, [])
125+
126+
return { value, setValue: updateValue }
127+
}
128+
129+
const { result } = renderHook(() => useCustomHook("test"))
130+
131+
expect(result.current.value).toBe("test")
132+
133+
// Test the callback
134+
result.current.setValue("new test")
135+
expect(result.current.value).toBe("NEW TEST")
136+
})
137+
})
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, it, expect, vi, beforeAll } from "vitest"
2+
import { render } from "@testing-library/react"
3+
import React from "react"
4+
5+
// Mock des composants VSCode
6+
vi.mock("@vscode/webview-ui-toolkit/react", () => ({
7+
VSCodeButton: ({ children, ...props }: any) => (
8+
<button data-testid="vscode-button" {...props}>
9+
{children}
10+
</button>
11+
),
12+
}))
13+
14+
describe("Snapshot Tests", () => {
15+
beforeAll(() => {
16+
// Initialize snapshot environment
17+
console.log("Initializing snapshot tests...")
18+
})
19+
it("should match snapshot for basic component", () => {
20+
const TestComponent = () => (
21+
<div data-testid="test-component">
22+
<h1>Test Title</h1>
23+
<p>Test Content</p>
24+
</div>
25+
)
26+
27+
const { container } = render(<TestComponent />)
28+
29+
expect(container.firstChild).toMatchSnapshot()
30+
})
31+
32+
it("should match snapshot for VSCode component", () => {
33+
const TestComponent = () => <div data-testid="vscode-button-test">Snapshot Test</div>
34+
35+
const { container } = render(<TestComponent />)
36+
37+
expect(container.firstChild).toMatchSnapshot()
38+
})
39+
40+
it("should match snapshot for complex component structure", () => {
41+
const TestComponent = () => (
42+
<div data-testid="complex-component">
43+
<header>
44+
<nav>
45+
<ul>
46+
<li>Item 1</li>
47+
<li>Item 2</li>
48+
</ul>
49+
</nav>
50+
</header>
51+
<main>
52+
<section>
53+
<h2>Section Title</h2>
54+
<div>
55+
<p>Section content</p>
56+
<button>Action</button>
57+
</div>
58+
</section>
59+
</main>
60+
</div>
61+
)
62+
63+
const { container } = render(<TestComponent />)
64+
65+
expect(container.firstChild).toMatchSnapshot()
66+
})
67+
})

webview-ui/vitest.setup.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import "@testing-library/jest-dom/vitest"
55
// Configure snapshot serialization for better test stability
66
import { expect } from "vitest"
77

8+
// Initialize snapshot client for Vitest 4.x
9+
// Note: SnapshotClient doesn't exist in Vitest 4.x, using alternative approach
10+
811
// Custom snapshot serializer for React components
912
expect.addSnapshotSerializer({
1013
test: (val) => val && typeof val === "object" && val.$$typeof === Symbol.for("react.element"),

0 commit comments

Comments
 (0)