|
| 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 | +}) |
0 commit comments