Skip to content

Commit 23d4dee

Browse files
committed
feat: add test units for Button.tsx
1 parent 46efb99 commit 23d4dee

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

src/components/Button/Button.test.tsx

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from "react";
2+
import { render, screen, fireEvent } from "@testing-library/react";
3+
import { Button } from "./Button";
4+
import { LnbThemeProvider } from "../../contexts/ThemeProvider";
5+
import { lightTheme } from "../../theme/defaultTheme";
6+
import { normalizeHex, rgbToHex } from "../../utils/rgbToHex";
7+
8+
const renderWithTheme = (ui: React.ReactElement) => {
9+
return render(<LnbThemeProvider theme={lightTheme}>{ui}</LnbThemeProvider>);
10+
};
11+
12+
describe("Button Component", () => {
13+
it("deve renderizar o texto do botão", () => {
14+
renderWithTheme(<Button>Enviar</Button>);
15+
expect(screen.getByText("Enviar")).toBeInTheDocument();
16+
});
17+
18+
it("deve disparar o evento onClick quando clicado", () => {
19+
const handleClick = jest.fn();
20+
renderWithTheme(<Button onClick={handleClick}>Clique aqui</Button>);
21+
22+
const button = screen.getByText("Clique aqui");
23+
fireEvent.click(button);
24+
25+
expect(handleClick).toHaveBeenCalledTimes(1);
26+
});
27+
28+
it("deve renderizar o botão desativado quando o atributo disabled for passado", () => {
29+
renderWithTheme(<Button disabled>Desativado</Button>);
30+
const button = screen.getByText("Desativado");
31+
32+
expect(button).toBeDisabled();
33+
});
34+
35+
it("deve aplicar a variante correta", () => {
36+
renderWithTheme(<Button variant="secondary">Secondary</Button>);
37+
const button = screen.getByText("Secondary");
38+
39+
const computedStyle = getComputedStyle(button);
40+
41+
const backgroundColorHex = normalizeHex(rgbToHex(computedStyle.backgroundColor));
42+
const colorHex = normalizeHex(rgbToHex(computedStyle.color));
43+
44+
expect(backgroundColorHex).toBe(normalizeHex(lightTheme.colors.secondary.main));
45+
expect(colorHex).toBe(normalizeHex(lightTheme.colors.secondary.contrast));
46+
});
47+
48+
it("deve aplicar a variante correta", () => {
49+
renderWithTheme(<Button variant="primary">Primary</Button>);
50+
const button = screen.getByText("Primary");
51+
52+
const computedStyle = getComputedStyle(button);
53+
54+
const backgroundColorHex = normalizeHex(rgbToHex(computedStyle.backgroundColor));
55+
const colorHex = normalizeHex(rgbToHex(computedStyle.color));
56+
57+
expect(backgroundColorHex).toBe(normalizeHex(lightTheme.colors.primary.main));
58+
expect(colorHex).toBe(normalizeHex(lightTheme.colors.primary.contrast));
59+
});
60+
});

src/theme/defaultTheme.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export const lightTheme = {
44
text: "#000000",
55
primary: { main: "#dc3545", contrast: "#fff" },
66
secondary: { main: "#6C757D", contrast: "#fff" },
7-
// "#6C757D",
87
border: "#DDDDDD",
98
},
109
spacing: (factor: number) => `${factor * 8}px`,
@@ -18,8 +17,8 @@ export const darkTheme = {
1817
colors: {
1918
background: "#121212",
2019
text: "#ffffff",
21-
primary: "#1E88E5",
22-
secondary: "#8A8A8A",
20+
primary: { main: "#1E88E5", contrast: "#fff" },
21+
secondary: { main: "#8A8A8A", contrast: "#fff" },
2322
border: "#333333",
2423
},
2524
spacing: (factor: number) => `${factor * 8}px`,

src/utils/rgbToHex.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function rgbToHex(rgb: string): string {
2+
const [r, g, b] = rgb
3+
.replace(/^rgba?\(|\s+|\)$/g, "")
4+
.split(",")
5+
.map(Number);
6+
return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
7+
}
8+
9+
export function normalizeHex(color: string): string {
10+
if (color.length === 4) {
11+
return `#${color[1]}${color[1]}${color[2]}${color[2]}${color[3]}${color[3]}`;
12+
}
13+
return color.toLowerCase();
14+
}

0 commit comments

Comments
 (0)