From d4d22dfa9b68d23513d6d9d6b5618d3e558122a5 Mon Sep 17 00:00:00 2001 From: Jyotsna Mehta <53934353+j24m@users.noreply.github.com> Date: Thu, 5 Oct 2023 10:42:03 +0530 Subject: [PATCH] feat/fix: Button component with tests and a few minor fixes (#22) * feat/fix: Button component with tests and a few minor fixes * Refactor : Improved tests and removed commented code * Refactor : test case * Refactor : default props, test * Refactor : added disabled prop to button component --------- Co-authored-by: Sunny Sahsi --- __tests__/components/Button.test.tsx | 38 ++++++++++++++++++++++++++++ public/assets/icons/add.tsx | 2 +- public/assets/icons/copy.tsx | 6 ++--- src/components/Button/index.tsx | 24 ++++++++++++++++++ src/components/Navbar/Navbar.tsx | 2 +- src/pages/dashboard/index.js | 18 +++++++------ src/pages/login/index.js | 7 ++--- 7 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 __tests__/components/Button.test.tsx create mode 100644 src/components/Button/index.tsx diff --git a/__tests__/components/Button.test.tsx b/__tests__/components/Button.test.tsx new file mode 100644 index 0000000..87a4e91 --- /dev/null +++ b/__tests__/components/Button.test.tsx @@ -0,0 +1,38 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import { Button } from '@/components/Button'; +Button; + +describe('Button', () => { + it('should render Button', () => { + const handleOnClickMock = jest.fn(); + const { getByRole } = render( + + ); + const buttonElement = getByRole('button'); + expect(buttonElement).toHaveAttribute('type', 'button'); + expect(buttonElement).toHaveTextContent('Generate'); + }); + + test('should render button with the type submit', () => { + const handleOnClickMock = jest.fn(); + const { getByRole } = render( + + ); + + const buttonElement = getByRole('button'); + expect(buttonElement).toHaveAttribute('type', 'submit'); + }); +}); diff --git a/public/assets/icons/add.tsx b/public/assets/icons/add.tsx index 69044fa..4f831de 100644 --- a/public/assets/icons/add.tsx +++ b/public/assets/icons/add.tsx @@ -1,7 +1,7 @@ const AddIcon = () => ( Create new - + ( Copy - + diff --git a/src/components/Button/index.tsx b/src/components/Button/index.tsx new file mode 100644 index 0000000..b152099 --- /dev/null +++ b/src/components/Button/index.tsx @@ -0,0 +1,24 @@ +import React from 'react'; + +interface ButtonProps { + type?: 'button' | 'submit' | 'reset'; + className: string; + onClick: (event: React.MouseEvent) => void; + children: React.ReactNode; + disabled?: boolean; +} + +const Button: React.FC = ({ type, className, onClick, children, disabled }) => { + return ( + + ); +}; + +Button.defaultProps = { + type: 'button', + className: 'w-full bg-gray-200 hover:bg-gray-300 ', +}; + +export { Button }; diff --git a/src/components/Navbar/Navbar.tsx b/src/components/Navbar/Navbar.tsx index 2052a2f..6fdf7b5 100644 --- a/src/components/Navbar/Navbar.tsx +++ b/src/components/Navbar/Navbar.tsx @@ -18,7 +18,7 @@ const Navbar: React.FC = () => { URL Shortener -