-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathButton.test.ts
More file actions
192 lines (168 loc) · 5.58 KB
/
Button.test.ts
File metadata and controls
192 lines (168 loc) · 5.58 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
import { createRawSnippet, tick } from "svelte";
import { expect } from "@open-wc/testing";
import { render, screen } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import sinon from "sinon";
import Button from "./Button.svelte";
const children = createRawSnippet(() => ({
render: () => "<span>test btn</span>",
}));
describe("Button", () => {
it("should render the button", () => {
render(Button, { children });
expect(screen.getByRole("button")).to.have.text("test btn");
});
it("should render as an anchor when href is provided", () => {
render(Button, { href: "#", children });
expect(screen.getByRole("link")).to.have.text("test btn");
});
it("should render the badge within the button", () => {
render(Button, {
children,
badge: "123",
});
render(Button, {
children,
badge: "456",
});
// Default button
expect(screen.getByText("123")).to.exist;
// Button rendered as anchor
expect(screen.getByText("456")).to.exist;
});
it("should allocate the correct spacing between button text and badge", () => {
render(Button, {
children: createRawSnippet(() => ({
render: () => "<span>test</span>",
})),
badge: "198",
});
expect(screen.getByRole("button")).to.have.text("test 198");
});
// props
it("should render the button with artbirary classes", () => {
render(Button, {
class: "fc-theme-primary-400",
children,
});
expect(screen.getByRole("button")).to.have.class(
"fc-theme-primary-400"
);
});
it("should render the button with type of 'submit'", () => {
render(Button, {
type: "submit",
children,
});
expect(screen.getByRole("button")).to.have.attribute("type", "submit");
});
it("should render the appropriate brand class", () => {
render(Button, {
branding: "github",
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__github");
});
it("should render the appropriate size class", () => {
render(Button, {
size: "xs",
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__xs");
});
it("should render the appropriate variant class", () => {
render(Button, {
variant: "tonal",
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__tonal");
});
it("should render the appropriate weight class", () => {
render(Button, {
weight: "clear",
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__clear");
});
it("should render including the dropdown class", () => {
render(Button, {
dropdown: true,
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__dropdown");
});
it("should render including the icon class", () => {
render(Button, {
icon: true,
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__icon");
});
it("should render including the link class", () => {
render(Button, {
link: true,
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__link");
});
it("should render the loader component when loading prop is provided", () => {
render(Button, {
loading: true,
children,
});
expect(screen.getByText("Loading…")).to.exist;
expect(screen.getByText("Loading…").closest(".s-loader")).to.exist;
});
it("should render including the selected class", () => {
render(Button, {
selected: true,
children,
});
expect(screen.getByRole("button")).to.have.class("is-selected");
});
it("should render including the unset class", () => {
render(Button, {
unset: true,
children,
});
expect(screen.getByRole("button")).to.have.class("s-btn__unset");
});
it("should render with the disabled attribute", () => {
render(Button, {
disabled: true,
children,
});
expect(screen.getByRole("button")).to.have.attribute("disabled");
});
it("should render with the aria-disabled attribute", () => {
render(Button, {
href: "#",
disabled: true,
children,
});
expect(screen.getByRole("link")).to.have.attribute(
"aria-disabled",
"true"
);
});
it("should adjust the classes on prop updates", async () => {
const { rerender } = render(Button, {
class: "fc-theme-primary-400",
children,
});
rerender({ class: "fc-theme-secondary-400" });
await tick();
expect(screen.getByRole("button")).not.to.have.class(
"fc-theme-primary-400"
);
expect(screen.getByRole("button")).to.have.class(
"fc-theme-secondary-400"
);
});
it("should call the onclick call back when the user click on it", async () => {
const onClickSpy = sinon.spy();
render(Button, { onclick: onClickSpy, children });
await userEvent.click(screen.getByRole("button"));
expect(onClickSpy).to.have.been.calledOnce;
});
});