|
1 |
| -import { test } from '@stencil/playwright'; |
| 1 | +import { type E2EPage, test } from '@stencil/playwright'; |
2 | 2 | import { testInputCallbacksAndEvents, testInputValueReflection } from '../../e2e';
|
3 |
| - |
| 3 | +import type { FillAction } from '../../e2e/utils/FillAction'; |
| 4 | +import type { Page } from '@playwright/test'; |
| 5 | +import { expect } from '@playwright/test'; |
| 6 | +import { KolEvent } from '../../utils/events'; |
4 | 7 | const COMPONENT_NAME = 'kol-input-color';
|
5 | 8 | const TEST_VALUE = '#cc006e';
|
6 |
| - |
| 9 | +const NEW_VALUE = '#00ccff'; |
| 10 | +const fillAction: FillAction = async (page) => { |
| 11 | + const textInput = page.locator('input[type="text"]'); |
| 12 | + await textInput.fill(TEST_VALUE); |
| 13 | + await textInput.dispatchEvent('input'); |
| 14 | +}; |
| 15 | +const selectTextInput = (page: Page & E2EPage) => page.locator('input[type="text"]'); |
| 16 | +const selectColorInput = (page: Page & E2EPage) => page.locator('input[type="color"]'); |
7 | 17 | test.describe(COMPONENT_NAME, () => {
|
8 |
| - testInputValueReflection<HTMLKolInputColorElement>(COMPONENT_NAME, TEST_VALUE); |
9 |
| - testInputCallbacksAndEvents<HTMLKolInputColorElement>(COMPONENT_NAME, TEST_VALUE); |
| 18 | + testInputValueReflection<HTMLKolInputColorElement>(COMPONENT_NAME, TEST_VALUE, fillAction); |
| 19 | + testInputCallbacksAndEvents<HTMLKolInputColorElement>(COMPONENT_NAME, TEST_VALUE, fillAction, ['input'], undefined, selectTextInput); |
| 20 | + test('should sync value between color input and text input', async ({ page }) => { |
| 21 | + await page.setContent(`<${COMPONENT_NAME} _label="Color Picker"></${COMPONENT_NAME}>`); |
| 22 | + const colorInput = selectColorInput(page); |
| 23 | + const textInput = selectTextInput(page); |
| 24 | + await colorInput.fill(TEST_VALUE); |
| 25 | + await expect(textInput).toHaveValue(TEST_VALUE); |
| 26 | + await textInput.fill(NEW_VALUE); |
| 27 | + await expect(colorInput).toHaveValue(NEW_VALUE); |
| 28 | + }); |
| 29 | + test.describe('Callbacks', () => { |
| 30 | + test(`should call onChange callback when internal input emits`, async ({ page }) => { |
| 31 | + await page.setContent(`<${COMPONENT_NAME} _label="Color Picker"></${COMPONENT_NAME}>`); |
| 32 | + const component = page.locator(COMPONENT_NAME); |
| 33 | + const textInput = selectTextInput(page); |
| 34 | + const callbackPromise = component.evaluate((element: HTMLKolInputColorElement) => { |
| 35 | + return new Promise<unknown>((resolve) => { |
| 36 | + element._on = { |
| 37 | + onChange: (_event: Event, value?: unknown) => { |
| 38 | + resolve(value); |
| 39 | + }, |
| 40 | + }; |
| 41 | + }); |
| 42 | + }); |
| 43 | + await page.waitForChanges(); |
| 44 | + await fillAction(page); |
| 45 | + await page.waitForChanges(); |
| 46 | + await textInput.dispatchEvent('change'); |
| 47 | + await expect(callbackPromise).resolves.toBe(TEST_VALUE); |
| 48 | + }); |
| 49 | + }); |
| 50 | + test.describe('DOM events', () => { |
| 51 | + test(`should emit change when internal input emits`, async ({ page }) => { |
| 52 | + await page.setContent(`<${COMPONENT_NAME} _label="Color Picker"></${COMPONENT_NAME}>`); |
| 53 | + const component = page.locator(COMPONENT_NAME); |
| 54 | + const textInput = selectTextInput(page); |
| 55 | + const eventPromise = component.evaluate((element: HTMLKolInputColorElement, KolEvent) => { |
| 56 | + return new Promise<unknown>((resolve) => { |
| 57 | + element.addEventListener(KolEvent.change, (event: Event) => { |
| 58 | + resolve((event as CustomEvent).detail); |
| 59 | + }); |
| 60 | + }); |
| 61 | + }, KolEvent); |
| 62 | + await page.waitForChanges(); |
| 63 | + await fillAction(page); |
| 64 | + await page.waitForChanges(); |
| 65 | + await textInput.dispatchEvent('change'); |
| 66 | + await expect(eventPromise).resolves.toBe(TEST_VALUE); |
| 67 | + }); |
| 68 | + }); |
10 | 69 | });
|
0 commit comments