|
| 1 | +import { test, expect, type Page } from '@playwright/test'; |
| 2 | + |
| 3 | +const EDITOR_SELECTOR = |
| 4 | + '[data-testid="test-submit-root"] .eti-editor [contenteditable="true"]'; |
| 5 | +const SUBMIT_PAGE = '/test-submit-props'; |
| 6 | +const SUBMIT_LOG_SELECTOR = '[data-testid="submit-log"]'; |
| 7 | +const TYPE_DELAY_MS = 50; |
| 8 | + |
| 9 | +interface SubmitLogSnapshot { |
| 10 | + submitCount: number; |
| 11 | + text: string; |
| 12 | +} |
| 13 | + |
| 14 | +async function readSubmitLog(page: Page): Promise<SubmitLogSnapshot> { |
| 15 | + const raw = await page.locator(SUBMIT_LOG_SELECTOR).innerText(); |
| 16 | + return JSON.parse(raw.trim()) as SubmitLogSnapshot; |
| 17 | +} |
| 18 | + |
| 19 | +async function expectSubmitLog( |
| 20 | + page: Page, |
| 21 | + expected: SubmitLogSnapshot |
| 22 | +): Promise<void> { |
| 23 | + await expect.poll(async () => readSubmitLog(page)).toStrictEqual(expected); |
| 24 | +} |
| 25 | + |
| 26 | +test.describe('submit and keyboard props', () => { |
| 27 | + test('newline mode inserts a paragraph break on Enter', async ({ page }) => { |
| 28 | + await page.goto(`${SUBMIT_PAGE}?mode=newline`); |
| 29 | + await page.waitForSelector(EDITOR_SELECTOR); |
| 30 | + |
| 31 | + const editor = page.locator(EDITOR_SELECTOR); |
| 32 | + await editor.click(); |
| 33 | + await expect(editor).toBeFocused(); |
| 34 | + |
| 35 | + await editor.pressSequentially(`Hello`, { delay: TYPE_DELAY_MS }); |
| 36 | + await editor.press('Enter'); |
| 37 | + await editor.pressSequentially(`World`, { delay: TYPE_DELAY_MS }); |
| 38 | + |
| 39 | + await expect(page.locator('.eti-editor p')).toHaveCount(2); |
| 40 | + await expect(page.locator(SUBMIT_LOG_SELECTOR)).toContainText( |
| 41 | + '"submitCount":0' |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + test('submit mode fires onSubmitEditing without adding a paragraph', async ({ |
| 46 | + page, |
| 47 | + }) => { |
| 48 | + await page.goto(`${SUBMIT_PAGE}?mode=submit`); |
| 49 | + await page.waitForSelector(EDITOR_SELECTOR); |
| 50 | + |
| 51 | + const editor = page.locator(EDITOR_SELECTOR); |
| 52 | + await editor.click(); |
| 53 | + await expect(editor).toBeFocused(); |
| 54 | + |
| 55 | + await editor.pressSequentially(`Hi`, { delay: TYPE_DELAY_MS }); |
| 56 | + await editor.press('Enter'); |
| 57 | + |
| 58 | + await expectSubmitLog(page, { |
| 59 | + submitCount: 1, |
| 60 | + text: 'Hi', |
| 61 | + }); |
| 62 | + |
| 63 | + await expect(page.locator('.eti-editor p')).toHaveCount(1); |
| 64 | + }); |
| 65 | + |
| 66 | + test('blurAndSubmit fires onSubmitEditing without adding a paragraph then blurs', async ({ |
| 67 | + page, |
| 68 | + }) => { |
| 69 | + await page.goto(`${SUBMIT_PAGE}?mode=blurAndSubmit`); |
| 70 | + await page.waitForSelector(EDITOR_SELECTOR); |
| 71 | + |
| 72 | + const editor = page.locator(EDITOR_SELECTOR); |
| 73 | + await editor.click(); |
| 74 | + await expect(editor).toBeFocused(); |
| 75 | + |
| 76 | + await editor.pressSequentially('x', { delay: TYPE_DELAY_MS }); |
| 77 | + await editor.press('Enter'); |
| 78 | + |
| 79 | + await expectSubmitLog(page, { |
| 80 | + submitCount: 1, |
| 81 | + text: 'x', |
| 82 | + }); |
| 83 | + |
| 84 | + await expect(page.locator('.eti-editor p')).toHaveCount(1); |
| 85 | + await expect(editor).not.toBeFocused(); |
| 86 | + }); |
| 87 | + |
| 88 | + test('returnKeyType sets enterkeyhint on the editable root', async ({ |
| 89 | + page, |
| 90 | + }) => { |
| 91 | + await page.goto(`${SUBMIT_PAGE}?enterKeyTest=1&returnKeyType=search`); |
| 92 | + await page.waitForSelector(EDITOR_SELECTOR); |
| 93 | + |
| 94 | + const editor = page.locator(EDITOR_SELECTOR); |
| 95 | + await expect(editor).toHaveAttribute('enterkeyhint', 'search'); |
| 96 | + }); |
| 97 | +}); |
0 commit comments