|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import FilePreview from './FilePreview'; |
| 4 | +import { ChatbotDisplayMode } from '../Chatbot'; |
| 5 | +import { Button, ModalBodyProps, ModalHeaderProps } from '@patternfly/react-core'; |
| 6 | + |
| 7 | +describe('FilePreview', () => { |
| 8 | + const defaultProps = { |
| 9 | + isModalOpen: true, |
| 10 | + handleModalToggle: jest.fn(), |
| 11 | + fileName: 'test-file.txt', |
| 12 | + children: 'File content preview' |
| 13 | + }; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should render with basic props', () => { |
| 20 | + render(<FilePreview {...defaultProps} />); |
| 21 | + expect(screen.getByText('File preview')).toBeInTheDocument(); |
| 22 | + expect(screen.getByText('test-file.txt')).toBeInTheDocument(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should render with custom title', () => { |
| 26 | + const customTitle = 'Custom file preview title'; |
| 27 | + render(<FilePreview {...defaultProps} title={customTitle} />); |
| 28 | + expect(screen.getByRole('heading', { name: customTitle })).toBeTruthy(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should handle modal toggle when closed', () => { |
| 32 | + const mockToggle = jest.fn(); |
| 33 | + render(<FilePreview {...defaultProps} isModalOpen={false} handleModalToggle={mockToggle} />); |
| 34 | + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should apply default display mode class', () => { |
| 38 | + render(<FilePreview {...defaultProps} />); |
| 39 | + const modal = screen.getByRole('dialog'); |
| 40 | + expect(modal).toHaveClass('pf-chatbot__file-preview-modal--default'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should apply custom display mode class', () => { |
| 44 | + render(<FilePreview {...defaultProps} displayMode={ChatbotDisplayMode.fullscreen} />); |
| 45 | + const modal = screen.getByRole('dialog'); |
| 46 | + expect(modal).toHaveClass('pf-chatbot__file-preview-modal--fullscreen'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should apply compact styling when isCompact is true', () => { |
| 50 | + render(<FilePreview {...defaultProps} isCompact />); |
| 51 | + const modal = screen.getByRole('dialog'); |
| 52 | + expect(modal).toHaveClass('pf-m-compact'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should not apply compact styling when isCompact is false', () => { |
| 56 | + render(<FilePreview {...defaultProps} isCompact={false} />); |
| 57 | + const modal = screen.getByRole('dialog'); |
| 58 | + expect(modal).not.toHaveClass('pf-m-compact'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should apply custom className', () => { |
| 62 | + const customClass = 'custom-file-preview'; |
| 63 | + render(<FilePreview {...defaultProps} className={customClass} />); |
| 64 | + const modal = screen.getByRole('dialog'); |
| 65 | + expect(modal).toHaveClass(customClass); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should pass through additional props to ChatbotModal', () => { |
| 69 | + render(<FilePreview {...defaultProps} data-testid="file-preview-modal" />); |
| 70 | + const modal = screen.getByTestId('file-preview-modal'); |
| 71 | + expect(modal).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should pass modalHeaderProps to ModalHeader', () => { |
| 75 | + const modalHeaderProps = { |
| 76 | + 'data-testid': 'custom-header' |
| 77 | + } as ModalHeaderProps; |
| 78 | + render(<FilePreview {...defaultProps} modalHeaderProps={modalHeaderProps} />); |
| 79 | + const header = screen.getByTestId('custom-header'); |
| 80 | + expect(header).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should pass modalBodyProps to ModalBody', () => { |
| 84 | + const modalBodyProps = { |
| 85 | + 'data-testid': 'custom-body' |
| 86 | + } as ModalBodyProps; |
| 87 | + render(<FilePreview {...defaultProps} modalBodyProps={modalBodyProps} />); |
| 88 | + const body = screen.getByTestId('custom-body'); |
| 89 | + expect(body).toBeInTheDocument(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should pass ouiaId to ChatbotModal', () => { |
| 93 | + const ouiaId = 'file-preview-ouia-id'; |
| 94 | + render(<FilePreview {...defaultProps} ouiaId={ouiaId} />); |
| 95 | + const modal = screen.getByRole('dialog'); |
| 96 | + expect(modal).toHaveAttribute('data-ouia-component-id', ouiaId); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should handle complex children', () => { |
| 100 | + const complexChildren = ( |
| 101 | + <div> |
| 102 | + <h3>File details</h3> |
| 103 | + <p>Size: 1.2 MB</p> |
| 104 | + <Button>Download</Button> |
| 105 | + </div> |
| 106 | + ); |
| 107 | + render(<FilePreview {...defaultProps}>{complexChildren}</FilePreview>); |
| 108 | + expect(screen.getByRole('heading', { name: /File details/i })).toBeTruthy(); |
| 109 | + expect(screen.getByText('Size: 1.2 MB')).toBeTruthy(); |
| 110 | + expect(screen.getByRole('button', { name: /Download/i })).toBeTruthy(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments