Skip to content

Commit 2f38c47

Browse files
committed
test(input-otp): improve autofill tests
1 parent f2e9166 commit 2f38c47

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

core/src/components/input-otp/test/basic/input-otp.e2e.ts

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import { expect } from '@playwright/test';
22
import type { Locator } from '@playwright/test';
33
import { configs, test } from '@utils/test/playwright';
44

5+
/**
6+
* Simulates an autofill event in an input element with the given value
7+
*/
8+
async function simulateAutofill(input: any, value: string) {
9+
await input.evaluate((input: any, value: string) => {
10+
(input as HTMLInputElement).value = value;
11+
input.dispatchEvent(new Event('input', { bubbles: true }));
12+
}, value);
13+
}
14+
515
/**
616
* Simulates a paste event in an input element with the given value
717
*/
@@ -429,37 +439,52 @@ configs({ modes: ['ios'] }).forEach(({ title, config }) => {
429439

430440
await verifyInputValues(inputOtp, ['1', '9', '3', '']);
431441
});
442+
});
432443

444+
test.describe(title('input-otp: autofill functionality'), () => {
433445
test('should handle autofill correctly', async ({ page }) => {
434446
await page.setContent(`<ion-input-otp>Description</ion-input-otp>`, config);
435447

436448
const firstInput = page.locator('ion-input-otp input').first();
449+
await firstInput.focus();
437450

438-
// Set the value in the 1st input directly and trigger input event
439-
// this simulates the value being set by autofill
440-
await firstInput.evaluate((input) => {
441-
(input as HTMLInputElement).value = '1234';
442-
input.dispatchEvent(new Event('input', { bubbles: true }));
443-
});
451+
await simulateAutofill(firstInput, '1234');
444452

445453
const inputOtp = page.locator('ion-input-otp');
446454
await verifyInputValues(inputOtp, ['1', '2', '3', '4']);
455+
456+
const lastInput = page.locator('ion-input-otp input').last();
457+
await expect(lastInput).toBeFocused();
447458
});
448459

449460
test('should handle autofill correctly when it exceeds the length', async ({ page }) => {
450461
await page.setContent(`<ion-input-otp>Description</ion-input-otp>`, config);
451462

452463
const firstInput = page.locator('ion-input-otp input').first();
464+
await firstInput.focus();
453465

454-
// Set the value in the 1st input directly and trigger input event
455-
// this simulates the value being set by autofill
456-
await firstInput.evaluate((input) => {
457-
(input as HTMLInputElement).value = '123456';
458-
input.dispatchEvent(new Event('input', { bubbles: true }));
459-
});
466+
await simulateAutofill(firstInput, '123456');
460467

461468
const inputOtp = page.locator('ion-input-otp');
462469
await verifyInputValues(inputOtp, ['1', '2', '3', '4']);
470+
471+
const lastInput = page.locator('ion-input-otp input').last();
472+
await expect(lastInput).toBeFocused();
473+
});
474+
475+
test('should handle autofill correctly when it is less than the length', async ({ page }) => {
476+
await page.setContent(`<ion-input-otp>Description</ion-input-otp>`, config);
477+
478+
const firstInput = page.locator('ion-input-otp input').first();
479+
await firstInput.focus();
480+
481+
await simulateAutofill(firstInput, '12');
482+
483+
const inputOtp = page.locator('ion-input-otp');
484+
await verifyInputValues(inputOtp, ['1', '2', '', '']);
485+
486+
const thirdInput = page.locator('ion-input-otp input').nth(2);
487+
await expect(thirdInput).toBeFocused();
463488
});
464489

465490
test('should handle autofill correctly when using autofill after typing 1 character', async ({ page }) => {
@@ -473,15 +498,27 @@ configs({ modes: ['ios'] }).forEach(({ title, config }) => {
473498
const secondInput = page.locator('ion-input-otp input').nth(1);
474499
await secondInput.focus();
475500

476-
// Set the value in the 2nd input directly and trigger input event
477-
// this simulates the value being set by autofill from the 2nd input
478-
await secondInput.evaluate((input) => {
479-
(input as HTMLInputElement).value = '1234';
480-
input.dispatchEvent(new Event('input', { bubbles: true }));
481-
});
501+
await simulateAutofill(secondInput, '1234');
482502

483503
const inputOtp = page.locator('ion-input-otp');
484504
await verifyInputValues(inputOtp, ['1', '2', '3', '4']);
505+
506+
const lastInput = page.locator('ion-input-otp input').last();
507+
await expect(lastInput).toBeFocused();
508+
});
509+
510+
test('should handle autofill correctly when autofill value contains invalid characters', async ({ page }) => {
511+
await page.setContent(`<ion-input-otp pattern="[a-zA-Z]">Description</ion-input-otp>`, config);
512+
513+
const firstInput = page.locator('ion-input-otp input').first();
514+
await firstInput.focus();
515+
516+
await simulateAutofill(firstInput, '1234');
517+
518+
const inputOtp = page.locator('ion-input-otp');
519+
await verifyInputValues(inputOtp, ['', '', '', '']);
520+
521+
await expect(firstInput).toBeFocused();
485522
});
486523
});
487524

0 commit comments

Comments
 (0)