|
1
|
import React from "react" |
|
2
|
import { describe, expect, test, vi } from "vitest" |
|
3
|
import { fireEvent, render } from "@testing-library/react" |
|
4
|
import { RadioItem } from "../index" |
|
5
|
|
|
6
|
describe("render", () => { |
|
7
|
test("renders radio item", () => { |
|
8
|
const { container } = render(<RadioItem />) |
|
9
|
expect(container).toBeInTheDocument() |
|
10
|
const radioItem = container.querySelector("input") |
|
11
|
expect(radioItem).toBeInTheDocument() |
|
12
|
expect(radioItem).toHaveAttribute("type", "radio") |
|
13
|
expect(radioItem?.checked).toBeFalsy() |
|
14
|
expect(radioItem?.disabled).toBeFalsy() |
|
15
|
}) |
|
16
|
|
|
17
|
test("renders radio item with checked", () => { |
|
18
|
const { container } = render(<RadioItem checked />) |
|
19
|
expect(container).toBeInTheDocument() |
|
20
|
const radioItem = container.querySelector("input") |
|
21
|
expect(radioItem).toBeInTheDocument() |
|
22
|
expect(radioItem?.checked).toBeTruthy() |
|
23
|
}) |
|
24
|
|
|
25
|
test("renders indicator when checked", () => { |
|
26
|
const { container } = render(<RadioItem checked />) |
|
27
|
expect(container).toBeInTheDocument() |
|
28
|
const radioItemCheckedIndicator = container.querySelector( |
|
29
|
"[data-testid='radio-item-checked-indicator']" |
|
30
|
) |
|
31
|
expect(radioItemCheckedIndicator).toBeInTheDocument() |
|
32
|
}) |
|
33
|
|
|
34
|
test("doesn't render indicator when not checked", () => { |
|
35
|
const { container } = render(<RadioItem />) |
|
36
|
expect(container).toBeInTheDocument() |
|
37
|
const radioItemCheckedIndicator = container.querySelector( |
|
38
|
"[data-testid='radio-item-checked-indicator']" |
|
39
|
) |
|
40
|
expect(radioItemCheckedIndicator).not.toBeInTheDocument() |
|
41
|
}) |
|
42
|
|
|
43
|
test("renders radio item with disabled", () => { |
|
44
|
const { container } = render(<RadioItem disabled />) |
|
45
|
expect(container).toBeInTheDocument() |
|
46
|
const radioItem = container.querySelector("input") |
|
47
|
expect(radioItem).toBeInTheDocument() |
|
48
|
expect(radioItem?.disabled).toBeTruthy() |
|
49
|
}) |
|
50
|
|
|
51
|
test("renders radio item with name", () => { |
|
52
|
const { container } = render(<RadioItem name="radio" />) |
|
53
|
expect(container).toBeInTheDocument() |
|
54
|
const radioItem = container.querySelector("input") |
|
55
|
expect(radioItem).toBeInTheDocument() |
|
56
|
expect(radioItem).toHaveAttribute("name", "radio") |
|
57
|
}) |
|
58
|
|
|
59
|
test("renders radio item with value", () => { |
|
60
|
const { container } = render(<RadioItem value="radio" />) |
|
61
|
expect(container).toBeInTheDocument() |
|
62
|
const radioItem = container.querySelector("input") |
|
63
|
expect(radioItem).toBeInTheDocument() |
|
64
|
expect(radioItem).toHaveAttribute("value", "radio") |
|
65
|
}) |
|
66
|
}) |
|
67
|
|
|
68
|
describe("interactions", () => { |
|
69
|
test("calls onClick when radio item is clicked", () => { |
|
70
|
const mockOnClick = vi.fn() |
|
71
|
const { container } = render(<RadioItem onClick={mockOnClick} />) |
|
72
|
const radioItem = container.querySelector("input") |
|
73
|
expect(radioItem).toBeInTheDocument() |
|
74
|
fireEvent.click(radioItem!) |
|
75
|
expect(mockOnClick).toHaveBeenCalledTimes(1) |
|
76
|
}) |
|
77
|
}) |