From 9ef812063d436cad7a8a5f0b97c25677ddb32c00 Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 26 Sep 2023 12:04:07 +0200 Subject: [PATCH 1/2] feat(chore): add button test --- src/components/button/button.test.tsx | 133 ++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/components/button/button.test.tsx diff --git a/src/components/button/button.test.tsx b/src/components/button/button.test.tsx new file mode 100644 index 00000000..ad21bcf3 --- /dev/null +++ b/src/components/button/button.test.tsx @@ -0,0 +1,133 @@ +import { describe, expect, it, vi } from "vitest"; +import { fireEvent, render, screen } from "@testing-library/react"; +import React from "react"; +import { Button } from "./button"; +import { AddIcon } from "../../icons"; + +describe("Button", () => { + it("renders a button with text and button type", () => { + const text = "Button Type"; + // ARRANGE + render(); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "button"); + }); + + it("renders a button with text and submit type", () => { + const text = "Submit Type"; + // ARRANGE + render( + + ); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "submit"); + }); + + it("renders a button with text and left icon", () => { + const text = "Left icon"; + // ARRANGE + render( + + ); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "button"); + expect(button.firstChild?.nodeName.toLowerCase()).toBe("svg"); + }); + + it("renders a button with text and right icon", () => { + const text = "Right icon"; + // ARRANGE + render( + + ); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "button"); + expect(button.lastChild?.nodeName.toLowerCase()).toBe("svg"); + }); + + it("renders a button with text and onClick", () => { + const text = "Onclick button"; + const mock = vi.fn(); + // ARRANGE + render(); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "button"); + fireEvent.click(button); + expect(mock).toHaveBeenCalledTimes(1); + }); + + it("renders a button with text and disabled state", () => { + const text = "Disabled button"; + const mock = vi.fn(); + // ARRANGE + render( + + ); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "button"); + expect(button).toBeDisabled(); + fireEvent.click(button); + expect(mock).toHaveBeenCalledTimes(0); + }); + + it("renders a button with text and form", () => { + const text = "Form button"; + const mock = vi.fn(); + // ARRANGE + render( + + ); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "submit"); + expect(button).toHaveAttribute("form", "form-test"); + }); + + it("renders a button with loading state", () => { + const text = "Loading button"; + const mock = vi.fn(); + // ARRANGE + render( + + ); + + // ASSERT + const button = screen.getByText(text); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute("type", "button"); + expect(button.firstChild?.nodeName.toLowerCase()).toBe("svg"); + expect(button.firstChild).toHaveClass("animate-spin"); + }); +}); From d2cf5a8d7a60e9ee8e20cc54fb484d7ada084dc2 Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 26 Sep 2023 20:03:30 +0200 Subject: [PATCH 2/2] update github action --- .github/workflows/test.yaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5424a782..9c5d2868 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,15 +1,18 @@ name: Test Components on: - pull_request_target: - types: [opened, reopened] + push: + pull_request: + branches: + - master jobs: run-tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: + fetch-depth: 0 persist-credentials: false - uses: actions/setup-node@v3 with: