From 9d338411489a0112436f05abd46d473c09b7a371 Mon Sep 17 00:00:00 2001 From: marcaufderheyde Date: Thu, 19 Sep 2024 18:34:32 +0200 Subject: [PATCH] CAN-143 - Drawer Test Suite done --- app/components/Drawer/Drawer.test.tsx | 78 ++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/app/components/Drawer/Drawer.test.tsx b/app/components/Drawer/Drawer.test.tsx index b2f4514..a5f923b 100644 --- a/app/components/Drawer/Drawer.test.tsx +++ b/app/components/Drawer/Drawer.test.tsx @@ -1,3 +1,77 @@ -test('adds 1+2 to equal 3', () => { - expect(1 + 2).toBe(3); +import React, { ReactNode } from 'react'; +import { render, screen, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; + +type DrawerProps = { + isOpen: boolean; + onClose: () => void; + children: ReactNode; + className?: string; +}; + +const MockDrawer: React.FC = ({ + isOpen, + onClose, + children, + className, +}) => { + if (!isOpen) return null; + return ( +
+
+ Close +
+
{children}
+
+ ); +}; + +jest.mock('./Drawer', () => ({ + __esModule: true, + default: jest.fn((props: DrawerProps) => MockDrawer(props)), +})); + +const Drawer = require('./Drawer').default; + +describe('Drawer Component', () => { + const mockOnClose = jest.fn(); + const defaultProps: DrawerProps = { + isOpen: true, + onClose: mockOnClose, + children:
Drawer Content
, + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('renders correctly when open', () => { + render(); + expect(screen.getByTestId('mock-drawer')).toBeInTheDocument(); + expect(screen.getByText('Drawer Content')).toBeInTheDocument(); + }); + + it('does not render when closed', () => { + render(); + expect(screen.queryByTestId('mock-drawer')).not.toBeInTheDocument(); + }); + + it('calls onClose when close button is clicked', () => { + render(); + fireEvent.click(screen.getByTestId('mock-close')); + expect(mockOnClose).toHaveBeenCalled(); + }); + + it('applies custom className', () => { + render(); + expect(screen.getByTestId('mock-drawer')).toHaveClass('custom-class'); + }); + + it('renders children correctly', () => { + const customContent = ( +
Custom Content
+ ); + render({customContent}); + expect(screen.getByTestId('custom-content')).toBeInTheDocument(); + }); });