import { fireEvent, render, screen } from '@testing-library/react' import * as React from 'react' import DrawerPlus from '.' vi.mock('@/hooks/use-breakpoints', () => ({ default: () => 'desktop', MediaType: { mobile: 'mobile', desktop: 'desktop', tablet: 'tablet' }, })) describe('DrawerPlus', () => { beforeEach(() => { vi.clearAllMocks() }) describe('Rendering', () => { it('should not render when isShow is false', () => { render( {}} title="Test Drawer" body={
Content
} />, ) expect(screen.queryByRole('dialog')).not.toBeInTheDocument() }) it('should render when isShow is true', () => { const bodyContent =
Body Content
render( {}} title="Test Drawer" body={bodyContent} />, ) expect(screen.getByRole('dialog')).toBeInTheDocument() expect(screen.getByText('Test Drawer')).toBeInTheDocument() expect(screen.getByText('Body Content')).toBeInTheDocument() }) it('should render footer when provided', () => { const footerContent =
Footer Content
render( {}} title="Test Drawer" body={
Body
} foot={footerContent} />, ) expect(screen.getByText('Footer Content')).toBeInTheDocument() }) it('should render JSX element as title', () => { const titleElement =

Custom Title

render( {}} title={titleElement} body={
Body
} />, ) expect(screen.getByTestId('custom-title')).toBeInTheDocument() }) it('should render titleDescription when provided', () => { render( {}} title="Test Drawer" titleDescription="Description text" body={
Body
} />, ) expect(screen.getByText('Description text')).toBeInTheDocument() }) it('should not render titleDescription when not provided', () => { render( {}} title="Test Drawer" body={
Body
} />, ) expect(screen.queryByText(/Description/)).not.toBeInTheDocument() }) it('should render JSX element as titleDescription', () => { const descElement = Custom Description render( {}} title="Test" titleDescription={descElement} body={
Body
} />, ) expect(screen.getByTestId('custom-desc')).toBeInTheDocument() }) }) describe('Props - Display Options', () => { it('should apply default maxWidthClassName', () => { render( {}} title="Test" body={
Body
} />, ) const innerPanel = screen.getByText('Test').closest('.bg-components-panel-bg') const outerPanel = innerPanel?.parentElement expect(outerPanel?.className).toContain('!max-w-[640px]') }) it('should apply custom maxWidthClassName', () => { render( {}} title="Test" body={
Body
} maxWidthClassName="!max-w-[800px]" />, ) const innerPanel = screen.getByText('Test').closest('.bg-components-panel-bg') const outerPanel = innerPanel?.parentElement expect(outerPanel?.className).toContain('!max-w-[800px]') }) it('should apply custom panelClassName', () => { render( {}} title="Test" body={
Body
} panelClassName="custom-panel" />, ) const innerPanel = screen.getByText('Test').closest('.bg-components-panel-bg') const outerPanel = innerPanel?.parentElement expect(outerPanel?.className).toContain('custom-panel') }) it('should apply custom dialogClassName', () => { render( {}} title="Test" body={
Body
} dialogClassName="custom-dialog" />, ) const dialog = screen.getByRole('dialog') expect(dialog.className).toContain('custom-dialog') }) it('should apply custom contentClassName', () => { render( {}} title="Test" body={
Body
} contentClassName="custom-content" />, ) const title = screen.getByText('Test') const header = title.closest('.shrink-0.border-b.border-divider-subtle') const content = header?.parentElement expect(content?.className).toContain('custom-content') }) it('should apply custom headerClassName', () => { render( {}} title="Test" body={
Body
} headerClassName="custom-header" />, ) const title = screen.getByText('Test') const header = title.closest('.shrink-0.border-b.border-divider-subtle') expect(header?.className).toContain('custom-header') }) it('should apply custom height', () => { render( {}} title="Test" body={
Body
} height="500px" />, ) const title = screen.getByText('Test') const header = title.closest('.shrink-0.border-b.border-divider-subtle') const content = header?.parentElement expect(content?.getAttribute('style')).toContain('height: 500px') }) it('should use default height', () => { render( {}} title="Test" body={
Body
} />, ) const title = screen.getByText('Test') const header = title.closest('.shrink-0.border-b.border-divider-subtle') const content = header?.parentElement expect(content?.getAttribute('style')).toContain('calc(100vh - 72px)') }) }) describe('Event Handlers', () => { it('should call onHide when close button is clicked', () => { const handleHide = vi.fn() render( Body} />, ) const title = screen.getByText('Test') const headerRight = title.nextElementSibling // .flex items-center const closeDiv = headerRight?.querySelector('.cursor-pointer') as HTMLElement fireEvent.click(closeDiv) expect(handleHide).toHaveBeenCalledTimes(1) }) }) describe('Complex Content', () => { it('should render complex JSX elements in body', () => { const complexBody = (

Header

Paragraph

) render( {}} title="Test" body={complexBody} />, ) expect(screen.getByText('Header')).toBeInTheDocument() expect(screen.getByText('Paragraph')).toBeInTheDocument() expect(screen.getByRole('button', { name: 'Action Button' })).toBeInTheDocument() }) it('should render complex footer', () => { const complexFooter = (
) render( {}} title="Test" body={
Body
} foot={complexFooter} />, ) expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument() expect(screen.getByRole('button', { name: 'Save' })).toBeInTheDocument() }) }) describe('Edge Cases', () => { it('should handle empty title', () => { render( {}} title="" body={
Body
} />, ) expect(screen.getByRole('dialog')).toBeInTheDocument() }) it('should handle undefined titleDescription', () => { render( {}} title="Test" titleDescription={undefined} body={
Body
} />, ) expect(screen.getByRole('dialog')).toBeInTheDocument() }) it('should handle rapid isShow toggle', () => { const { rerender } = render( {}} title="Test" body={
Body
} />, ) expect(screen.getByRole('dialog')).toBeInTheDocument() rerender( {}} title="Test" body={
Body
} />, ) expect(screen.queryByRole('dialog')).not.toBeInTheDocument() rerender( {}} title="Test" body={
Body
} />, ) expect(screen.getByRole('dialog')).toBeInTheDocument() }) it('should handle special characters in title', () => { const specialTitle = 'Test <> & " \' | Drawer' render( {}} title={specialTitle} body={
Body
} />, ) expect(screen.getByText(specialTitle)).toBeInTheDocument() }) it('should handle empty body content', () => { render( {}} title="Test" body={
} />, ) expect(screen.getByRole('dialog')).toBeInTheDocument() }) it('should apply both custom maxWidth and panel classNames', () => { render( {}} title="Test" body={
Body
} maxWidthClassName="!max-w-[500px]" panelClassName="custom-style" />, ) const innerPanel = screen.getByText('Test').closest('.bg-components-panel-bg') const outerPanel = innerPanel?.parentElement expect(outerPanel?.className).toContain('!max-w-[500px]') expect(outerPanel?.className).toContain('custom-style') }) }) describe('Memoization', () => { it('should be memoized and not re-render on parent changes', () => { const { rerender } = render( {}} title="Test" body={
Body
} />, ) const dialog = screen.getByRole('dialog') rerender( {}} title="Test" body={
Body
} />, ) expect(dialog).toBeInTheDocument() }) }) })