|
12 | 12 | * restores its own last typed submit and no chip text. |
13 | 13 | * A file the composer refuses for size names itself and the limit in the same |
14 | 14 | * band above the composer, and is not attached (objectstack#34463). |
| 15 | + * The accept list reads the HTML `accept` grammar (`.ext` / `type/*` / exact |
| 16 | + * MIME), and a file it refuses names itself and the accepted list in that same |
| 17 | + * band (objectstack#35276). |
15 | 18 | */ |
16 | 19 | import '@testing-library/jest-dom/vitest'; |
17 | 20 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
@@ -394,3 +397,120 @@ describe('ChatbotEnhanced composer — a file over maxFileSize is named to the u |
394 | 397 | expect(notice).not.toHaveTextContent(enLead); |
395 | 398 | }); |
396 | 399 | }); |
| 400 | + |
| 401 | +describe('ChatbotEnhanced composer — the accept list reads the HTML accept grammar, and a refused type is named to the user (objectstack#35276)', () => { |
| 402 | + const DEFAULT_ACCEPT_SHOWN = 'image/*, .pdf, .doc, .docx, .txt, .csv, .tsv, .xlsx, .xlsm'; |
| 403 | + const file = (name: string, type: string, bytes = 10) => new File([new Uint8Array(bytes)], name, { type }); |
| 404 | + |
| 405 | + beforeEach(() => { |
| 406 | + // jsdom has no object URLs; the composer makes one per attached file. |
| 407 | + Object.defineProperty(URL, 'createObjectURL', { configurable: true, value: vi.fn(() => 'blob:attached') }); |
| 408 | + Object.defineProperty(URL, 'revokeObjectURL', { configurable: true, value: vi.fn() }); |
| 409 | + }); |
| 410 | + |
| 411 | + function pick(container: HTMLElement, files: File[]) { |
| 412 | + const input = container.querySelector('input[type="file"]') as HTMLInputElement; |
| 413 | + Object.defineProperty(input, 'files', { configurable: true, value: files }); |
| 414 | + fireEvent.change(input); |
| 415 | + } |
| 416 | + |
| 417 | + /** A non-image attachment chip shows the file name as its own text. */ |
| 418 | + const attached = (name: string) => screen.queryByText(name, { selector: 'span' }); |
| 419 | + |
| 420 | + it('a CSV and a PDF picked with the default accept list attach, with no notice', () => { |
| 421 | + const { container } = render(<ChatbotEnhanced placeholder="Ask…" enableFileUpload />); |
| 422 | + pick(container, [file('data.csv', 'text/csv'), file('report.pdf', 'application/pdf')]); |
| 423 | + |
| 424 | + expect(attached('data.csv')).toBeInTheDocument(); |
| 425 | + expect(attached('report.pdf')).toBeInTheDocument(); |
| 426 | + expect(screen.queryByTestId('chat-attachment-refused')).toBeNull(); |
| 427 | + }); |
| 428 | + |
| 429 | + it('a `.ext` token matches the name case-insensitively, whatever type the browser reported', () => { |
| 430 | + const { container } = render(<ChatbotEnhanced placeholder="Ask…" enableFileUpload />); |
| 431 | + pick(container, [file('EXPORT.CSV', ''), file('Book.XLSX', 'application/octet-stream')]); |
| 432 | + |
| 433 | + expect(attached('EXPORT.CSV')).toBeInTheDocument(); |
| 434 | + expect(attached('Book.XLSX')).toBeInTheDocument(); |
| 435 | + expect(screen.queryByTestId('chat-attachment-refused')).toBeNull(); |
| 436 | + }); |
| 437 | + |
| 438 | + it('extension, wildcard and exact-MIME tokens each admit their own files and nothing else', () => { |
| 439 | + const { container } = render( |
| 440 | + <ChatbotEnhanced placeholder="Ask…" enableFileUpload acceptedFileTypes="image/*,.CSV,application/pdf" />, |
| 441 | + ); |
| 442 | + pick(container, [ |
| 443 | + file('photo.png', 'image/png'), // image/* — MIME prefix |
| 444 | + file('data.csv', 'text/csv'), // .CSV — extension, upper-case token |
| 445 | + file('doc.bin', 'application/pdf'), // application/pdf — exact MIME, the name does not matter |
| 446 | + file('clip.mp4', 'video/mp4'), // no token admits it |
| 447 | + file('scan.pdf', 'application/x-pdf'), // exact MIME is not a prefix and no `.pdf` token is listed |
| 448 | + ]); |
| 449 | + |
| 450 | + expect(screen.getByAltText('photo.png')).toBeInTheDocument(); |
| 451 | + expect(attached('data.csv')).toBeInTheDocument(); |
| 452 | + expect(attached('doc.bin')).toBeInTheDocument(); |
| 453 | + expect(attached('clip.mp4')).toBeNull(); |
| 454 | + expect(attached('scan.pdf')).toBeNull(); |
| 455 | + const notice = screen.getByTestId('chat-attachment-refused'); |
| 456 | + expect(notice).toHaveTextContent('clip.mp4'); |
| 457 | + expect(notice).toHaveTextContent('scan.pdf'); |
| 458 | + expect(notice).not.toHaveTextContent('photo.png'); |
| 459 | + }); |
| 460 | + |
| 461 | + it('a refused type shows a notice naming the file and the accepted list, and is not attached', () => { |
| 462 | + const { container } = render(<ChatbotEnhanced placeholder="Ask…" enableFileUpload />); |
| 463 | + pick(container, [file('setup.exe', 'application/x-msdownload')]); |
| 464 | + |
| 465 | + const notice = screen.getByTestId('chat-attachment-refused'); |
| 466 | + expect(notice).toHaveAttribute('role', 'alert'); |
| 467 | + expect(notice).toHaveTextContent('setup.exe'); |
| 468 | + expect(notice).toHaveTextContent(DEFAULT_ACCEPT_SHOWN); |
| 469 | + expect(attached('setup.exe')).toBeNull(); |
| 470 | + }); |
| 471 | + |
| 472 | + it('a mixed pick attaches the accepted file and names only the refused one', () => { |
| 473 | + const { container } = render(<ChatbotEnhanced placeholder="Ask…" enableFileUpload />); |
| 474 | + pick(container, [file('setup.exe', 'application/x-msdownload'), file('data.csv', 'text/csv')]); |
| 475 | + |
| 476 | + expect(attached('data.csv')).toBeInTheDocument(); |
| 477 | + const notice = screen.getByTestId('chat-attachment-refused'); |
| 478 | + expect(notice).toHaveTextContent('setup.exe'); |
| 479 | + expect(notice).not.toHaveTextContent('data.csv'); |
| 480 | + }); |
| 481 | + |
| 482 | + it('one pick refused for type AND for size names both files', () => { |
| 483 | + const { container } = render(<ChatbotEnhanced placeholder="Ask…" enableFileUpload maxFileSize={1024} />); |
| 484 | + pick(container, [file('setup.exe', 'application/x-msdownload'), file('big.png', 'image/png', 1025)]); |
| 485 | + |
| 486 | + const notice = screen.getByTestId('chat-attachment-refused'); |
| 487 | + expect(notice).toHaveTextContent('setup.exe'); |
| 488 | + expect(notice).toHaveTextContent('big.png'); |
| 489 | + }); |
| 490 | + |
| 491 | + it('CONTROL: a .png picked with the default accept list attaches with no notice', () => { |
| 492 | + const { container } = render(<ChatbotEnhanced placeholder="Ask…" enableFileUpload />); |
| 493 | + pick(container, [file('small.png', 'image/png')]); |
| 494 | + |
| 495 | + expect(screen.getByAltText('small.png')).toBeInTheDocument(); |
| 496 | + expect(screen.queryByTestId('chat-attachment-refused')).toBeNull(); |
| 497 | + }); |
| 498 | + |
| 499 | + it('under zh the notice is the zh pack sentence, not the English one', async () => { |
| 500 | + const zhLead = zh.fields.file.typeNotAccepted.split('{{name}}')[1].split('{{')[0]; |
| 501 | + const enLead = en.fields.file.typeNotAccepted.split('{{name}}')[1].split('{{')[0]; |
| 502 | + expect(zhLead).not.toBe(enLead); |
| 503 | + const { container } = render( |
| 504 | + <I18nProvider config={{ defaultLanguage: 'zh', detectBrowserLanguage: false }} persistLanguage={false}> |
| 505 | + <ChatbotEnhanced placeholder="Ask…" enableFileUpload /> |
| 506 | + </I18nProvider>, |
| 507 | + ); |
| 508 | + await screen.findByPlaceholderText('Ask…'); |
| 509 | + pick(container, [file('setup.exe', 'application/x-msdownload')]); |
| 510 | + |
| 511 | + const notice = await screen.findByTestId('chat-attachment-refused'); |
| 512 | + expect(notice).toHaveTextContent(zhLead.trim()); |
| 513 | + expect(notice).toHaveTextContent('setup.exe'); |
| 514 | + expect(notice).not.toHaveTextContent(enLead.trim()); |
| 515 | + }); |
| 516 | +}); |
0 commit comments