|
| 1 | +import type {Expect, Page} from '@playwright/test'; |
| 2 | + |
| 3 | +import type {DataTransferType, MarkdownEditorMode} from 'src'; |
| 4 | + |
| 5 | +class MarkdownEditorLocators { |
| 6 | + readonly component; |
| 7 | + readonly editor; |
| 8 | + |
| 9 | + readonly settingsButton; |
| 10 | + readonly settingsContent; |
| 11 | + |
| 12 | + readonly contenteditable; |
| 13 | + |
| 14 | + constructor(page: Page) { |
| 15 | + this.component = page.getByTestId('playground-md-editor'); |
| 16 | + this.editor = page.getByTestId('g-md-editor-mode'); |
| 17 | + |
| 18 | + this.settingsButton = page.getByTestId('g-md-settings-button'); |
| 19 | + this.settingsContent = page.getByTestId('g-md-settings-content'); |
| 20 | + |
| 21 | + this.contenteditable = this.editor.locator('[contenteditable=true]'); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +type PasteData = Partial<Record<DataTransferType, string>>; |
| 26 | + |
| 27 | +export class MarkdownEditorPage { |
| 28 | + protected readonly page: Page; |
| 29 | + protected readonly expect: Expect; |
| 30 | + protected readonly locators; |
| 31 | + |
| 32 | + constructor(page: Page, expect: Expect) { |
| 33 | + this.page = page; |
| 34 | + this.expect = expect; |
| 35 | + |
| 36 | + this.locators = new MarkdownEditorLocators(page); |
| 37 | + } |
| 38 | + |
| 39 | + async getMode(): Promise<MarkdownEditorMode> { |
| 40 | + const value = await this.locators.editor.getAttribute('data-mode'); |
| 41 | + const mode = value as MarkdownEditorMode | null; |
| 42 | + if (mode === 'wysiwyg' || mode === 'markup') return mode; |
| 43 | + throw new Error(`MarkdownEditorPage.getMode(): unknown editor mode "${mode}"`); |
| 44 | + } |
| 45 | + |
| 46 | + async openSettingsPopup() { |
| 47 | + if (await this.locators.settingsContent.isVisible()) return; |
| 48 | + |
| 49 | + await this.locators.settingsButton.click(); |
| 50 | + await this.locators.settingsContent.waitFor({state: 'visible'}); |
| 51 | + } |
| 52 | + |
| 53 | + async switchMode(mode: MarkdownEditorMode) { |
| 54 | + if ((await this.getMode()) === mode) return; |
| 55 | + |
| 56 | + await this.openSettingsPopup(); |
| 57 | + await this.locators.settingsContent.getByTestId(`md-settings-mode-${mode}`).click(); |
| 58 | + await this.assertMode(mode); |
| 59 | + } |
| 60 | + |
| 61 | + async assertMode(mode: MarkdownEditorMode) { |
| 62 | + await this.expect.poll(() => this.getMode()).toBe(mode); |
| 63 | + } |
| 64 | + |
| 65 | + async blur() { |
| 66 | + await this.locators.contenteditable.blur(); |
| 67 | + } |
| 68 | + |
| 69 | + async press(key: string) { |
| 70 | + await this.locators.contenteditable.press(key); |
| 71 | + } |
| 72 | + |
| 73 | + async clearContent() { |
| 74 | + await this.press('ControlOrMeta+A'); |
| 75 | + await this.press('Backspace'); |
| 76 | + } |
| 77 | + |
| 78 | + async paste(value: PasteData | string) { |
| 79 | + const data: PasteData = typeof value === 'string' ? {'text/plain': value} : value; |
| 80 | + |
| 81 | + await this.locators.contenteditable.evaluate((element, data) => { |
| 82 | + const clipboardData = new DataTransfer(); |
| 83 | + |
| 84 | + for (const [key, value] of Object.entries(data)) { |
| 85 | + clipboardData.setData(key, value); |
| 86 | + } |
| 87 | + |
| 88 | + element.focus(); |
| 89 | + element.dispatchEvent(new ClipboardEvent('paste', {clipboardData})); |
| 90 | + }, data); |
| 91 | + } |
| 92 | + |
| 93 | + async fill(text: string) { |
| 94 | + this.locators.contenteditable.fill(text); |
| 95 | + } |
| 96 | + |
| 97 | + async selectTextIn(selector?: string) { |
| 98 | + let loc = this.locators.contenteditable; |
| 99 | + if (selector) loc = loc.locator(selector); |
| 100 | + |
| 101 | + loc.selectText(); |
| 102 | + await this.page.waitForTimeout(100); |
| 103 | + } |
| 104 | +} |
0 commit comments