|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import type { Attachment } from 'stream-chat'; |
| 4 | + |
| 5 | +import { UnsupportedAttachment } from '../UnsupportedAttachment'; |
| 6 | +import { ComponentProvider } from '../../../context/ComponentContext'; |
| 7 | +import { TranslationProvider } from '../../../context'; |
| 8 | +import { |
| 9 | + mockComponentContext, |
| 10 | + mockTranslationContextValue, |
| 11 | +} from '../../../mock-builders'; |
| 12 | +import type { FileIconProps } from '../../FileIcon/FileIcon'; |
| 13 | + |
| 14 | +const renderUnsupported = ( |
| 15 | + attachment: Attachment, |
| 16 | + componentOverrides: Record<string, unknown> = {}, |
| 17 | +) => |
| 18 | + render( |
| 19 | + <TranslationProvider value={mockTranslationContextValue()}> |
| 20 | + <ComponentProvider value={mockComponentContext(componentOverrides)}> |
| 21 | + <UnsupportedAttachment attachment={attachment} /> |
| 22 | + </ComponentProvider> |
| 23 | + </TranslationProvider>, |
| 24 | + ); |
| 25 | + |
| 26 | +describe('UnsupportedAttachment', () => { |
| 27 | + it('renders attachment title when title is set', () => { |
| 28 | + renderUnsupported({ |
| 29 | + mime_type: 'application/x-custom', |
| 30 | + title: 'Custom payload', |
| 31 | + type: 'unknown', |
| 32 | + } as Attachment); |
| 33 | + |
| 34 | + expect(screen.getByTestId('unsupported-attachment-title')).toHaveTextContent( |
| 35 | + 'Custom payload', |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('falls back to translated label when title is missing', () => { |
| 40 | + renderUnsupported({ type: 'weird' } as Attachment); |
| 41 | + |
| 42 | + expect(screen.getByTestId('unsupported-attachment-title')).toHaveTextContent( |
| 43 | + 'Unsupported attachment', |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('uses AttachmentFileIcon from context when provided', () => { |
| 48 | + const CustomAttachmentFileIcon = ({ fileName, mimeType }: FileIconProps) => ( |
| 49 | + <span |
| 50 | + data-file-name={fileName} |
| 51 | + data-mime-type={mimeType} |
| 52 | + data-testid='custom-attachment-file-icon' |
| 53 | + /> |
| 54 | + ); |
| 55 | + |
| 56 | + renderUnsupported( |
| 57 | + { |
| 58 | + mime_type: 'application/octet-stream', |
| 59 | + title: 'data.bin', |
| 60 | + type: 'file', |
| 61 | + } as Attachment, |
| 62 | + { AttachmentFileIcon: CustomAttachmentFileIcon }, |
| 63 | + ); |
| 64 | + |
| 65 | + const icon = screen.getByTestId('custom-attachment-file-icon'); |
| 66 | + expect(icon).toHaveAttribute('data-file-name', 'data.bin'); |
| 67 | + expect(icon).toHaveAttribute('data-mime-type', 'application/octet-stream'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments