|
| 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.locator('.g-md-editor-component'); |
| 16 | + this.editor = page.locator('.g-md-editor-component__editor'); |
| 17 | + |
| 18 | + this.settingsButton = page.getByTestId('md-settings-button'); |
| 19 | + this.settingsContent = page.getByTestId('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 | + return await this.locators.editor.evaluate((element) => { |
| 41 | + if (element.classList.contains('g-md-editor-component__editor_mode_wysiwyg')) { |
| 42 | + return 'wysiwyg'; |
| 43 | + } |
| 44 | + |
| 45 | + if (element.classList.contains('g-md-editor-component__editor_mode_markup')) { |
| 46 | + return 'markup'; |
| 47 | + } |
| 48 | + |
| 49 | + throw new Error('MarkdownEditorPage.getMode(): unknown editor mode'); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + async openSettingsPopup() { |
| 54 | + if (await this.locators.settingsContent.isVisible()) return; |
| 55 | + |
| 56 | + await this.locators.settingsButton.click(); |
| 57 | + await this.locators.settingsContent.waitFor({state: 'visible'}); |
| 58 | + } |
| 59 | + |
| 60 | + async setMode(mode: MarkdownEditorMode) { |
| 61 | + if ((await this.getMode()) === mode) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + await this.openSettingsPopup(); |
| 66 | + await this.locators.settingsContent.getByTestId(`md-settings-mode-${mode}`).click(); |
| 67 | + await this.assertMode(mode); |
| 68 | + } |
| 69 | + |
| 70 | + async assertMode(mode: MarkdownEditorMode) { |
| 71 | + await this.expect.poll(() => this.getMode()).toBe(mode); |
| 72 | + } |
| 73 | + |
| 74 | + async blur() { |
| 75 | + await this.locators.contenteditable.blur(); |
| 76 | + } |
| 77 | + |
| 78 | + async clearContent() { |
| 79 | + await this.locators.contenteditable.press('ControlOrMeta+A'); |
| 80 | + await this.locators.contenteditable.press('Backspace'); |
| 81 | + } |
| 82 | + |
| 83 | + async paste(data: PasteData) { |
| 84 | + await this.locators.contenteditable.evaluate((element, data) => { |
| 85 | + const clipboardData = new DataTransfer(); |
| 86 | + |
| 87 | + for (const [key, value] of Object.entries(data)) { |
| 88 | + clipboardData.setData(key, value); |
| 89 | + } |
| 90 | + |
| 91 | + element.focus(); |
| 92 | + element.dispatchEvent(new ClipboardEvent('paste', {clipboardData})); |
| 93 | + }, data); |
| 94 | + } |
| 95 | +} |
0 commit comments