From 9f6f7bba5502a9b37a4b1a4173c374cc892a596c Mon Sep 17 00:00:00 2001 From: Drikus Roor Date: Thu, 28 Dec 2023 13:53:28 +0100 Subject: [PATCH] test: Refactor FloatingActionButton component and add unit tests --- .../FloatingActionButton.js | 38 ++++++---- .../FloatingActionButton.test.js | 73 +++++++++++++++++++ 2 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 frontend/src/components/FloatingActionButton/FloatingActionButton.test.js diff --git a/frontend/src/components/FloatingActionButton/FloatingActionButton.js b/frontend/src/components/FloatingActionButton/FloatingActionButton.js index fb50f0b5c..8563f2c3f 100644 --- a/frontend/src/components/FloatingActionButton/FloatingActionButton.js +++ b/frontend/src/components/FloatingActionButton/FloatingActionButton.js @@ -21,27 +21,39 @@ const FloatingActionButton = ({ } return (<> -
+
-
+
{children}
-
setExpanded(false)} aria-hidden={expanded ? 'false' : 'true'} role="presentation" diff --git a/frontend/src/components/FloatingActionButton/FloatingActionButton.test.js b/frontend/src/components/FloatingActionButton/FloatingActionButton.test.js new file mode 100644 index 000000000..684c83805 --- /dev/null +++ b/frontend/src/components/FloatingActionButton/FloatingActionButton.test.js @@ -0,0 +1,73 @@ +import React from 'react'; +import { render, fireEvent, screen } from '@testing-library/react'; +import FloatingActionButton from './FloatingActionButton'; + +describe('FloatingActionButton', () => { + it('renders the button with the initial icon', () => { + render(); + const icon = screen.getByTestId('floating-action-button__icon'); + expect(icon).toBeInTheDocument(); + expect(icon).toHaveClass('fa-comment'); + }); + + it('toggles the content on click', () => { + const { getByTestId } = render(
Test Content
); + + const toggleButton = getByTestId('floating-action-button__toggle-button'); + fireEvent.click(toggleButton); + + const content = getByTestId('floating-action-button'); + expect(content).toHaveClass('floating-action-button--expanded'); + + fireEvent.click(toggleButton); + expect(content).not.toHaveClass('floating-action-button--expanded'); + }); + + it('displays the correct icon when expanded', () => { + const { getByTestId } = render(); + + const toggleButton = getByTestId('floating-action-button__toggle-button'); + fireEvent.click(toggleButton); + + const icon = getByTestId('floating-action-button__icon'); + expect(icon).toBeInTheDocument(); + expect(icon).toHaveClass('fa-times'); + }); + + it('closes the expanded content when the overlay is clicked', () => { + const { getByTestId } = render(
Test Content
); + + const toggleButton = getByTestId('floating-action-button__toggle-button'); + fireEvent.click(toggleButton); + + const overlay = getByTestId('floating-action-button__overlay'); + fireEvent.click(overlay); + + const content = getByTestId('floating-action-button__content'); + expect(content).not.toHaveClass('floating-action-button--expanded'); + }); + + it('initially renders in a collapsed state', () => { + render(); + expect(screen.getByTestId('floating-action-button')).not.toHaveClass('floating-action-button--expanded'); + }); + + it('correctly applies position classes', () => { + render(); + expect(screen.getByTestId('floating-action-button')).toHaveClass('floating-action-button--bottom'); + expect(screen.getByTestId('floating-action-button')).toHaveClass('floating-action-button--left'); + }); + + it('applies custom class name', () => { + render(); + expect(screen.getByTestId('floating-action-button')).toHaveClass('custom-class'); + }); + + it('updates aria-hidden attribute of overlay correctly', () => { + render(); + const overlay = screen.getByTestId('floating-action-button__overlay'); + expect(overlay).toHaveAttribute('aria-hidden', 'true'); + fireEvent.click(screen.getByTestId('floating-action-button__toggle-button')); + expect(overlay).toHaveAttribute('aria-hidden', 'false'); + }); +});