|
| 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 | +}); |
0 commit comments