Skip to content

Commit fe40bd8

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

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

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

Lines changed: 68 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,69 @@ 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+
const keyDown1 = new KeyboardEvent('keydown', {
930+
key: ' ',
931+
bubbles: true,
932+
repeat: false,
933+
});
934+
inputEl.dispatchEvent(keyDown1);
935+
vi.advanceTimersByTime(300);
936+
// start another long press while timer is running (simulating repeat keydown)
937+
const keyDown2 = new KeyboardEvent('keydown', {
938+
key: ' ',
939+
bubbles: true,
940+
repeat: true,
941+
});
942+
inputEl.dispatchEvent(keyDown2);
943+
vi.advanceTimersByTime(350);
944+
const keyUp = new KeyboardEvent('keyup', {
945+
key: ' ',
946+
bubbles: true,
947+
});
948+
inputEl.dispatchEvent(keyUp);
949+
vi.runAllTimers();
950+
});
951+
await Updates.next();
952+
953+
expect(getTextField(element).value).toEqual('+');
954+
955+
// pointer long press should reset suppressNextClick flag when click does not fire
956+
element.value = '';
957+
await Updates.next();
958+
const btn = getDigitButtons(element)[10] as Button;
959+
withFakeTimers(() => {
960+
simulatePointerLongPress(btn, {
961+
duration: 600,
962+
});
963+
// advance timers to execute the setTimeout in _endLongPress
964+
vi.advanceTimersByTime(1);
965+
});
966+
await Updates.next();
967+
968+
btn.click();
969+
await Updates.next();
970+
expect(getTextField(element).value).toEqual('+0');
971+
});
908972
});
909973
});

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)