|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 2 | +import '../../../src/static/js/shared-threads.js'; |
| 3 | +import '../../../src/static/js/components/acb-thread-header.js'; |
| 4 | +import '../../../src/static/js/components/acb-thread-context-menu.js'; |
| 5 | + |
| 6 | +const { exportThread } = window.AcbThreads; |
| 7 | + |
| 8 | +// ───────────────────────────────────────────────────────────────────────────── |
| 9 | +// DOM / visual-structure tests |
| 10 | +// ───────────────────────────────────────────────────────────────────────────── |
| 11 | + |
| 12 | +describe('Web Component: acb-thread-header — export button', () => { |
| 13 | + let element; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + element = document.createElement('acb-thread-header'); |
| 17 | + document.body.appendChild(element); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + document.body.innerHTML = ''; |
| 22 | + }); |
| 23 | + |
| 24 | + it('renders export button in thread header', () => { |
| 25 | + const btn = element.querySelector('#export-thread-btn'); |
| 26 | + expect(btn).toBeTruthy(); |
| 27 | + expect(btn.tagName).toBe('BUTTON'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('export button has correct aria-label', () => { |
| 31 | + const btn = element.querySelector('#export-thread-btn'); |
| 32 | + expect(btn?.getAttribute('aria-label')).toBe('Export thread as Markdown'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('export button has download SVG icon', () => { |
| 36 | + const btn = element.querySelector('#export-thread-btn'); |
| 37 | + const svg = btn?.querySelector('svg'); |
| 38 | + expect(svg).toBeTruthy(); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('Web Component: acb-thread-context-menu — Export .md item', () => { |
| 43 | + let element; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + element = document.createElement('acb-thread-context-menu'); |
| 47 | + document.body.appendChild(element); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + document.body.innerHTML = ''; |
| 52 | + }); |
| 53 | + |
| 54 | + it('renders export item in context menu', () => { |
| 55 | + const btn = element.querySelector('#ctx-export'); |
| 56 | + expect(btn).toBeTruthy(); |
| 57 | + expect(btn.tagName).toBe('BUTTON'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('context menu export item has correct text', () => { |
| 61 | + const btn = element.querySelector('#ctx-export'); |
| 62 | + expect(btn?.textContent?.trim()).toBe('Export .md'); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +// ───────────────────────────────────────────────────────────────────────────── |
| 67 | +// Logic tests — exportThread() |
| 68 | +// ───────────────────────────────────────────────────────────────────────────── |
| 69 | + |
| 70 | +describe('exportThread()', () => { |
| 71 | + let fetchMock; |
| 72 | + let createObjectURLMock; |
| 73 | + let revokeObjectURLMock; |
| 74 | + let appendChildSpy; |
| 75 | + let removeChildSpy; |
| 76 | + let clickSpy; |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + fetchMock = vi.fn(); |
| 80 | + global.fetch = fetchMock; |
| 81 | + |
| 82 | + createObjectURLMock = vi.fn(() => 'blob:mock-url'); |
| 83 | + revokeObjectURLMock = vi.fn(); |
| 84 | + global.URL.createObjectURL = createObjectURLMock; |
| 85 | + global.URL.revokeObjectURL = revokeObjectURLMock; |
| 86 | + |
| 87 | + clickSpy = vi.fn(); |
| 88 | + appendChildSpy = vi.spyOn(document.body, 'appendChild').mockImplementation((el) => { |
| 89 | + if (el.tagName === 'A') { |
| 90 | + el.click = clickSpy; |
| 91 | + } |
| 92 | + }); |
| 93 | + removeChildSpy = vi.spyOn(document.body, 'removeChild').mockImplementation(() => {}); |
| 94 | + }); |
| 95 | + |
| 96 | + afterEach(() => { |
| 97 | + vi.restoreAllMocks(); |
| 98 | + delete global.fetch; |
| 99 | + }); |
| 100 | + |
| 101 | + it('exportThread creates Blob and triggers download', async () => { |
| 102 | + const markdownContent = '# My Thread\n\n> **Status:** discuss\n'; |
| 103 | + fetchMock.mockResolvedValue({ |
| 104 | + ok: true, |
| 105 | + text: async () => markdownContent, |
| 106 | + }); |
| 107 | + |
| 108 | + await exportThread({ threadId: 'thread-123', topic: 'My Thread' }); |
| 109 | + |
| 110 | + expect(fetchMock).toHaveBeenCalledWith('/api/threads/thread-123/export'); |
| 111 | + expect(createObjectURLMock).toHaveBeenCalledOnce(); |
| 112 | + const blobArg = createObjectURLMock.mock.calls[0][0]; |
| 113 | + expect(blobArg).toBeInstanceOf(Blob); |
| 114 | + expect(clickSpy).toHaveBeenCalledOnce(); |
| 115 | + expect(revokeObjectURLMock).toHaveBeenCalledWith('blob:mock-url'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('exportThread uses slugified filename from topic', async () => { |
| 119 | + fetchMock.mockResolvedValue({ |
| 120 | + ok: true, |
| 121 | + text: async () => '# My Cool Thread\n', |
| 122 | + }); |
| 123 | + |
| 124 | + let capturedHref = null; |
| 125 | + let capturedDownload = null; |
| 126 | + appendChildSpy.mockImplementation((el) => { |
| 127 | + if (el.tagName === 'A') { |
| 128 | + capturedHref = el.href; |
| 129 | + capturedDownload = el.download; |
| 130 | + el.click = clickSpy; |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + await exportThread({ threadId: 'thread-abc', topic: 'My Cool Thread' }); |
| 135 | + |
| 136 | + expect(capturedDownload).toBe('my-cool-thread.md'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('exportThread handles fetch error gracefully', async () => { |
| 140 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 141 | + fetchMock.mockRejectedValue(new Error('Network error')); |
| 142 | + |
| 143 | + await expect(exportThread({ threadId: 'thread-err', topic: 'Error Thread' })).resolves.toBeUndefined(); |
| 144 | + expect(warnSpy).toHaveBeenCalled(); |
| 145 | + expect(createObjectURLMock).not.toHaveBeenCalled(); |
| 146 | + |
| 147 | + warnSpy.mockRestore(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments