|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
|
|
5
|
// mock hooks |
|
6
|
const mockColorMode = "light" |
|
7
|
|
|
8
|
const defaultUseColorModeReturn = { |
|
9
|
colorMode: mockColorMode, |
|
10
|
} |
|
11
|
|
|
12
|
const mockUseColorMode = vi.fn(() => defaultUseColorModeReturn) |
|
13
|
|
|
14
|
vi.mock("@/providers/ColorMode", () => ({ |
|
15
|
useColorMode: () => mockUseColorMode(), |
|
16
|
})) |
|
17
|
|
|
18
|
import { ThemeImage } from "../../ThemeImage" |
|
19
|
|
|
20
|
beforeEach(() => { |
|
21
|
vi.clearAllMocks() |
|
22
|
mockUseColorMode.mockReturnValue(defaultUseColorModeReturn) |
|
23
|
}) |
|
24
|
|
|
25
|
describe("rendering", () => { |
|
26
|
test("renders image", () => { |
|
27
|
const { container } = render(<ThemeImage light="/light.png" />) |
|
28
|
const img = container.querySelector("img") |
|
29
|
expect(img).toBeInTheDocument() |
|
30
|
}) |
|
31
|
|
|
32
|
test("renders light image when colorMode is light", () => { |
|
33
|
mockUseColorMode.mockReturnValue({ colorMode: "light" }) |
|
34
|
const { container } = render(<ThemeImage light="/light.png" />) |
|
35
|
const img = container.querySelector("img") |
|
36
|
expect(img).toHaveAttribute("src", "/light.png") |
|
37
|
}) |
|
38
|
|
|
39
|
test("renders dark image when colorMode is dark and dark prop is provided", () => { |
|
40
|
mockUseColorMode.mockReturnValue({ colorMode: "dark" }) |
|
41
|
const { container } = render( |
|
42
|
<ThemeImage light="/light.png" dark="/dark.png" /> |
|
43
|
) |
|
44
|
const img = container.querySelector("img") |
|
45
|
expect(img).toHaveAttribute("src", "/dark.png") |
|
46
|
}) |
|
47
|
|
|
48
|
test("renders light image when colorMode is dark but dark prop is not provided", () => { |
|
49
|
mockUseColorMode.mockReturnValue({ colorMode: "dark" }) |
|
50
|
const { container } = render(<ThemeImage light="/light.png" />) |
|
51
|
const img = container.querySelector("img") |
|
52
|
expect(img).toHaveAttribute("src", "/light.png") |
|
53
|
}) |
|
54
|
|
|
55
|
test("renders alt text", () => { |
|
56
|
const { container } = render( |
|
57
|
<ThemeImage light="/light.png" alt="Test Image" /> |
|
58
|
) |
|
59
|
const img = container.querySelector("img") |
|
60
|
expect(img).toHaveAttribute("alt", "Test Image") |
|
61
|
}) |
|
62
|
|
|
63
|
test("passes through other props", () => { |
|
64
|
const { container } = render( |
|
65
|
<ThemeImage light="/light.png" className="custom-class" width={100} /> |
|
66
|
) |
|
67
|
const img = container.querySelector("img") |
|
68
|
expect(img).toHaveClass("custom-class") |
|
69
|
expect(img).toHaveAttribute("width", "100") |
|
70
|
}) |
|
71
|
}) |