|
| 1 | +import { Editor } from '@tiptap/core'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { EmailTheming } from '../plugins/email-theming/extension'; |
| 4 | +import { StarterKit } from './index'; |
| 5 | + |
| 6 | +vi.mock('@tiptap/react', () => ({ |
| 7 | + ReactNodeViewRenderer: () => () => null, |
| 8 | + useEditorState: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock('tippy.js', () => ({ |
| 12 | + default: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('@/env', () => ({ |
| 16 | + env: new Proxy( |
| 17 | + {}, |
| 18 | + { |
| 19 | + get: () => '', |
| 20 | + }, |
| 21 | + ), |
| 22 | +})); |
| 23 | + |
| 24 | +function docWithLink(style?: string) { |
| 25 | + const attrs: Record<string, unknown> = { href: 'https://resend.com' }; |
| 26 | + if (style !== undefined) { |
| 27 | + attrs.style = style; |
| 28 | + } |
| 29 | + return { |
| 30 | + type: 'doc', |
| 31 | + content: [ |
| 32 | + { |
| 33 | + type: 'paragraph', |
| 34 | + content: [ |
| 35 | + { |
| 36 | + type: 'text', |
| 37 | + marks: [{ type: 'link', attrs }], |
| 38 | + text: 'click', |
| 39 | + }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + ], |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function createEditor(theme: 'basic' | 'minimal', content = docWithLink()) { |
| 47 | + return new Editor({ |
| 48 | + extensions: [StarterKit, EmailTheming.configure({ theme })], |
| 49 | + content, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +function findLinkMark(editor: Editor) { |
| 54 | + const walk = (nodes: unknown[]): Record<string, unknown> | undefined => { |
| 55 | + for (const n of nodes) { |
| 56 | + const node = n as Record<string, unknown>; |
| 57 | + const marks = node.marks as Array<Record<string, unknown>> | undefined; |
| 58 | + const linkMark = marks?.find((m) => m.type === 'link'); |
| 59 | + if (linkMark) return linkMark; |
| 60 | + const children = node.content as unknown[] | undefined; |
| 61 | + if (children) { |
| 62 | + const found = walk(children); |
| 63 | + if (found) return found; |
| 64 | + } |
| 65 | + } |
| 66 | + return undefined; |
| 67 | + }; |
| 68 | + return walk((editor.getJSON().content ?? []) as unknown[]); |
| 69 | +} |
| 70 | + |
| 71 | +const COLOR_RE = /color:\s*#0670DB/i; |
| 72 | +const UNDERLINE_RE = /text-decoration:\s*underline/i; |
| 73 | + |
| 74 | +describe('Link mark theming', () => { |
| 75 | + let editor: Editor; |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + editor?.destroy(); |
| 79 | + document.head |
| 80 | + .querySelectorAll('style[id^="tiptap-theme-"]') |
| 81 | + .forEach((node) => { |
| 82 | + node.remove(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('emits theme-resolved color and text-decoration on plain links (basic)', () => { |
| 87 | + editor = createEditor('basic'); |
| 88 | + const html = editor.getHTML(); |
| 89 | + expect(html).toMatch(COLOR_RE); |
| 90 | + expect(html).toMatch(UNDERLINE_RE); |
| 91 | + }); |
| 92 | + |
| 93 | + it('emits theme-resolved color and text-decoration on plain links (minimal)', () => { |
| 94 | + editor = createEditor('minimal'); |
| 95 | + const html = editor.getHTML(); |
| 96 | + expect(html).toMatch(COLOR_RE); |
| 97 | + expect(html).toMatch(UNDERLINE_RE); |
| 98 | + }); |
| 99 | + |
| 100 | + it('preserves class="node-link" in the rendered output', () => { |
| 101 | + editor = createEditor('basic'); |
| 102 | + expect(editor.getHTML()).toContain('class="node-link"'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('lets user-specified color win over the theme color', () => { |
| 106 | + editor = createEditor('basic', docWithLink('color: red')); |
| 107 | + const html = editor.getHTML(); |
| 108 | + expect(html).toMatch(/color:\s*red/i); |
| 109 | + expect(html).not.toMatch(COLOR_RE); |
| 110 | + expect(html).toMatch(UNDERLINE_RE); |
| 111 | + }); |
| 112 | + |
| 113 | + it('keeps mark.attrs.style empty for plain links (inspector contract)', () => { |
| 114 | + editor = createEditor('basic'); |
| 115 | + const mark = findLinkMark(editor); |
| 116 | + expect(mark?.type).toBe('link'); |
| 117 | + expect((mark?.attrs as Record<string, unknown>).style).toBe(''); |
| 118 | + }); |
| 119 | + |
| 120 | + it('round-trips a plain <a href> through setContent+getHTML with themed style', () => { |
| 121 | + editor = createEditor('basic'); |
| 122 | + editor.commands.setContent( |
| 123 | + '<p><a href="https://resend.com">click</a></p>', |
| 124 | + { emitUpdate: true }, |
| 125 | + ); |
| 126 | + const html = editor.getHTML(); |
| 127 | + expect(html).toMatch(COLOR_RE); |
| 128 | + expect(html).toMatch(UNDERLINE_RE); |
| 129 | + }); |
| 130 | +}); |
0 commit comments