mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-03 02:11:07 +05:30
- WelcomeDialog: add quick start, feature showcase, keyboard shortcuts, recent files, version display - ExportDocxDialog/ExportHtmlDialog/ExportPdfDialog: add header/footer, paper size, margin controls - GitStatusPanel: full staging/unstaging, discard, commit workflow - CommandPalette: fuzzy command search with keyboard navigation - FindReplaceBar: find/replace with regex, case, whole-word options - New dialogs: BatchMediaConverterDialog, HeaderFooterDialog, PdfEditorDialog, UniversalConverterDialog - Tests: comprehensive coverage for all new/updated components
90 lines
3.0 KiB
TypeScript
90 lines
3.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { WordExportDialog } from '@/components/modals/WordExportDialog';
|
|
import { useFileStore } from '@/stores/file-store';
|
|
import { useEditorStore } from '@/stores/editor-store';
|
|
import { useSettingsStore } from '@/stores/settings-store';
|
|
|
|
vi.mock('@/lib/docx-export', () => ({
|
|
generateDocx: vi.fn().mockResolvedValue(
|
|
new Blob([new Uint8Array([1, 2, 3])], {
|
|
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
})
|
|
),
|
|
}));
|
|
|
|
vi.mock('@/lib/toast', () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('@/lib/ipc', () => ({
|
|
ipc: {
|
|
app: {
|
|
showSaveDialog: vi.fn(),
|
|
},
|
|
file: {
|
|
writeBuffer: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
import { generateDocx } from '@/lib/docx-export';
|
|
import { toast } from '@/lib/toast';
|
|
import { ipc } from '@/lib/ipc';
|
|
|
|
describe('WordExportDialog', () => {
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
vi.clearAllMocks();
|
|
useSettingsStore.setState(useSettingsStore.getInitialState());
|
|
useFileStore.setState({
|
|
activeTabId: '/test.md',
|
|
openTabs: [{ id: '/test.md', path: '/test.md', title: 'test.md', dirty: false }],
|
|
} as any);
|
|
useEditorStore.setState({
|
|
buffers: new Map([
|
|
['/test.md', { id: '/test.md', path: '/test.md', content: '# hi', dirty: false }],
|
|
]),
|
|
} as any);
|
|
(ipc.app.showSaveDialog as any).mockResolvedValue({ ok: true, data: '/out.docx' });
|
|
(ipc.file.writeBuffer as any).mockResolvedValue({ ok: true });
|
|
});
|
|
|
|
it('renders with template selector (standard / custom)', () => {
|
|
render(<WordExportDialog sourcePath="/test.md" />);
|
|
expect(screen.getByText(/export to word/i)).toBeInTheDocument();
|
|
expect(screen.getByText(/test\.md/)).toBeInTheDocument();
|
|
});
|
|
|
|
it('submitting calls generateDocx and writes the file', async () => {
|
|
render(<WordExportDialog sourcePath="/test.md" />);
|
|
await userEvent.click(screen.getByRole('button', { name: /^export$/i }));
|
|
expect(generateDocx).toHaveBeenCalledTimes(1);
|
|
expect(ipc.file.writeBuffer).toHaveBeenCalledTimes(1);
|
|
expect(toast.success).toHaveBeenCalledWith(expect.stringContaining('Exported test.md'));
|
|
});
|
|
|
|
it('shows error message on write failure', async () => {
|
|
(ipc.file.writeBuffer as any).mockResolvedValue({
|
|
ok: false,
|
|
error: { code: 'E', message: 'write failed' },
|
|
});
|
|
render(<WordExportDialog sourcePath="/test.md" />);
|
|
await userEvent.click(screen.getByRole('button', { name: /^export$/i }));
|
|
expect(await screen.findByText(/write failed/i)).toBeInTheDocument();
|
|
});
|
|
|
|
it('switches to custom template path when "Custom" is selected', async () => {
|
|
useSettingsStore.setState({
|
|
...useSettingsStore.getInitialState(),
|
|
docxCustomTemplatePath: '/my.dotx',
|
|
});
|
|
render(<WordExportDialog sourcePath="/test.md" />);
|
|
expect(screen.getByText(/\/my\.dotx/)).toBeInTheDocument();
|
|
});
|
|
});
|