|
| 1 | +import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"; |
| 2 | +import { notes } from "../state"; |
| 3 | +import { CycloopsIO } from "./io"; |
| 4 | +import type { Note } from "../db"; |
| 5 | +import { db } from "../db"; |
| 6 | +import { notesToGpx, gpxToNotes } from "../gpx"; |
| 7 | + |
| 8 | +customElements.define("cycloops-io", CycloopsIO); |
| 9 | + |
| 10 | +// Mock db |
| 11 | +vi.mock("../db", () => ({ |
| 12 | + db: { |
| 13 | + notes: { |
| 14 | + add: vi.fn(), |
| 15 | + }, |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +// Mock gpx module so we can control its output |
| 20 | +vi.mock("../gpx", () => ({ |
| 21 | + notesToGpx: vi.fn(() => "<gpx/>"), |
| 22 | + gpxToNotes: vi.fn(() => [ |
| 23 | + { lat: 51.5, lon: -0.1, text: "Imported note", time: 1000000 }, |
| 24 | + ]), |
| 25 | +})); |
| 26 | + |
| 27 | +const mockNotes: Note[] = [ |
| 28 | + { id: 1, time: 1000, text: "Note A", lat: 10, lon: 20 }, |
| 29 | +]; |
| 30 | + |
| 31 | +describe("CycloopsIO component", () => { |
| 32 | + let io: CycloopsIO; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + document.body.innerHTML = ""; |
| 36 | + notes.value = mockNotes; |
| 37 | + io = document.createElement("cycloops-io") as CycloopsIO; |
| 38 | + document.body.appendChild(io); |
| 39 | + vi.clearAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + vi.restoreAllMocks(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("renders an export button and an import button", () => { |
| 47 | + const buttons = io.querySelectorAll("button"); |
| 48 | + expect(buttons).toHaveLength(2); |
| 49 | + expect(buttons[0].title).toBe("Export GPX"); |
| 50 | + expect(buttons[1].title).toBe("Import GPX"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("renders a hidden file input for import", () => { |
| 54 | + const input = io.querySelector('input[type="file"]') as HTMLInputElement; |
| 55 | + expect(input).not.toBeNull(); |
| 56 | + expect(input.style.display).toBe("none"); |
| 57 | + expect(input.accept).toContain(".gpx"); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("handleExport", () => { |
| 61 | + it("creates a downloadable blob URL and triggers a click", () => { |
| 62 | + vi.mocked(notesToGpx).mockReturnValue("<gpx>data</gpx>"); |
| 63 | + |
| 64 | + const mockUrl = "blob:mock-url"; |
| 65 | + const createObjectURL = vi |
| 66 | + .spyOn(URL, "createObjectURL") |
| 67 | + .mockReturnValue(mockUrl); |
| 68 | + const revokeObjectURL = vi |
| 69 | + .spyOn(URL, "revokeObjectURL") |
| 70 | + .mockImplementation(() => {}); |
| 71 | + |
| 72 | + const clickSpy = vi.fn(); |
| 73 | + const origCreate = document.createElement.bind(document); |
| 74 | + vi.spyOn(document, "createElement").mockImplementation((tag: string) => { |
| 75 | + const el = origCreate(tag); |
| 76 | + if (tag === "a") el.click = clickSpy; |
| 77 | + return el; |
| 78 | + }); |
| 79 | + |
| 80 | + io.handleExport(); |
| 81 | + |
| 82 | + expect(notesToGpx).toHaveBeenCalledWith(mockNotes); |
| 83 | + expect(createObjectURL).toHaveBeenCalledOnce(); |
| 84 | + expect(clickSpy).toHaveBeenCalledOnce(); |
| 85 | + expect(revokeObjectURL).toHaveBeenCalledWith(mockUrl); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe("handleImport", () => { |
| 90 | + it("reads a file and adds parsed notes to the db", async () => { |
| 91 | + const fakeGpxContent = "<gpx/>"; |
| 92 | + const fakeFile = new File([fakeGpxContent], "ride.gpx", { |
| 93 | + type: "application/gpx+xml", |
| 94 | + }); |
| 95 | + |
| 96 | + const input = io.querySelector('input[type="file"]') as HTMLInputElement; |
| 97 | + Object.defineProperty(input, "files", { |
| 98 | + value: [fakeFile], |
| 99 | + configurable: true, |
| 100 | + }); |
| 101 | + |
| 102 | + await io.handleImport(input); |
| 103 | + |
| 104 | + expect(gpxToNotes).toHaveBeenCalledOnce(); |
| 105 | + expect(db.notes.add).toHaveBeenCalledOnce(); |
| 106 | + expect(db.notes.add).toHaveBeenCalledWith({ |
| 107 | + lat: 51.5, |
| 108 | + lon: -0.1, |
| 109 | + text: "Imported note", |
| 110 | + time: 1000000, |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("does nothing when no file is selected", async () => { |
| 115 | + const input = io.querySelector('input[type="file"]') as HTMLInputElement; |
| 116 | + Object.defineProperty(input, "files", { |
| 117 | + value: [], |
| 118 | + configurable: true, |
| 119 | + }); |
| 120 | + |
| 121 | + await io.handleImport(input); |
| 122 | + |
| 123 | + expect(db.notes.add).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("resets the input value after import", async () => { |
| 127 | + const fakeFile = new File(["<gpx/>"], "ride.gpx"); |
| 128 | + const input = io.querySelector('input[type="file"]') as HTMLInputElement; |
| 129 | + Object.defineProperty(input, "files", { |
| 130 | + value: [fakeFile], |
| 131 | + configurable: true, |
| 132 | + }); |
| 133 | + |
| 134 | + await io.handleImport(input); |
| 135 | + |
| 136 | + expect(input.value).toBe(""); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments