Skip to content

Commit 2a71a24

Browse files
committed
VIV-2876 add unit test
1 parent e9927b2 commit 2a71a24

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

libs/components/src/lib/dial-pad/dial-pad.spec.ts

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ describe('vwc-dial-pad', () => {
3434
return getTextField(component).querySelector('vwc-button')!;
3535
}
3636

37+
function getZeroButton(component: HTMLElement) {
38+
return getDigitButtons(component)[10] as Button;
39+
}
40+
3741
async function setValue(value: string) {
3842
element.value = value;
3943
await Updates.next();
@@ -565,10 +569,6 @@ describe('vwc-dial-pad', () => {
565569
});
566570

567571
describe('long press on 0', () => {
568-
function getZeroButton(component: HTMLElement) {
569-
return getDigitButtons(component)[10] as Button;
570-
}
571-
572572
it('should add "0" when tapping 0', async () => {
573573
getZeroButton(element).click();
574574
await Updates.next();
@@ -905,5 +905,71 @@ describe('vwc-dial-pad', () => {
905905

906906
expect(getTextField(element).value).toEqual(' ');
907907
});
908+
909+
it('should not start keyboard long press when disabled', async () => {
910+
element.pattern = '^\\+?[0-9#*]*$';
911+
element.disabled = true;
912+
await Updates.next();
913+
const inputEl = getInput(element);
914+
withFakeTimers(() => {
915+
simulateKeyboardLongPress(inputEl, { pressDuration: 650 });
916+
});
917+
await Updates.next();
918+
919+
expect(getTextField(element).value).toEqual('');
920+
});
921+
922+
it('should handle edge cases: prevent timer restart and reset "skip next click" flag when click does not fire', async () => {
923+
element.pattern = '^\\+?[0-9#*]*$';
924+
await Updates.next();
925+
926+
// keyboard long press timer should not restart if already running
927+
const inputEl = getInput(element);
928+
withFakeTimers(() => {
929+
// Start first long press
930+
const keyDown1 = new KeyboardEvent('keydown', {
931+
key: ' ',
932+
bubbles: true,
933+
repeat: false,
934+
});
935+
inputEl.dispatchEvent(keyDown1);
936+
vi.advanceTimersByTime(300);
937+
// start another long press while timer is running (should be prevented)
938+
const keyDown2 = new KeyboardEvent('keydown', {
939+
key: ' ',
940+
bubbles: true,
941+
repeat: false,
942+
});
943+
inputEl.dispatchEvent(keyDown2);
944+
vi.advanceTimersByTime(350);
945+
const keyUp = new KeyboardEvent('keyup', {
946+
key: ' ',
947+
bubbles: true,
948+
});
949+
inputEl.dispatchEvent(keyUp);
950+
vi.runAllTimers();
951+
});
952+
await Updates.next();
953+
954+
// Should only add one '+' from the first long press
955+
expect(getTextField(element).value).toEqual('+');
956+
957+
// pointer long press should reset suppressNextClick flag when click does not fire
958+
element.value = '';
959+
await Updates.next();
960+
const btn = getDigitButtons(element)[10] as Button;
961+
withFakeTimers(() => {
962+
simulatePointerLongPress(btn, {
963+
duration: 600,
964+
});
965+
// advance timers to execute the setTimeout in _endLongPress
966+
vi.advanceTimersByTime(1);
967+
});
968+
await Updates.next();
969+
970+
btn.click();
971+
await Updates.next();
972+
expect(getTextField(element).value).toEqual('+0');
973+
});
908974
});
909975
});

libs/components/src/lib/dial-pad/dial-pad.template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function handleKeyUp(x: DialPad, e: KeyboardEvent) {
109109
e.preventDefault();
110110

111111
const wasLongPress = x._endKeyboardLongPress();
112-
if (!wasLongPress) {
112+
if (!wasLongPress && !x.disabled && !x.callActive) {
113113
// Short press - add space character (normal space input)
114114
x.value += ' ';
115115
x.$emit('input');

0 commit comments

Comments
 (0)