Files
edentext/tests/unit/save-as-format.test.ts
T
Steffen Becker b8ff3d764f feat: Save As offers both document formats in one picker
The picker lists .odt and .docx, and the chosen extension decides which
exporter runs and which format the document keeps — no silent switch to
.odt for a file that came in as .docx. Both save paths now build their
bytes through one helper instead of repeating the argument list.
2026-09-04 19:30:12 +02:00

48 lines
1.8 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { saveAsDocument } from '../../src/lib/export/saveFile';
// A handle that records what was written to it, like the picker hands back.
function handleFor(name: string) {
const written: Uint8Array[] = [];
return {
handle: { name, createWritable: async () => ({ write: (b: Uint8Array) => { written.push(b); }, close: async () => {} }) },
written,
};
}
async function saveAs(pickerName: string | null, fallback: 'odt' | 'docx') {
const picked = pickerName ? handleFor(pickerName) : null;
const win = window as unknown as Record<string, unknown>;
if (picked) win.showSaveFilePicker = async () => picked.handle;
else delete win.showSaveFilePicker;
const built: string[] = [];
const res = await saveAsDocument(async (kind) => { built.push(kind); return new Uint8Array([1, 2]); },
`doc.${fallback}`, fallback);
return { built, kind: res.kind, handle: res.handle, written: picked?.written };
}
describe('Save As builds the format the picker chose', () => {
it('writes .docx when the chosen name says so', async () => {
const r = await saveAs('report.docx', 'odt');
expect(r.built).toEqual(['docx']);
expect(r.kind).toBe('docx');
expect(r.written?.[0]).toEqual(new Uint8Array([1, 2]));
});
it('writes .odt for a document that came in as .docx', async () => {
const r = await saveAs('report.odt', 'docx');
expect(r.built).toEqual(['odt']);
expect(r.kind).toBe('odt');
});
it('keeps the document format where there is no picker', async () => {
vi.stubGlobal('alert', () => {});
URL.createObjectURL = () => 'blob:x';
URL.revokeObjectURL = () => {};
const r = await saveAs(null, 'docx');
expect(r.built).toEqual(['docx']);
expect(r.kind).toBe('docx');
expect(r.handle).toBeNull();
});
});