|
| 1 | +import {test, expect} from '@playwright/test'; |
| 2 | + |
| 3 | +let componentId; |
| 4 | + |
| 5 | +async function createTextField(page, config) { |
| 6 | + const result = await page.evaluate(async (config) => { |
| 7 | + return Neo.worker.App.createNeoInstance({ |
| 8 | + importPath: '../form/field/Text.mjs', |
| 9 | + ntype : 'textfield', |
| 10 | + parentId : 'component-test-viewport', |
| 11 | + labelText : 'My label', |
| 12 | + labelWidth: 80, |
| 13 | + width : 300, |
| 14 | + ...config |
| 15 | + }); |
| 16 | + }, config); |
| 17 | + |
| 18 | + if (!result.success) { |
| 19 | + throw new Error(`Component creation failed: ${result.error.message}`); |
| 20 | + } |
| 21 | + |
| 22 | + return result.id; |
| 23 | +} |
| 24 | + |
| 25 | +test.describe('Neo.form.field.Text', () => { |
| 26 | + test.beforeEach(async ({page}) => { |
| 27 | + await page.goto('/test/playwright/component/apps/empty-viewport/index.html'); |
| 28 | + await page.waitForSelector('#component-test-viewport'); |
| 29 | + }); |
| 30 | + |
| 31 | + test.afterEach(async ({page}) => { |
| 32 | + if (componentId) { |
| 33 | + await page.evaluate(async (id) => { |
| 34 | + const result = await Neo.worker.App.destroyNeoInstance(id); |
| 35 | + if (!result.success) { |
| 36 | + console.error(`Failed to destroy component ${id}:`, result.error); |
| 37 | + } |
| 38 | + }, componentId); |
| 39 | + componentId = null; |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + test('Check isTouched on focusLeave', async ({page}) => { |
| 44 | + componentId = await createTextField(page, {value: 'Hello World!'}); |
| 45 | + const field = page.locator(`#${componentId}`); |
| 46 | + const input = field.locator('input'); |
| 47 | + |
| 48 | + await expect(input).toHaveValue('Hello World!'); |
| 49 | + await expect(field).not.toHaveClass(/neo-is-touched/); |
| 50 | + |
| 51 | + await input.click(); |
| 52 | + await page.locator('#component-test-viewport').click(); // Click outside to blur |
| 53 | + |
| 54 | + await expect(field).toHaveClass(/neo-is-touched/); |
| 55 | + }); |
| 56 | + |
| 57 | + test('Check isTouched on focusEnter', async ({page}) => { |
| 58 | + componentId = await createTextField(page, {isTouchedEvent: 'focusEnter', value: 'Hello World!'}); |
| 59 | + const field = page.locator(`#${componentId}`); |
| 60 | + const input = field.locator('input'); |
| 61 | + |
| 62 | + await expect(input).toHaveValue('Hello World!'); |
| 63 | + await expect(field).not.toHaveClass(/neo-is-touched/); |
| 64 | + |
| 65 | + await input.click(); |
| 66 | + |
| 67 | + await expect(field).toHaveClass(/neo-is-touched/); |
| 68 | + }); |
| 69 | + |
| 70 | + test('Check isTouched set initially', async ({page}) => { |
| 71 | + componentId = await createTextField(page, {isTouched: true, value: 'Hello World!'}); |
| 72 | + const field = page.locator(`#${componentId}`); |
| 73 | + const input = field.locator('input'); |
| 74 | + |
| 75 | + await expect(input).toHaveValue('Hello World!'); |
| 76 | + await expect(field).toHaveClass(/neo-is-touched/); |
| 77 | + }); |
| 78 | + |
| 79 | + test('Label Positions', async ({page}) => { |
| 80 | + componentId = await createTextField(page); |
| 81 | + const field = page.locator(`#${componentId}`); |
| 82 | + const label = field.locator('.neo-textfield-label'); |
| 83 | + |
| 84 | + // left (default) |
| 85 | + await expect(label).toBeVisible(); |
| 86 | + |
| 87 | + // top |
| 88 | + await page.evaluate(id => Neo.worker.App.setConfigs({id, labelPosition: 'top'}), componentId); |
| 89 | + await expect(field).toHaveClass(/label-top/); |
| 90 | + |
| 91 | + // bottom |
| 92 | + await page.evaluate(id => Neo.worker.App.setConfigs({id, labelPosition: 'bottom'}), componentId); |
| 93 | + await expect(field).toHaveClass(/label-bottom/); |
| 94 | + |
| 95 | + // right |
| 96 | + await page.evaluate(id => Neo.worker.App.setConfigs({id, labelPosition: 'right'}), componentId); |
| 97 | + await expect(field).toHaveClass(/label-right/); |
| 98 | + |
| 99 | + // inline |
| 100 | + await page.evaluate(id => Neo.worker.App.setConfigs({id, labelPosition: 'inline'}), componentId); |
| 101 | + await expect(field).toHaveClass(/label-inline/); |
| 102 | + }); |
| 103 | +}); |
0 commit comments