-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathButton.test.ts
More file actions
259 lines (207 loc) · 6.51 KB
/
Button.test.ts
File metadata and controls
259 lines (207 loc) · 6.51 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
247
248
249
250
251
252
253
254
255
256
257
258
259
import { test, describe, afterEach, expect } from "vitest";
import { cleanup, render, fireEvent } from "@self/tootils/render";
import Button from "./Index.svelte";
const loading_status = {
status: "complete" as const,
eta: 0,
queue_position: 1,
queue_size: 1,
scroll_to_output: false,
visible: true,
fn_index: 0,
show_progress: "full" as const,
type: "input" as const,
stream_state: "closed" as const
};
const default_props = {
value: "Submit",
variant: "primary" as const,
size: "md" as const,
link: null as string | null,
icon: null,
link_target: "_blank" as const,
interactive: true,
loading_status
};
describe("Button", () => {
afterEach(() => cleanup());
test("renders with correct text", async () => {
const { getByText } = await render(Button, {
...default_props
});
expect(getByText("Submit")).toBeTruthy();
});
test("renders with custom value", async () => {
const { getByText } = await render(Button, {
...default_props,
value: "Click Me"
});
expect(getByText("Click Me")).toBeTruthy();
});
test("renders as a button element when no link is provided", async () => {
const { container } = await render(Button, {
...default_props
});
const button = container.querySelector("button");
expect(button).not.toBeNull();
expect(container.querySelector("a")).toBeNull();
});
test("renders as an anchor element when link is provided", async () => {
const { container } = await render(Button, {
...default_props,
link: "https://example.com"
});
const anchor = container.querySelector("a");
expect(anchor).not.toBeNull();
expect(anchor?.getAttribute("href")).toBe("https://example.com");
});
test("anchor has target and rel attributes", async () => {
const { container } = await render(Button, {
...default_props,
link: "https://example.com",
link_target: "_blank" as const
});
const anchor = container.querySelector("a");
expect(anchor?.getAttribute("target")).toBe("_blank");
expect(anchor?.getAttribute("rel")).toBe("noopener noreferrer");
});
test("anchor with _self target has no rel attribute", async () => {
const { container } = await render(Button, {
...default_props,
link: "https://example.com",
link_target: "_self" as const
});
const anchor = container.querySelector("a");
expect(anchor?.getAttribute("target")).toBe("_self");
expect(anchor?.getAttribute("rel")).toBeNull();
});
});
describe("Props: variant", () => {
afterEach(() => cleanup());
test("applies primary variant class", async () => {
const { container } = await render(Button, {
...default_props,
variant: "primary" as const
});
const button = container.querySelector("button");
expect(button?.classList.contains("primary")).toBe(true);
});
test("applies secondary variant class", async () => {
const { container } = await render(Button, {
...default_props,
variant: "secondary" as const
});
const button = container.querySelector("button");
expect(button?.classList.contains("secondary")).toBe(true);
});
test("applies stop variant class", async () => {
const { container } = await render(Button, {
...default_props,
variant: "stop" as const
});
const button = container.querySelector("button");
expect(button?.classList.contains("stop")).toBe(true);
});
});
describe("Props: size", () => {
afterEach(() => cleanup());
test("applies sm size class", async () => {
const { container } = await render(Button, {
...default_props,
size: "sm" as const
});
const button = container.querySelector("button");
expect(button?.classList.contains("sm")).toBe(true);
});
test("applies md size class", async () => {
const { container } = await render(Button, {
...default_props,
size: "md" as const
});
const button = container.querySelector("button");
expect(button?.classList.contains("md")).toBe(true);
});
test("applies lg size class", async () => {
const { container } = await render(Button, {
...default_props,
size: "lg" as const
});
const button = container.querySelector("button");
expect(button?.classList.contains("lg")).toBe(true);
});
});
describe("Props: interactive", () => {
afterEach(() => cleanup());
test("button is enabled when interactive is true", async () => {
const { container } = await render(Button, {
...default_props,
interactive: true
});
const button = container.querySelector("button") as HTMLButtonElement;
expect(button.disabled).toBe(false);
});
test("button is disabled when interactive is false", async () => {
const { container } = await render(Button, {
...default_props,
interactive: false
});
const button = container.querySelector("button") as HTMLButtonElement;
expect(button.disabled).toBe(true);
});
});
describe("Props: visible", () => {
afterEach(() => cleanup());
test("button is visible by default", async () => {
const { container } = await render(Button, {
...default_props
});
const button = container.querySelector("button");
expect(button?.classList.contains("hidden")).toBe(false);
});
test("button is hidden when visible is false", async () => {
const { container } = await render(Button, {
...default_props,
visible: false
});
const button = container.querySelector("button");
expect(button?.classList.contains("hidden")).toBe(true);
});
test("button is hidden when visible is 'hidden'", async () => {
const { container } = await render(Button, {
...default_props,
visible: "hidden"
});
const button = container.querySelector("button");
expect(button?.classList.contains("hidden")).toBe(true);
});
});
describe("Props: elem_id and elem_classes", () => {
afterEach(() => cleanup());
test("elem_id is applied to the button", async () => {
const { container } = await render(Button, {
...default_props,
elem_id: "my-button"
});
expect(container.querySelector("#my-button")).not.toBeNull();
});
test("elem_classes are applied to the button", async () => {
const { container } = await render(Button, {
...default_props,
elem_classes: ["custom-class"]
});
const button = container.querySelector("button");
expect(button?.classList.contains("custom-class")).toBe(true);
});
});
describe("Events: click", () => {
afterEach(() => cleanup());
test("clicking the button fires click event", async () => {
const { container, listen } = await render(Button, {
...default_props
});
const click = listen("click");
const button = container.querySelector("button") as HTMLButtonElement;
await fireEvent.click(button);
expect(click).toHaveBeenCalledTimes(1);
});
});