|
| 1 | +/** |
| 2 | + * Tests for the "Export ZIP" wiring in ProjectTab. |
| 3 | + * |
| 4 | + * Scope: the component derives ONE project-folder slug and uses it for both |
| 5 | + * the in-archive top-level folder (passed to `onExportZip`) and the download |
| 6 | + * filename stem, so the two can never drift (GH #147). The slug is sanitized |
| 7 | + * via `projectFolderName`, so a hostile character in the project name must not |
| 8 | + * leak into either. |
| 9 | + * |
| 10 | + * The path normalization itself is exhaustively covered by node-env unit tests |
| 11 | + * (quarto-sync-client/export-zip.test.ts and project-folder-name.test.ts); here |
| 12 | + * we only assert the UI hands the same, sanitized slug to both consumers. |
| 13 | + * |
| 14 | + * @vitest-environment jsdom |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 18 | +import { render, screen, fireEvent, cleanup } from '@testing-library/react'; |
| 19 | +import ProjectTab from './ProjectTab'; |
| 20 | +import type { ProjectEntry } from '@quarto/preview-renderer/types/project'; |
| 21 | + |
| 22 | +function makeProject(description: string): ProjectEntry { |
| 23 | + return { |
| 24 | + id: 'local-1', |
| 25 | + indexDocId: 'automerge:abc123', |
| 26 | + syncServer: 'wss://example.test/sync', |
| 27 | + description, |
| 28 | + createdAt: '2026-07-01T00:00:00.000Z', |
| 29 | + lastAccessed: '2026-07-01T00:00:00.000Z', |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +describe('ProjectTab — Export ZIP wiring', () => { |
| 34 | + let clickedDownloadNames: string[]; |
| 35 | + let createElementSpy: ReturnType<typeof vi.spyOn>; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + clickedDownloadNames = []; |
| 39 | + // jsdom does not implement object URLs; stub them. |
| 40 | + vi.stubGlobal('URL', { |
| 41 | + ...URL, |
| 42 | + createObjectURL: vi.fn(() => 'blob:mock'), |
| 43 | + revokeObjectURL: vi.fn(), |
| 44 | + }); |
| 45 | + // Capture the download filename of any anchor the handler clicks. |
| 46 | + const realCreateElement = document.createElement.bind(document); |
| 47 | + createElementSpy = vi |
| 48 | + .spyOn(document, 'createElement') |
| 49 | + .mockImplementation((tag: string, opts?: ElementCreationOptions) => { |
| 50 | + const el = realCreateElement(tag, opts); |
| 51 | + if (tag === 'a') { |
| 52 | + vi.spyOn(el as HTMLAnchorElement, 'click').mockImplementation(() => { |
| 53 | + clickedDownloadNames.push((el as HTMLAnchorElement).download); |
| 54 | + }); |
| 55 | + } |
| 56 | + return el; |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + createElementSpy.mockRestore(); |
| 62 | + vi.unstubAllGlobals(); |
| 63 | + cleanup(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('passes the sanitized slug as rootDir and reuses it for the filename', () => { |
| 67 | + const onExportZip = vi.fn(() => new Uint8Array([1, 2, 3])); |
| 68 | + render( |
| 69 | + <ProjectTab |
| 70 | + project={makeProject('Demo Playground')} |
| 71 | + onChooseNewProject={() => {}} |
| 72 | + onExportZip={onExportZip} |
| 73 | + />, |
| 74 | + ); |
| 75 | + |
| 76 | + fireEvent.click(screen.getByText('Export ZIP')); |
| 77 | + |
| 78 | + expect(onExportZip).toHaveBeenCalledWith('Demo-Playground'); |
| 79 | + expect(clickedDownloadNames).toEqual(['Demo-Playground.zip']); |
| 80 | + }); |
| 81 | + |
| 82 | + it('sanitizes hostile characters in the project name for both outputs', () => { |
| 83 | + const onExportZip = vi.fn(() => new Uint8Array([1])); |
| 84 | + render( |
| 85 | + <ProjectTab |
| 86 | + project={makeProject('Demo: Playground?')} |
| 87 | + onChooseNewProject={() => {}} |
| 88 | + onExportZip={onExportZip} |
| 89 | + />, |
| 90 | + ); |
| 91 | + |
| 92 | + fireEvent.click(screen.getByText('Export ZIP')); |
| 93 | + |
| 94 | + // ':' and '?' collapse to hyphens; folder and filename stay in lock-step. |
| 95 | + expect(onExportZip).toHaveBeenCalledWith('Demo-Playground'); |
| 96 | + expect(clickedDownloadNames).toEqual(['Demo-Playground.zip']); |
| 97 | + }); |
| 98 | + |
| 99 | + it('falls back to "project" when the name is empty', () => { |
| 100 | + const onExportZip = vi.fn(() => new Uint8Array([1])); |
| 101 | + render( |
| 102 | + <ProjectTab |
| 103 | + project={makeProject('')} |
| 104 | + onChooseNewProject={() => {}} |
| 105 | + onExportZip={onExportZip} |
| 106 | + />, |
| 107 | + ); |
| 108 | + |
| 109 | + fireEvent.click(screen.getByText('Export ZIP')); |
| 110 | + |
| 111 | + expect(onExportZip).toHaveBeenCalledWith('project'); |
| 112 | + expect(clickedDownloadNames).toEqual(['project.zip']); |
| 113 | + }); |
| 114 | +}); |
0 commit comments