|
1 | | -import { render, screen, act } from "@testing-library/react" |
| 1 | +import React from "react" |
| 2 | +import { render, screen } from "@testing-library/react" |
2 | 3 | import RooTips from "../RooTips" |
3 | | -import React from "react" // Import React for JSX types |
4 | 4 |
|
5 | 5 | // Mock the translation hook |
6 | 6 | jest.mock("react-i18next", () => ({ |
7 | 7 | useTranslation: () => ({ |
8 | 8 | t: (key: string) => key, // Simple mock that returns the key |
9 | 9 | }), |
10 | | - Trans: ({ children }: { children: React.ReactNode }) => children, |
| 10 | + // Mock Trans component if it were used directly, but it's not here |
11 | 11 | })) |
12 | 12 |
|
13 | 13 | // Mock VSCodeLink |
14 | 14 | jest.mock("@vscode/webview-ui-toolkit/react", () => ({ |
15 | 15 | VSCodeLink: ({ href, children }: { href: string; children: React.ReactNode }) => <a href={href}>{children}</a>, |
16 | 16 | })) |
17 | 17 |
|
| 18 | +// Mock clsx if complex class logic needs specific testing (optional) |
| 19 | +// jest.mock('clsx'); |
| 20 | + |
18 | 21 | describe("RooTips Component", () => { |
19 | 22 | beforeEach(() => { |
20 | 23 | jest.useFakeTimers() |
| 24 | + // Reset Math.random mock for consistent starting points if needed |
| 25 | + // jest.spyOn(global.Math, 'random').mockReturnValue(0); // Example: always start with the first tip |
21 | 26 | }) |
22 | 27 |
|
23 | 28 | afterEach(() => { |
24 | 29 | jest.runOnlyPendingTimers() |
25 | 30 | jest.useRealTimers() |
| 31 | + // Restore Math.random if mocked |
| 32 | + // jest.spyOn(global.Math, 'random').mockRestore(); |
26 | 33 | }) |
27 | 34 |
|
28 | | - test("renders and cycles through tips by default (cycle=true)", () => { |
29 | | - render(<RooTips />) |
30 | | - |
31 | | - // Initial render (random tip) - check if one tip is rendered |
32 | | - // We check for the link text pattern as the description is included |
33 | | - expect(screen.getByRole("link", { name: /rooTips\..*\.title/i })).toBeInTheDocument() |
34 | | - expect(screen.getAllByRole("link")).toHaveLength(1) |
35 | | - |
36 | | - // Fast-forward time to trigger the interval + fade timeout |
37 | | - act(() => { |
38 | | - jest.advanceTimersByTime(11000 + 1000) // interval + fade duration |
| 35 | + describe("when cycle is false (default)", () => { |
| 36 | + beforeEach(() => { |
| 37 | + render(<RooTips cycle={false} />) |
39 | 38 | }) |
40 | 39 |
|
41 | | - // After interval, a different tip should be potentially rendered (still one tip) |
42 | | - // Note: Due to random start, we can't guarantee a *different* tip if there are only 2, |
43 | | - // but the core logic is that it attempts to cycle. We re-check the structure. |
44 | | - expect(screen.getByRole("link", { name: /rooTips\..*\.title/i })).toBeInTheDocument() |
45 | | - expect(screen.getAllByRole("link")).toHaveLength(1) |
46 | | - }) |
47 | | - |
48 | | - test("renders only the top two tips when cycle is false", () => { |
49 | | - render(<RooTips cycle={false} />) |
50 | | - |
51 | | - // Check if the first two tips are rendered |
52 | | - expect(screen.getByRole("link", { name: "rooTips.boomerangTasks.title" })).toBeInTheDocument() |
53 | | - expect(screen.getByText("rooTips.boomerangTasks.description")).toBeInTheDocument() |
54 | | - expect(screen.getByRole("link", { name: "rooTips.stickyModels.title" })).toBeInTheDocument() |
55 | | - expect(screen.getByText("rooTips.stickyModels.description")).toBeInTheDocument() |
56 | | - |
57 | | - // Ensure only two tips are present |
58 | | - expect(screen.getAllByRole("link")).toHaveLength(2) |
59 | | - |
60 | | - // Check that the third tip is not rendered |
61 | | - expect(screen.queryByRole("link", { name: "rooTips.tools.title" })).not.toBeInTheDocument() |
62 | | - |
63 | | - // Fast-forward time - nothing should change |
64 | | - act(() => { |
65 | | - jest.advanceTimersByTime(12000) |
| 40 | + test("renders only the top two tips", () => { |
| 41 | + // Ensure only two tips are present (check by link role) |
| 42 | + expect(screen.getAllByRole("link")).toHaveLength(2) |
66 | 43 | }) |
67 | | - |
68 | | - // Verify the state remains the same (still top two tips) |
69 | | - expect(screen.getByRole("link", { name: "rooTips.boomerangTasks.title" })).toBeInTheDocument() |
70 | | - expect(screen.getByRole("link", { name: "rooTips.stickyModels.title" })).toBeInTheDocument() |
71 | | - expect(screen.getAllByRole("link")).toHaveLength(2) |
72 | | - expect(screen.queryByRole("link", { name: "rooTips.tools.title" })).not.toBeInTheDocument() |
73 | 44 | }) |
74 | 45 | }) |
0 commit comments