|
| 1 | +import { expect } from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { setImmediate } from 'node:timers/promises'; |
| 4 | +import { copyMarkdown } from '../../portal-template/src/lib/copy-markdown'; |
| 5 | + |
| 6 | +class FakeClipboardItem { |
| 7 | + constructor(readonly items: Record<string, Promise<string>>) {} |
| 8 | +} |
| 9 | + |
| 10 | +describe('copying a page as Markdown', () => { |
| 11 | + let clipboard: string; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + clipboard = 'before'; |
| 15 | + sinon.define(globalThis, 'ClipboardItem', FakeClipboardItem); |
| 16 | + sinon.define(globalThis.navigator, 'clipboard', { |
| 17 | + write: async ([item]: FakeClipboardItem[]) => { |
| 18 | + clipboard = await item.items['text/plain']; |
| 19 | + } |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => sinon.restore()); |
| 24 | + |
| 25 | + it('puts the Markdown fetched under the base path on the clipboard', async () => { |
| 26 | + const fetch = sinon.stub(globalThis, 'fetch').resolves(new globalThis.Response('# Intro')); |
| 27 | + |
| 28 | + await copyMarkdown('/guides/intro.md', '/docs/'); |
| 29 | + |
| 30 | + expect(fetch.firstCall.args[0]).to.equal('/docs/guides/intro.md'); |
| 31 | + expect(clipboard).to.equal('# Intro'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('fetches from the root when the site is hosted there', async () => { |
| 35 | + const fetch = sinon.stub(globalThis, 'fetch').resolves(new globalThis.Response('# Intro')); |
| 36 | + |
| 37 | + await copyMarkdown('/guides/intro.md', '/'); |
| 38 | + |
| 39 | + expect(fetch.firstCall.args[0]).to.equal('/guides/intro.md'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('rejects on an error response and leaves the clipboard as it was', async () => { |
| 43 | + sinon.stub(globalThis, 'fetch').resolves(new globalThis.Response('<html>Not Found</html>', { status: 404 })); |
| 44 | + |
| 45 | + const error = await copyMarkdown('/guides/missing.md', '/').catch((reason: unknown) => reason); |
| 46 | + |
| 47 | + expect(error).to.be.an('error').with.property('message', 'Failed to fetch /guides/missing.md: 404'); |
| 48 | + expect(clipboard).to.equal('before'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('rejects when the request gets no response and leaves the clipboard as it was', async () => { |
| 52 | + const offline = new TypeError('Failed to fetch'); |
| 53 | + sinon.stub(globalThis, 'fetch').rejects(offline); |
| 54 | + |
| 55 | + const error = await copyMarkdown('/guides/offline.md', '/').catch((reason: unknown) => reason); |
| 56 | + |
| 57 | + expect(error).to.equal(offline); |
| 58 | + expect(clipboard).to.equal('before'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('rejects when the clipboard refuses the write', async () => { |
| 62 | + sinon.stub(globalThis, 'fetch').resolves(new globalThis.Response('# Denied')); |
| 63 | + const denied = new globalThis.DOMException('Write permission denied.', 'NotAllowedError'); |
| 64 | + sinon.stub(globalThis.navigator.clipboard, 'write').rejects(denied); |
| 65 | + |
| 66 | + const error = await copyMarkdown('/guides/denied.md', '/').catch((reason: unknown) => reason); |
| 67 | + |
| 68 | + expect(error).to.equal(denied); |
| 69 | + expect(clipboard).to.equal('before'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('rejects with the refusal, and leaves no rejection unhandled, when the clipboard refuses before the fetch fails', async () => { |
| 73 | + sinon.stub(globalThis, 'fetch').resolves(new globalThis.Response('<html>Not Found</html>', { status: 404 })); |
| 74 | + const blocked = new globalThis.DOMException('Blocked by a permissions policy.', 'NotAllowedError'); |
| 75 | + sinon.stub(globalThis.navigator.clipboard, 'write').rejects(blocked); |
| 76 | + const unhandled: unknown[] = []; |
| 77 | + const record = (reason: unknown) => unhandled.push(reason); |
| 78 | + process.prependListener('unhandledRejection', record); |
| 79 | + |
| 80 | + const error = await copyMarkdown('/guides/blocked.md', '/').catch((reason: unknown) => reason); |
| 81 | + await setImmediate(); |
| 82 | + process.off('unhandledRejection', record); |
| 83 | + |
| 84 | + expect(error).to.equal(blocked); |
| 85 | + expect(unhandled).to.be.empty; |
| 86 | + }); |
| 87 | +}); |
0 commit comments