| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | |
| 3 | const state = vi.hoisted(() => ({ |
| 4 | cleanupHtmlEditor: vi.fn() |
| 5 | })) |
| 6 | |
| 7 | vi.mock('../../../src/renderer/src/lib/ipc', () => ({ |
| 8 | ipc: { |
| 9 | cleanupHtmlEditor: state.cleanupHtmlEditor |
| 10 | } |
| 11 | })) |
| 12 | |
| 13 | import { useHtmlEditorStore } from '../../../src/renderer/src/store/htmlEditorStore' |
| 14 | |
| 15 | const documentOne = { |
| 16 | id: 'hedit-1', |
| 17 | title: 'Landing page', |
| 18 | sourcePath: '/tmp/landing.html', |
| 19 | htmlPath: '/tmp/landing/current.html', |
| 20 | designWidth: 1280, |
| 21 | updatedAt: 1, |
| 22 | thumbnailPath: null |
| 23 | } |
| 24 | |
| 25 | const documentTwo = { |
| 26 | ...documentOne, |
| 27 | id: 'hedit-2', |
| 28 | title: 'Pricing page' |
| 29 | } |
| 30 | |
| 31 | describe('html editor document library', () => { |
| 32 | beforeEach(() => { |
| 33 | state.cleanupHtmlEditor.mockReset() |
| 34 | useHtmlEditorStore.setState({ |
| 35 | docId: 'hedit-1', |
| 36 | title: documentOne.title, |
| 37 | htmlPath: documentOne.htmlPath, |
| 38 | sourcePath: documentOne.sourcePath, |
| 39 | designWidth: documentOne.designWidth, |
| 40 | html: '<html></html>', |
| 41 | importing: false, |
| 42 | exporting: false, |
| 43 | error: null, |
| 44 | documents: [documentOne, documentTwo] |
| 45 | }) |
| 46 | }) |
| 47 | |
| 48 | afterEach(() => { |
| 49 | useHtmlEditorStore.getState().reset() |
| 50 | }) |
| 51 | |
| 52 | it('removes only the requested document from the library after database cleanup succeeds', async () => { |
| 53 | state.cleanupHtmlEditor.mockResolvedValue({ ok: true }) |
| 54 | |
| 55 | await expect(useHtmlEditorStore.getState().removeDocument('hedit-1')).resolves.toBe(true) |
| 56 | |
| 57 | expect(state.cleanupHtmlEditor).toHaveBeenCalledWith({ docId: 'hedit-1' }) |
| 58 | expect(useHtmlEditorStore.getState()).toMatchObject({ |
| 59 | docId: null, |
| 60 | htmlPath: null, |
| 61 | sourcePath: null, |
| 62 | html: '', |
| 63 | documents: [documentTwo] |
| 64 | }) |
| 65 | }) |
| 66 | |
| 67 | it('keeps the document in the library when database cleanup fails', async () => { |
| 68 | state.cleanupHtmlEditor.mockResolvedValue({ ok: false }) |
| 69 | |
| 70 | await expect(useHtmlEditorStore.getState().removeDocument('hedit-1')).resolves.toBe(false) |
| 71 | |
| 72 | expect(useHtmlEditorStore.getState().documents).toEqual([documentOne, documentTwo]) |
| 73 | }) |
| 74 | }) |
| 75 |