-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(editor): component tests #3140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
10c49bb
initial setup
cassiozen 682a84d
Migrate e2e tests into component tests
cassiozen d4d8ebd
fix CI
cassiozen 0b6f8b3
fix mod
cassiozen 239fec9
address comment
cassiozen 56f08ee
fixes after rebase
cassiozen d4c913f
use docker image with built-in playwright
cassiozen 07e5f0a
removd test-specific editor
cassiozen ede1306
adds playwright path
cassiozen d27458e
refactor: reorganize editor integration tests
cassiozen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * Shared helpers for browser-level component tests. | ||
| * | ||
| * These utilities simulate clipboard and drag-drop interactions that require | ||
| * a real browser environment (DataTransfer, ClipboardEvent, etc.). | ||
| */ | ||
|
|
||
| /** | ||
| * Simulates pasting plain text into an element by dispatching a ClipboardEvent | ||
| * with the given text set as `text/plain` in the DataTransfer. | ||
| */ | ||
| export function pasteText(element: Element, text: string): void { | ||
| const dataTransfer = new DataTransfer(); | ||
| dataTransfer.setData('text/plain', text); | ||
| element.dispatchEvent( | ||
| new ClipboardEvent('paste', { | ||
| clipboardData: dataTransfer, | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Simulates pasting HTML content into an element by dispatching a ClipboardEvent | ||
| * with the given markup set as `text/html` in the DataTransfer. | ||
| */ | ||
| export function pasteHtml(element: Element, html: string): void { | ||
| const dataTransfer = new DataTransfer(); | ||
| dataTransfer.setData('text/html', html); | ||
| element.dispatchEvent( | ||
| new ClipboardEvent('paste', { | ||
| clipboardData: dataTransfer, | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }), | ||
| ); | ||
| } |
194 changes: 194 additions & 0 deletions
194
packages/editor/src/__tests__/editor-integration.browser.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import { page, userEvent } from 'vitest/browser'; | ||
| import { render } from 'vitest-browser-react'; | ||
| import { EmailEditor } from '../email-editor/email-editor'; | ||
| import { pasteHtml, pasteText } from './browser-test-helpers'; | ||
|
|
||
| const htmlTemplate = ` | ||
| <!doctype html> | ||
| <html> | ||
| <body> | ||
| <h1>Pasted heading</h1> | ||
| <p>Pasted body content</p> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| function getEditor() { | ||
| return page.getByRole('textbox'); | ||
| } | ||
|
|
||
| describe('editor integration (browser)', () => { | ||
| it('loads and content is editable', async () => { | ||
| render(<EmailEditor />); | ||
|
|
||
| const editor = getEditor(); | ||
| await expect.element(editor).toBeVisible(); | ||
| await expect.element(editor).toHaveAttribute('contenteditable', 'true'); | ||
|
|
||
| // Type content into the editor | ||
| await editor.click(); | ||
| await userEvent.keyboard('Hello world'); | ||
|
|
||
| // Content should be visible | ||
| await expect.element(editor).toHaveTextContent('Hello world'); | ||
| }); | ||
|
|
||
| it('slash command opens and inserts a heading', async () => { | ||
| render(<EmailEditor />); | ||
|
|
||
| const editor = getEditor(); | ||
| await editor.click(); | ||
|
|
||
| // Type "/" to trigger slash command menu | ||
| await userEvent.keyboard('/'); | ||
|
|
||
| // The slash command menu should be visible (rendered via portal to body) | ||
| const titleButton = page.getByRole('button', { | ||
| name: 'Title', | ||
| exact: true, | ||
| }); | ||
| await expect.element(titleButton).toBeVisible(); | ||
|
|
||
| // Click "Title" (H1) from the command menu | ||
| await titleButton.click(); | ||
|
|
||
| // Menu should close | ||
| await expect.element(titleButton).not.toBeInTheDocument(); | ||
|
|
||
| // A heading element should now exist in the editor | ||
| const editorEl = editor.element() as HTMLElement; | ||
| expect(editorEl.innerHTML).toMatch(/<h1/i); | ||
|
|
||
| // Type content into the heading | ||
| await userEvent.keyboard('E2E Heading Content'); | ||
|
|
||
| expect(editorEl.textContent).toContain('E2E Heading Content'); | ||
| }); | ||
|
|
||
| it('slash command inserts a bullet list', async () => { | ||
| render(<EmailEditor />); | ||
|
|
||
| const editor = getEditor(); | ||
| await editor.click(); | ||
|
|
||
| await userEvent.keyboard('/'); | ||
|
|
||
| const bulletListButton = page.getByRole('button', { name: 'Bullet list' }); | ||
| await expect.element(bulletListButton).toBeVisible(); | ||
|
|
||
| await bulletListButton.click(); | ||
|
|
||
| await expect.element(bulletListButton).not.toBeInTheDocument(); | ||
|
|
||
| const editorEl = editor.element() as HTMLElement; | ||
| expect(editorEl.innerHTML).toMatch(/<ul/i); | ||
|
|
||
| // Type list items | ||
| await userEvent.keyboard('First item'); | ||
| await userEvent.keyboard('{Enter}'); | ||
| await userEvent.keyboard('Second item'); | ||
|
|
||
| expect(editorEl.textContent).toContain('First item'); | ||
| expect(editorEl.textContent).toContain('Second item'); | ||
| }); | ||
|
|
||
| it('applies text formatting via keyboard shortcuts', async () => { | ||
| // Use Control on Linux (CI), Meta (Cmd) on macOS | ||
| const mod = navigator.platform.includes('Mac') ? 'Meta' : 'Control'; | ||
|
|
||
| render(<EmailEditor />); | ||
|
|
||
| const editor = getEditor(); | ||
| await editor.click(); | ||
|
|
||
| // Type and apply bold | ||
| await userEvent.keyboard('Bold text'); | ||
| await userEvent.keyboard(`{${mod}>}a{/${mod}}`); | ||
| await userEvent.keyboard(`{${mod}>}b{/${mod}}`); | ||
|
|
||
| const editorEl = editor.element() as HTMLElement; | ||
| expect(editorEl.innerHTML).toMatch(/<strong/i); | ||
|
|
||
| // Move to end, new line, type and apply italic | ||
| await userEvent.keyboard('{End}'); | ||
| await userEvent.keyboard('{Enter}'); | ||
| await userEvent.keyboard('Italic text'); | ||
| await userEvent.keyboard(`{${mod}>}a{/${mod}}`); | ||
| await userEvent.keyboard(`{${mod}>}i{/${mod}}`); | ||
|
|
||
| expect(editorEl.innerHTML).toMatch(/<em/i); | ||
|
|
||
| // Move to end, new line, type and apply underline | ||
| await userEvent.keyboard('{End}'); | ||
| await userEvent.keyboard('{Enter}'); | ||
| await userEvent.keyboard('Underlined text'); | ||
| await userEvent.keyboard(`{${mod}>}a{/${mod}}`); | ||
| await userEvent.keyboard(`{${mod}>}u{/${mod}}`); | ||
|
|
||
| expect(editorEl.innerHTML).toMatch(/<u[ >]/i); | ||
| }); | ||
|
|
||
| it('pasting plain text into an empty editor inserts text', async () => { | ||
| render(<EmailEditor />); | ||
|
|
||
| const editor = getEditor(); | ||
| await editor.click(); | ||
|
|
||
| const editorEl = editor.element() as HTMLElement; | ||
| pasteText(editorEl, 'hello world'); | ||
|
|
||
| await expect.element(editor).toHaveTextContent('hello world'); | ||
| }); | ||
|
|
||
| it('pasting plain text into a non-empty editor appends text', async () => { | ||
| render(<EmailEditor />); | ||
|
|
||
| const editor = getEditor(); | ||
| await editor.click(); | ||
| await userEvent.keyboard('existing'); | ||
|
|
||
| const editorEl = editor.element() as HTMLElement; | ||
| pasteText(editorEl, ' plus pasted'); | ||
|
|
||
| await expect.element(editor).toHaveTextContent('existing plus pasted'); | ||
| }); | ||
|
|
||
| it('pasting HTML into a non-empty document preserves existing content', async () => { | ||
| render( | ||
| <EmailEditor | ||
| content={{ | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'heading', | ||
| attrs: { level: 1 }, | ||
| content: [{ type: 'text', text: 'Existing heading' }], | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Existing paragraph' }], | ||
| }, | ||
| ], | ||
| }} | ||
| />, | ||
| ); | ||
|
|
||
| const editor = getEditor(); | ||
|
|
||
| // Click on the paragraph text to place cursor there, then move to end | ||
| const para = page.getByText('Existing paragraph'); | ||
| await para.click(); | ||
| await userEvent.keyboard('{End}'); | ||
|
|
||
| const editorEl = editor.element() as HTMLElement; | ||
| pasteHtml(editorEl, htmlTemplate); | ||
|
|
||
| // The original content should still be present | ||
| expect(editorEl.textContent).toContain('Existing heading'); | ||
| expect(editorEl.textContent).toContain('Existing paragraph'); | ||
|
|
||
| // The pasted content should also appear | ||
| expect(editorEl.textContent).toContain('Pasted heading'); | ||
| expect(editorEl.textContent).toContain('Pasted body content'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * Stub for `@react-email/render` and `@react-email/markdown` in the Vite | ||
| * browser test environment. These packages depend on Node-only modules | ||
| * (`prettier`, `md-to-react-email`) that Vite cannot resolve in the browser. | ||
| * | ||
| * The stubs satisfy the named exports that `@react-email/components` | ||
| * barrel-re-exports, so that extensions importing individual components | ||
| * (e.g. `{ Heading }`) from the components package can resolve correctly. | ||
| */ | ||
|
|
||
| // @react-email/render exports | ||
| export const plainTextSelectors = {}; | ||
| export const pretty = async (html: string) => html; | ||
| export const render = async () => ''; | ||
| export const renderAsync = async () => ''; | ||
| export const toPlainText = () => ''; | ||
|
|
||
| // @react-email/markdown exports | ||
| export const Markdown = () => null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { page } from 'vitest/browser'; | ||
| import { render } from 'vitest-browser-react'; | ||
| import { BubbleMenuItem } from './item'; | ||
|
|
||
| describe('BubbleMenuItem (browser)', () => { | ||
| it('renders with correct aria attributes when inactive', async () => { | ||
| render( | ||
| <BubbleMenuItem name="bold" isActive={false} onCommand={() => {}}> | ||
| <span>B</span> | ||
| </BubbleMenuItem>, | ||
| ); | ||
|
|
||
| const button = page.getByRole('button', { name: 'bold' }); | ||
| await expect.element(button).toBeVisible(); | ||
| await expect.element(button).toHaveAttribute('aria-pressed', 'false'); | ||
| }); | ||
|
|
||
| it('sets aria-pressed when active', async () => { | ||
| render( | ||
| <BubbleMenuItem name="bold" isActive={true} onCommand={() => {}}> | ||
| <span>B</span> | ||
| </BubbleMenuItem>, | ||
| ); | ||
|
|
||
| const button = page.getByRole('button', { name: 'bold' }); | ||
| await expect.element(button).toHaveAttribute('aria-pressed', 'true'); | ||
| }); | ||
|
|
||
| it('calls onCommand on click', async () => { | ||
| const onCommand = vi.fn(); | ||
| render( | ||
| <BubbleMenuItem name="bold" isActive={false} onCommand={onCommand}> | ||
| <span>B</span> | ||
| </BubbleMenuItem>, | ||
| ); | ||
|
|
||
| const button = page.getByRole('button', { name: 'bold' }); | ||
| await expect.element(button).toBeVisible(); | ||
| await button.click(); | ||
| expect(onCommand).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.