forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.test.tsx
More file actions
246 lines (210 loc) · 9.17 KB
/
Copy pathButton.test.tsx
File metadata and controls
246 lines (210 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// src/components/ui/__tests__/Button.test.tsx
import { render } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { describe, expect, it, vi } from "vitest"
import { Button } from "@/components/ui/Button"
// ---------------------------------------------------------------------------
// default type attribute
// ---------------------------------------------------------------------------
describe("Button type attribute", () => {
it("defaults to type='button'", () => {
const { getByRole } = render(<Button>Click</Button>)
expect(getByRole("button").getAttribute("type")).toBe("button")
})
it("can be explicitly set to type='submit'", () => {
const { getByRole } = render(<Button type="submit">Submit</Button>)
expect(getByRole("button").getAttribute("type")).toBe("submit")
})
it("can be explicitly set to type='reset'", () => {
const { getByRole } = render(<Button type="reset">Reset</Button>)
expect(getByRole("button").getAttribute("type")).toBe("reset")
})
})
// ---------------------------------------------------------------------------
// text prop and children priority
// ---------------------------------------------------------------------------
describe("Button text prop and children", () => {
it("renders the text prop as content when no children provided", () => {
const { getByRole } = render(<Button text="Save changes" />)
expect(getByRole("button").textContent).toContain("Save changes")
})
it("renders children when provided and no text prop", () => {
const { getByRole } = render(<Button>Child content</Button>)
expect(getByRole("button").textContent).toContain("Child content")
})
it("children take priority over text when both are provided", () => {
const { getByRole } = render(<Button text="text prop">children content</Button>)
const btn = getByRole("button")
expect(btn.textContent).toContain("children content")
expect(btn.textContent).not.toContain("text prop")
})
})
// ---------------------------------------------------------------------------
// icon props
// ---------------------------------------------------------------------------
describe("Button icon props", () => {
it("renders leftIcon before content", () => {
const { getByRole, getByTestId } = render(
<Button leftIcon={<span data-testid="left-icon">L</span>}>Label</Button>
)
const btn = getByRole("button")
const icon = getByTestId("left-icon")
const label = btn.lastChild as ChildNode
// icon node should come before the text node
expect(btn.contains(icon)).toBe(true)
expect(btn.firstChild).toBe(icon)
expect(label.textContent).toBe("Label")
})
it("renders rightIcon after content", () => {
const { getByRole, getByTestId } = render(
<Button rightIcon={<span data-testid="right-icon">R</span>}>Label</Button>
)
const btn = getByRole("button")
const icon = getByTestId("right-icon")
expect(btn.contains(icon)).toBe(true)
expect(btn.lastChild).toBe(icon)
})
it("renders both leftIcon and rightIcon simultaneously", () => {
const { getByTestId } = render(
<Button
leftIcon={<span data-testid="left-icon">L</span>}
rightIcon={<span data-testid="right-icon">R</span>}
>
Label
</Button>
)
expect(getByTestId("left-icon")).toBeDefined()
expect(getByTestId("right-icon")).toBeDefined()
})
})
// ---------------------------------------------------------------------------
// loading state
// ---------------------------------------------------------------------------
describe("Button loading prop", () => {
it("disables the button when loading=true", () => {
const { getByRole } = render(<Button loading>Saving</Button>)
expect((getByRole("button") as HTMLButtonElement).disabled).toBe(true)
})
it("sets aria-busy='true' when loading=true", () => {
const { getByRole } = render(<Button loading>Saving</Button>)
expect(getByRole("button").getAttribute("aria-busy")).toBe("true")
})
it("does not set aria-busy when loading=false", () => {
const { getByRole } = render(<Button>Idle</Button>)
expect(getByRole("button").getAttribute("aria-busy")).toBeNull()
})
it("shows SpinnerIcon (svg) when loading=true", () => {
const { getByRole } = render(<Button loading>Saving</Button>)
const btn = getByRole("button")
const svg = btn.querySelector("svg")
expect(svg).not.toBeNull()
})
it("replaces leftIcon with SpinnerIcon when loading=true", () => {
const { getByRole, queryByTestId } = render(
<Button loading leftIcon={<span data-testid="left-icon">L</span>}>
Saving
</Button>
)
const btn = getByRole("button")
const svg = btn.querySelector("svg")
expect(svg).not.toBeNull()
expect(queryByTestId("left-icon")).toBeNull()
})
it("SpinnerIcon has animate-spin class when loading=true", () => {
const { getByRole } = render(<Button loading>Saving</Button>)
const svg = getByRole("button").querySelector("svg")
// SVG elements expose className as SVGAnimatedString; read via getAttribute
expect(svg?.getAttribute("class")).toContain("animate-spin")
})
})
// ---------------------------------------------------------------------------
// size="icon"
// ---------------------------------------------------------------------------
describe("Button size='icon'", () => {
it("applies p-1.5 when size='icon'", () => {
const { getByRole } = render(<Button size="icon">i</Button>)
expect(getByRole("button").className).toContain("p-1.5")
})
it("applies rounded-nm-sm when size='icon'", () => {
const { getByRole } = render(<Button size="icon">i</Button>)
expect(getByRole("button").className).toContain("rounded-nm-sm")
})
})
// ---------------------------------------------------------------------------
// variants
// ---------------------------------------------------------------------------
describe("Button variants render without error", () => {
const variants = ["primary", "secondary", "ghost", "danger", "minimal"] as const
// Distinguishing class for each variant, derived from the CVA definition in Button.tsx.
// These are checked in addition to the render-crash guard (getByRole) so that a
// variant silently losing its class is caught even if the component still mounts.
const variantClass: Record<(typeof variants)[number], string> = {
primary: "bg-accent-dim",
secondary: "bg-raised",
ghost: "text-secondary", // bg-transparent is shared with minimal; text-secondary is unique to ghost
danger: "bg-danger-dim",
minimal: "text-muted", // bg-transparent is shared with ghost; text-muted is unique to minimal
}
for (const variant of variants) {
it(`renders variant='${variant}'`, () => {
const { getByRole } = render(<Button variant={variant}>Label</Button>)
const btn = getByRole("button")
expect(btn).toBeDefined()
expect(btn.className).toContain(variantClass[variant])
})
}
})
// ---------------------------------------------------------------------------
// disabled prop
// ---------------------------------------------------------------------------
describe("Button disabled prop", () => {
it("is not disabled by default", () => {
const { getByRole } = render(<Button>Click</Button>)
expect((getByRole("button") as HTMLButtonElement).disabled).toBe(false)
})
it("disables the button when disabled=true", () => {
const { getByRole } = render(<Button disabled>Click</Button>)
expect((getByRole("button") as HTMLButtonElement).disabled).toBe(true)
})
})
// ---------------------------------------------------------------------------
// onClick handler
// ---------------------------------------------------------------------------
describe("Button onClick", () => {
it("fires onClick when clicked", async () => {
const user = userEvent.setup()
const handler = vi.fn()
const { getByRole } = render(<Button onClick={handler}>Click</Button>)
await user.click(getByRole("button"))
expect(handler).toHaveBeenCalledTimes(1)
})
it("does not fire onClick when disabled", async () => {
const user = userEvent.setup()
const handler = vi.fn()
const { getByRole } = render(
<Button disabled onClick={handler}>
Click
</Button>
)
await user.click(getByRole("button"))
expect(handler).not.toHaveBeenCalled()
})
})
// ---------------------------------------------------------------------------
// className is appended (not overriding base classes)
// ---------------------------------------------------------------------------
describe("Button className prop", () => {
it("appends the custom className alongside base classes", () => {
const { getByRole } = render(<Button className="my-custom-class">Click</Button>)
const cls = getByRole("button").className
expect(cls).toContain("my-custom-class")
// base classes from CVA should still be present
expect(cls).toContain("inline-flex")
})
it("base classes remain when className is provided", () => {
const { getByRole } = render(<Button className="override-attempt">Click</Button>)
const cls = getByRole("button").className
expect(cls).toContain("items-center")
expect(cls).toContain("justify-center")
})
})