| import { userEvent } from 'vitest/browser' | |
| import otpStyles from '../../../scss/forms/_otp-input.scss?inline' | |
| import OtpInput from '../../src/otp-input.js' | |
| import { clearFixture, createEvent, getFixture } from '../helpers/fixture.js' | |
| describe('OtpInput', () => { | |
| let fixtureEl | |
| beforeAll(() => { | |
| fixtureEl = getFixture() | |
| }) | |
| afterEach(() => { | |
| clearFixture() | |
| }) | |
| const getOtpHtml = (attributes = '', maxlength = 6) => | |
| `<div class="otp" ${attributes}><input type="text" class="otp-input" maxlength="${maxlength}"></div>` | |
| const typeInto = (input, value) => { | |
| input.value = value | |
| input.dispatchEvent(createEvent('input')) | |
| } | |
| // The compiled CSS lays the slots out horizontally, but the unit test DOM has | |
| // no stylesheet so the generated `<div>`s stack vertically (identical x-range). | |
| // Force an inline horizontal layout so coordinate-based hit testing is | |
| // deterministic when exercising pointer/tap behavior. | |
| const layoutSlotsHorizontally = (otpEl, slotWidth = 30) => { | |
| for (const slot of otpEl.querySelectorAll('.otp-slot, .otp-separator')) { | |
| slot.style.display = 'inline-block' | |
| slot.style.width = `${slotWidth}px` | |
| slot.style.margin = '0' | |
| slot.style.padding = '0' | |
| } | |
| } | |
| const pointerDownOnSlot = (input, slot) => { | |
| const { left, width } = slot.getBoundingClientRect() | |
| input.dispatchEvent(new MouseEvent('pointerdown', { | |
| bubbles: true, cancelable: true, clientX: left + (width / 2) | |
| })) | |
| } | |
| const beforeInput = (input, options) => | |
| input.dispatchEvent(new InputEvent('beforeinput', { bubbles: true, cancelable: true, ...options })) | |
| const pasteInto = (input, value) => { | |
| const event = new Event('paste', { bubbles: true, cancelable: true }) | |
| event.clipboardData = { getData: () => value } | |
| input.dispatchEvent(event) | |
| // Emulate the browser's default action. It applies maxlength before the | |
| // input event lets the component sanitize the value. | |
| if (!event.defaultPrevented && !input.readOnly && !input.disabled && value) { | |
| const start = input.selectionStart ?? input.value.length | |
| const end = input.selectionEnd ?? start | |
| const available = Math.max(0, input.maxLength - (input.value.length - (end - start))) | |
| input.setRangeText(value.slice(0, available), start, end, 'end') | |
| input.dispatchEvent(new InputEvent('input', { bubbles: true, inputType: 'insertFromPaste' })) | |
| } | |
| return event | |
| } | |
| describe('VERSION', () => { | |
| it('should return plugin version', () => { | |
| expect(OtpInput.VERSION).toEqual(jasmine.any(String)) | |
| }) | |
| }) | |
| describe('DATA_KEY', () => { | |
| it('should return plugin data key', () => { | |
| expect(OtpInput.DATA_KEY).toEqual('bs.otpInput') | |
| }) | |
| }) | |
| describe('Default', () => { | |
| it('should return default config', () => { | |
| expect(OtpInput.Default).toEqual(jasmine.any(Object)) | |
| expect(OtpInput.Default.mask).toBeFalse() | |
| expect(OtpInput.Default.type).toEqual('numeric') | |
| }) | |
| }) | |
| describe('DefaultType', () => { | |
| it('should return default type config', () => { | |
| expect(OtpInput.DefaultType).toEqual(jasmine.any(Object)) | |
| }) | |
| }) | |
| describe('constructor', () => { | |
| it('should take care of element either passed as a CSS selector or DOM element', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otpBySelector = new OtpInput('.otp') | |
| expect(otpBySelector._element).toEqual(otpEl) | |
| const otpByElement = new OtpInput(otpEl) | |
| expect(otpByElement._element).toEqual(otpEl) | |
| }) | |
| it('should set up the input with correct attributes', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| expect(input.getAttribute('maxlength')).toEqual('6') | |
| expect(input.getAttribute('inputmode')).toEqual('numeric') | |
| expect(input.getAttribute('pattern')).toEqual('[0-9]*') | |
| expect(input.getAttribute('autocomplete')).toEqual('one-time-code') | |
| }) | |
| it('should render one slot per character and mark the container as rendered', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| expect(otpEl).toHaveClass('otp-rendered') | |
| expect(otpEl.querySelectorAll('.otp-slot').length).toEqual(6) | |
| }) | |
| it('should derive length from the maxlength attribute', () => { | |
| fixtureEl.innerHTML = getOtpHtml('', 4) | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| expect(otpEl.querySelectorAll('.otp-slot').length).toEqual(4) | |
| }) | |
| it('should let the length option override the maxlength attribute', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl, { length: 4 }) | |
| expect(otp).toBeInstanceOf(OtpInput) | |
| const input = otpEl.querySelector('input') | |
| expect(input.getAttribute('maxlength')).toEqual('4') | |
| expect(otpEl.querySelectorAll('.otp-slot').length).toEqual(4) | |
| }) | |
| it('should set a text inputmode and pattern for the alphanumeric type', () => { | |
| fixtureEl.innerHTML = getOtpHtml('data-bs-type="alphanumeric"') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| expect(input.getAttribute('inputmode')).toEqual('text') | |
| expect(input.getAttribute('pattern')).toEqual('[A-Za-z0-9]*') | |
| }) | |
| it('should set a text inputmode and pattern for the alpha type and strip digits', () => { | |
| fixtureEl.innerHTML = getOtpHtml('data-bs-type="alpha"') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| expect(input.getAttribute('inputmode')).toEqual('text') | |
| expect(input.getAttribute('pattern')).toEqual('[A-Za-z]*') | |
| otp.setValue('a1B2c3') | |
| expect(otp.getValue()).toEqual('aBc') | |
| }) | |
| it('should fall back to the numeric type for an unknown type', () => { | |
| fixtureEl.innerHTML = getOtpHtml('data-bs-type="hex"') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| expect(input.getAttribute('inputmode')).toEqual('numeric') | |
| expect(input.getAttribute('pattern')).toEqual('[0-9]*') | |
| otp.setValue('1a2b3c') | |
| expect(otp.getValue()).toEqual('123') | |
| }) | |
| for (const maxlength of ['0', '-2', 'abc']) { | |
| it(`should fall back to six slots for the maxlength attribute ${JSON.stringify(maxlength)}`, () => { | |
| fixtureEl.innerHTML = getOtpHtml('', maxlength) | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| expect(otpEl.querySelector('input').getAttribute('maxlength')).toEqual('6') | |
| expect(otpEl.querySelectorAll('.otp-slot').length).toEqual(6) | |
| }) | |
| } | |
| for (const type of ['number', 'password']) { | |
| it(`should change a ${type} input to a text input`, () => { | |
| fixtureEl.innerHTML = `<div class="otp"><input type="${type}" maxlength="6"></div>` | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| expect(otpEl.querySelector('input').type).toEqual('text') | |
| }) | |
| } | |
| it('should keep an existing autocomplete attribute', () => { | |
| fixtureEl.innerHTML = '<div class="otp"><input type="text" maxlength="6" autocomplete="off"></div>' | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| expect(otpEl.querySelector('input').getAttribute('autocomplete')).toEqual('off') | |
| }) | |
| it('should sanitize a pre-filled value', () => { | |
| fixtureEl.innerHTML = '<div class="otp"><input type="text" maxlength="4" value="1a2-3 45"></div>' | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| expect(otp.getValue()).toEqual('1234') | |
| }) | |
| }) | |
| describe('value handling', () => { | |
| it('should return the input value from getValue', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| expect(otp.getValue()).toEqual('') | |
| otpEl.querySelector('input').value = '123' | |
| expect(otp.getValue()).toEqual('123') | |
| }) | |
| it('should set the value and render it into the slots', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| otp.setValue('123456') | |
| expect(otp.getValue()).toEqual('123456') | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| expect(slots[0].textContent).toEqual('1') | |
| expect(slots[5].textContent).toEqual('6') | |
| }) | |
| it('should sanitize disallowed characters and respect the length', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| otp.setValue('12ab34xy567890') | |
| expect(otp.getValue()).toEqual('123456') | |
| }) | |
| it('should accept numeric values', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| otp.setValue(123456) | |
| expect(otp.getValue()).toEqual('123456') | |
| }) | |
| it('should clear the value', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| otp.setValue('123456') | |
| otp.clear() | |
| expect(otp.getValue()).toEqual('') | |
| expect(document.activeElement).toEqual(otpEl.querySelector('input')) | |
| }) | |
| }) | |
| describe('input handling', () => { | |
| it('should strip disallowed characters as they are typed', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| typeInto(input, 'a1b2') | |
| expect(input.value).toEqual('12') | |
| }) | |
| it('should distribute a pasted/autofilled value across the slots', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| // A paste or SMS autofill lands as a single multi-character input event | |
| typeInto(input, '123-456') | |
| expect(input.value).toEqual('123456') | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| expect([...slots].map(slot => slot.textContent).join('')).toEqual('123456') | |
| }) | |
| it('should preserve all digits when pasted text contains a separator', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| pasteInto(input, '123-456') | |
| expect(input.value).toEqual('123456') | |
| }) | |
| it('should keep a short formatted paste invalid and allow undo and redo', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const input = otpEl.querySelector('input') | |
| input.minLength = 6 | |
| input.required = true | |
| const otp = new OtpInput(otpEl) | |
| const slots = otpEl.querySelector('.otp-slots') | |
| input.focus() | |
| pasteInto(input, '1-2') | |
| expect(otp.getValue()).toEqual('12') | |
| expect(input.checkValidity()).toBeFalse() | |
| expect(slots.textContent).toEqual('12') | |
| const modifier = /Mac/.test(navigator.platform) ? 'Meta' : 'Control' | |
| await userEvent.keyboard(`{${modifier}>}z{/${modifier}}`) | |
| expect(otp.getValue()).toEqual('') | |
| expect(slots.textContent).toEqual('') | |
| await userEvent.keyboard(`{${modifier}>}{Shift>}z{/Shift}{/${modifier}}`) | |
| expect(otp.getValue()).toEqual('12') | |
| expect(input.checkValidity()).toBeFalse() | |
| pasteInto(input, '123-456') | |
| expect(otp.getValue()).toEqual('123456') | |
| expect(input.checkValidity()).toBeTrue() | |
| }) | |
| it('should replace the selected range with a partial pasted value', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.setSelectionRange(2, 4) | |
| pasteInto(input, '99') | |
| expect(input.value).toEqual('129956') | |
| expect(input.selectionStart).toEqual(4) | |
| }) | |
| it('should replace the existing value when pasting a full code', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('12') | |
| pasteInto(input, '654-321') | |
| expect(input.value).toEqual('654321') | |
| }) | |
| it('should not paste into a readonly input', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.readOnly = true | |
| const event = pasteInto(input, '654321') | |
| expect(event.defaultPrevented).toBeFalse() | |
| expect(input.value).toEqual('123456') | |
| }) | |
| it('should not paste into an input inside a disabled fieldset', () => { | |
| fixtureEl.innerHTML = `<fieldset disabled>${getOtpHtml()}</fieldset>` | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| const event = pasteInto(input, '654321') | |
| expect(event.defaultPrevented).toBeFalse() | |
| expect(input.value).toEqual('123456') | |
| }) | |
| it('should not clear the value when pasting empty text', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| const event = pasteInto(input, '') | |
| expect(event.defaultPrevented).toBeFalse() | |
| expect(input.value).toEqual('123456') | |
| }) | |
| it('should keep letters for the alphanumeric type', () => { | |
| fixtureEl.innerHTML = getOtpHtml('data-bs-type="alphanumeric"') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| typeInto(input, 'a1B2c3') | |
| expect(input.value).toEqual('a1B2c3') | |
| }) | |
| }) | |
| describe('interaction', () => { | |
| it('should position the active slot from the tapped coordinate', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| // The input is already focused (keyboard up), so a tap repositions the | |
| // caret immediately based on its x-coordinate | |
| input.focus() | |
| const { left, width } = slots[0].getBoundingClientRect() | |
| input.dispatchEvent(new MouseEvent('pointerdown', { | |
| bubbles: true, cancelable: true, clientX: left + (width / 2) | |
| })) | |
| expect(input.selectionStart).toEqual(0) | |
| // A filled slot is selected so the next keystroke overwrites it | |
| expect(input.selectionEnd).toEqual(1) | |
| expect(slots[0]).toHaveClass('otp-slot-active') | |
| }) | |
| it('should overwrite the active slot instead of inserting', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.focus() | |
| input.setSelectionRange(2, 3) | |
| await userEvent.keyboard('9') | |
| expect(input.value).toEqual('129456') | |
| // Caret advances to the next slot | |
| expect(input.selectionStart).toEqual(3) | |
| }) | |
| it('should delete the previous character on backspace', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123') | |
| input.focus() | |
| input.setSelectionRange(3, 3) | |
| await userEvent.keyboard('{Backspace}') | |
| expect(input.value).toEqual('12') | |
| expect(input.selectionStart).toEqual(2) | |
| }) | |
| it('should focus the first empty slot on keyboard focus', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('12') | |
| input.focus() | |
| expect(input.selectionStart).toEqual(2) | |
| expect(otpEl.querySelectorAll('.otp-slot')[2]).toHaveClass('otp-slot-active') | |
| }) | |
| it('should swallow a disallowed character on beforeinput', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| input.focus() | |
| const event = new InputEvent('beforeinput', { | |
| inputType: 'insertText', data: 'a', bubbles: true, cancelable: true | |
| }) | |
| input.dispatchEvent(event) | |
| expect(input.value).toEqual('') | |
| expect(event.defaultPrevented).toBeTrue() | |
| }) | |
| it('should position the caret from the tap only after the input focuses natively (iPadOS keyboard fix)', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| // Input is not yet focused: a tap must NOT reposition the caret itself | |
| // (that would let the browser raise then immediately dismiss the keyboard). | |
| // The tapped slot is only remembered until focus settles. | |
| pointerDownOnSlot(input, slots[0]) | |
| expect(document.activeElement).not.toEqual(input) | |
| // Focus settles (the browser focuses the input from the same tap) | |
| input.focus() | |
| // Caret lands on the tapped slot (0), not the first-empty/last slot (5) | |
| // that a plain keyboard focus would choose | |
| expect(input.selectionStart).toEqual(0) | |
| expect(input.selectionEnd).toEqual(1) | |
| expect(slots[0]).toHaveClass('otp-slot-active') | |
| }) | |
| it('should clamp a tap beyond the filled slots to the first empty slot', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('12') | |
| layoutSlotsHorizontally(otpEl) | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| input.focus() | |
| // Move the caret away from the default first-empty position first | |
| input.setSelectionRange(0, 1) | |
| // Tap the far right (last) slot while only two of six are filled | |
| pointerDownOnSlot(input, slots[5]) | |
| // The caret is clamped to the first empty slot (index 2), not slot 5 | |
| expect(input.selectionStart).toEqual(2) | |
| expect(slots[2]).toHaveClass('otp-slot-active') | |
| }) | |
| it('should clear the selected slot and keep the caret on backspace', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.focus() | |
| // A filled slot is selected (as the active slot always is) | |
| input.setSelectionRange(2, 3) | |
| await userEvent.keyboard('{Backspace}') | |
| expect(input.value).toEqual('12456') | |
| expect(input.selectionStart).toEqual(2) | |
| }) | |
| it('should replace a multi-slot selection with the typed character', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.focus() | |
| // Select all, then type over it | |
| input.setSelectionRange(0, 6) | |
| await userEvent.keyboard('9') | |
| expect(input.value).toEqual('9') | |
| expect(input.selectionStart).toEqual(1) | |
| }) | |
| it('should do nothing on backspace at the start of an empty field', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| input.focus() | |
| input.setSelectionRange(0, 0) | |
| await userEvent.keyboard('{Backspace}') | |
| expect(input.value).toEqual('') | |
| }) | |
| it('should delete the next character on delete and keep the caret', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123') | |
| input.focus() | |
| input.setSelectionRange(1, 1) | |
| await userEvent.keyboard('{Delete}') | |
| expect(input.value).toEqual('13') | |
| expect(input.selectionStart).toEqual(1) | |
| }) | |
| it('should clear the selected slot and keep the caret on delete', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.focus() | |
| input.setSelectionRange(2, 3) | |
| await userEvent.keyboard('{Delete}') | |
| expect(input.value).toEqual('12456') | |
| expect(input.selectionStart).toEqual(2) | |
| }) | |
| it('should do nothing on delete at the end of the value', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123') | |
| input.focus() | |
| input.setSelectionRange(3, 3) | |
| await userEvent.keyboard('{Delete}') | |
| expect(input.value).toEqual('123') | |
| }) | |
| it('should leave a readonly input to the browser on beforeinput', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.readOnly = true | |
| input.focus() | |
| input.setSelectionRange(2, 3) | |
| const typed = beforeInput(input, { inputType: 'insertText', data: '9' }) | |
| const deleted = beforeInput(input, { inputType: 'deleteContentBackward' }) | |
| // Not cancelled, so the browser's own readonly handling applies | |
| expect(typed).toBeTrue() | |
| expect(deleted).toBeTrue() | |
| expect(input.value).toEqual('123456') | |
| }) | |
| it('should leave an input inside a disabled fieldset to the browser on beforeinput', () => { | |
| fixtureEl.innerHTML = `<fieldset disabled>${getOtpHtml()}</fieldset>` | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| const typed = beforeInput(input, { inputType: 'insertText', data: '9' }) | |
| expect(typed).toBeTrue() | |
| expect(input.value).toEqual('123456') | |
| }) | |
| it('should append into an empty slot when typing at the caret', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('12') | |
| input.focus() | |
| input.setSelectionRange(2, 2) | |
| await userEvent.keyboard('3') | |
| expect(input.value).toEqual('123') | |
| expect(input.selectionStart).toEqual(3) | |
| }) | |
| it('should place the caret on the first empty slot after a paste/autofill', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| input.focus() | |
| // A paste lands as a single multi-character input event | |
| typeInto(input, '123') | |
| expect(input.value).toEqual('123') | |
| expect(input.selectionStart).toEqual(3) | |
| expect(input.selectionEnd).toEqual(3) | |
| }) | |
| it('should highlight each slot in the selection and remove the highlight after collapse', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.focus() | |
| input.setSelectionRange(1, 4, 'backward') | |
| document.dispatchEvent(new Event('selectionchange')) | |
| const selected = () => [...otpEl.querySelectorAll('.otp-slot-selected')].map(slot => slot.textContent).join('') | |
| expect(selected()).toEqual('234') | |
| input.setSelectionRange(2, 2) | |
| document.dispatchEvent(new Event('selectionchange')) | |
| expect(selected()).toEqual('') | |
| }) | |
| it('should replace the slot at a collapsed caret and keep native undo and input events', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| const values = [] | |
| input.addEventListener('input', () => values.push(input.value)) | |
| otp.setValue('123456') | |
| input.focus() | |
| input.setSelectionRange(2, 2) | |
| await userEvent.keyboard('9') | |
| expect(input.value).toEqual('129456') | |
| const modifier = /Mac/.test(navigator.platform) ? 'Meta' : 'Control' | |
| await userEvent.keyboard(`{${modifier}>}z{/${modifier}}`) | |
| expect(input.value).toEqual('123456') | |
| expect(values).toEqual(['129456', '123456']) | |
| expect(otpEl.querySelector('.otp-slots').textContent).toEqual('123456') | |
| }) | |
| it('should map a tap to the correct slot in RTL', () => { | |
| fixtureEl.innerHTML = `<div dir="rtl">${getOtpHtml()}</div>` | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| otp.setValue('123456') | |
| layoutSlotsHorizontally(otpEl) | |
| input.focus() | |
| pointerDownOnSlot(input, slots[3]) | |
| expect(input.selectionStart).toEqual(3) | |
| expect(input.selectionEnd).toEqual(4) | |
| }) | |
| for (const [type, code] of [['numeric', '123'], ['alphanumeric', 'A1B']]) { | |
| it(`should move one slot per arrow in RTL for ${type} codes`, async () => { | |
| fixtureEl.innerHTML = `<div dir="rtl">${getOtpHtml(`data-bs-type="${type}"`)}</div>` | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue(code) | |
| input.focus() | |
| const pressArrow = async (key, slot) => { | |
| await userEvent.keyboard(`{${key}}`) | |
| expect(input.selectionStart).toEqual(slot) | |
| expect(otpEl.querySelectorAll('.otp-slot')[slot]).toHaveClass('otp-slot-active') | |
| } | |
| await pressArrow('ArrowRight', 2) | |
| await pressArrow('ArrowRight', 1) | |
| await pressArrow('ArrowLeft', 2) | |
| await pressArrow('ArrowLeft', 3) | |
| await pressArrow('ArrowLeft', 3) | |
| expect(input.value).toEqual(code) | |
| }) | |
| } | |
| it('should collapse an RTL selection in the arrow direction', async () => { | |
| fixtureEl.innerHTML = `<div dir="rtl">${getOtpHtml()}</div>` | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.focus() | |
| input.setSelectionRange(1, 4) | |
| await userEvent.keyboard('{ArrowRight}') | |
| expect(input.selectionStart).toEqual(1) | |
| expect(input.selectionEnd).toEqual(2) | |
| input.setSelectionRange(1, 4) | |
| await userEvent.keyboard('{ArrowLeft}') | |
| expect(input.selectionStart).toEqual(4) | |
| expect(input.selectionEnd).toEqual(5) | |
| }) | |
| it('should leave touch pointerdown to the browser and place the caret on click', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| const slot = otpEl.querySelectorAll('.otp-slot')[2] | |
| otp.setValue('123456') | |
| layoutSlotsHorizontally(otpEl) | |
| input.focus() | |
| const { left, width } = slot.getBoundingClientRect() | |
| const options = { | |
| bubbles: true, | |
| cancelable: true, | |
| pointerType: 'touch', | |
| clientX: left + (width / 2) | |
| } | |
| expect(input.dispatchEvent(new PointerEvent('pointerdown', options))).toBeTrue() | |
| expect(input.selectionStart).toEqual(5) | |
| input.dispatchEvent(new MouseEvent('click', options)) | |
| await expect.poll(() => input.selectionStart).toEqual(2) | |
| }) | |
| it('should keep the active slot in sync on selectionchange', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| input.focus() | |
| input.setSelectionRange(3, 4) | |
| document.dispatchEvent(new Event('selectionchange')) | |
| expect(otpEl.querySelectorAll('.otp-slot')[3]).toHaveClass('otp-slot-active') | |
| }) | |
| it('should not remember a tap on a disabled input', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123') | |
| layoutSlotsHorizontally(otpEl) | |
| input.disabled = true | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| pointerDownOnSlot(input, slots[1]) | |
| // Enabled later, the first keyboard focus selects the first empty slot, | |
| // not the slot tapped while disabled | |
| input.disabled = false | |
| input.focus() | |
| expect(input.selectionStart).toEqual(3) | |
| expect(slots[3]).toHaveClass('otp-slot-active') | |
| }) | |
| it('should not remember a tap on an input inside a disabled fieldset', () => { | |
| fixtureEl.innerHTML = `<fieldset disabled>${getOtpHtml()}</fieldset>` | |
| const fieldset = fixtureEl.querySelector('fieldset') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123') | |
| layoutSlotsHorizontally(otpEl) | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| pointerDownOnSlot(input, slots[1]) | |
| fieldset.disabled = false | |
| input.focus() | |
| expect(input.selectionStart).toEqual(3) | |
| expect(slots[3]).toHaveClass('otp-slot-active') | |
| }) | |
| }) | |
| describe('focus', () => { | |
| it('should focus the input and select the first empty slot', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123') | |
| otp.focus() | |
| expect(document.activeElement).toEqual(input) | |
| expect(input.selectionStart).toEqual(3) | |
| expect(otpEl.querySelectorAll('.otp-slot')[3]).toHaveClass('otp-slot-active') | |
| }) | |
| it('should select the last slot when the value is already full', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| otp.focus() | |
| // Clamp to the last slot and select it so the next keystroke overwrites it | |
| expect(input.selectionStart).toEqual(5) | |
| expect(input.selectionEnd).toEqual(6) | |
| }) | |
| }) | |
| describe('mask', () => { | |
| it('should render the mask character but keep the real value', () => { | |
| fixtureEl.innerHTML = getOtpHtml('data-bs-mask="true"') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| otp.setValue('123456') | |
| expect(otp.getValue()).toEqual('123456') | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| expect([...slots].every(slot => slot.textContent === '•')).toBeTrue() | |
| }) | |
| it('should not turn the input into a password field', () => { | |
| fixtureEl.innerHTML = getOtpHtml('data-bs-mask="true"') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| expect(otpEl.querySelector('input').type).toEqual('text') | |
| }) | |
| }) | |
| describe('separators', () => { | |
| it('should render separators between configured groups', () => { | |
| fixtureEl.innerHTML = getOtpHtml('data-bs-groups="[3,3]"') | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| expect(otpEl.querySelectorAll('.otp-separator').length).toEqual(1) | |
| expect(otpEl.querySelectorAll('.otp-slot').length).toEqual(6) | |
| }) | |
| it('should repeat the last group size and not add a trailing separator', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl, { groups: [2] }) // eslint-disable-line no-new | |
| const classes = [...otpEl.querySelector('.otp-slots').children].map(child => child.className) | |
| expect(classes).toEqual([ | |
| 'otp-slot', | |
| 'otp-slot', | |
| 'otp-separator', | |
| 'otp-slot', | |
| 'otp-slot', | |
| 'otp-separator', | |
| 'otp-slot', | |
| 'otp-slot' | |
| ]) | |
| }) | |
| it('should render the separator option as the separator text', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl, { groups: [3, 3], separator: '-' }) // eslint-disable-line no-new | |
| expect(otpEl.querySelector('.otp-separator').textContent).toEqual('-') | |
| }) | |
| }) | |
| it('should keep large groups and badge space within a narrow container', () => { | |
| fixtureEl.innerHTML = `<div style="width: 260px">${getOtpHtml()}</div>` | |
| const style = document.createElement('style') | |
| style.textContent = otpStyles | |
| fixtureEl.append(style) | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| otpEl.classList.add('otp-lg', 'otp-connected', 'otp-badge-space') | |
| otpEl.style.setProperty('--btn-input-lg-min-height', '44px') | |
| otpEl.style.setProperty('--border-width', '1px') | |
| otpEl.style.setProperty('--radius-5', '5px') | |
| otpEl.style.setProperty('--font-weight-medium', '600') | |
| new OtpInput(otpEl, { groups: [3, 3] }) // eslint-disable-line no-new | |
| const wrapper = otpEl.getBoundingClientRect() | |
| const slots = otpEl.querySelectorAll('.otp-slot') | |
| const lastSlot = slots[5].getBoundingClientRect() | |
| expect(wrapper.width).toBeLessThanOrEqual(260) | |
| expect(lastSlot.right).toBeLessThan(wrapper.right) | |
| expect(lastSlot.height).toBeGreaterThanOrEqual(44) | |
| expect(getComputedStyle(slots[3]).borderStartStartRadius).toEqual('5px') | |
| expect(getComputedStyle(slots[2]).borderStartEndRadius).toEqual('5px') | |
| expect(getComputedStyle(slots[0]).fontWeight).toEqual('600') | |
| }) | |
| describe('form integration', () => { | |
| it('should synchronize slots with the reset value and honor a canceled reset', async () => { | |
| fixtureEl.innerHTML = `<form>${getOtpHtml()}<button type="reset">Reset</button></form>` | |
| const form = fixtureEl.querySelector('form') | |
| const reset = form.querySelector('button') | |
| const otpEl = form.querySelector('.otp') | |
| const input = otpEl.querySelector('input') | |
| input.name = 'otp' | |
| input.defaultValue = '12' | |
| const otp = new OtpInput(otpEl) | |
| otp.setValue('654321') | |
| form.addEventListener('reset', event => event.preventDefault(), { once: true }) | |
| reset.focus() | |
| await userEvent.keyboard('{Enter}') | |
| expect(otpEl.querySelector('.otp-slots').textContent).toEqual('654321') | |
| await userEvent.keyboard('{Enter}') | |
| await expect.poll(() => otpEl.querySelector('.otp-slots').textContent).toEqual('12') | |
| expect(new FormData(form).get('otp')).toEqual('12') | |
| }) | |
| it('should retain native validation and input attributes and show a decorative placeholder', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const input = otpEl.querySelector('input') | |
| input.pattern = '[A-Z]{6}' | |
| input.inputMode = 'text' | |
| input.placeholder = 'AAAAAA' | |
| input.required = true | |
| const otp = new OtpInput(otpEl, { type: 'alpha' }) | |
| expect(input.inputMode).toEqual('text') | |
| expect(input.checkValidity()).toBeFalse() | |
| expect(input.value).toEqual('') | |
| expect(otpEl.querySelector('.otp-slots').textContent).toEqual('AAAAAA') | |
| expect(otpEl.querySelector('.otp-slots').getAttribute('aria-hidden')).toEqual('true') | |
| otp.setValue('abc') | |
| expect(input.validity.patternMismatch).toBeTrue() | |
| expect(otpEl.querySelector('.otp-slots').textContent).toEqual('abc') | |
| otp.setValue('ABCDEF') | |
| expect(input.checkValidity()).toBeTrue() | |
| }) | |
| it('should transform formatted paste and emit one native input event', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const input = otpEl.querySelector('input') | |
| const values = [] | |
| input.addEventListener('input', () => values.push(input.value)) | |
| const otp = new OtpInput(otpEl, { pasteTransformer: text => text.split(':')[1] }) | |
| input.focus() | |
| pasteInto(input, '1:123-456') | |
| expect(otp.getValue()).toEqual('123456') | |
| expect(values).toEqual(['123456']) | |
| }) | |
| }) | |
| describe('events', () => { | |
| it('should trigger complete event when the value is full', () => { | |
| return new Promise(resolve => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| otpEl.addEventListener('complete.bs.otpInput', event => { | |
| expect(event.value).toEqual('123456') | |
| resolve() | |
| }) | |
| otp.setValue('123456') | |
| }) | |
| }) | |
| it('should only emit complete for a new full code', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const codes = [] | |
| otpEl.addEventListener('complete.bs.otpInput', event => codes.push(event.value)) | |
| otp.setValue('123456') | |
| otp.setValue('123456') | |
| otp.setValue('123459') | |
| otp.clear() | |
| otp.setValue('123459') | |
| expect(codes).toEqual(['123456', '123459', '123459']) | |
| }) | |
| it('should stop after an input listener disposes the instance', async () => { | |
| fixtureEl.innerHTML = getOtpHtml('', 1) | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const input = otpEl.querySelector('input') | |
| const otp = new OtpInput(otpEl) | |
| const events = [] | |
| otpEl.addEventListener('input.bs.otpInput', () => { | |
| events.push('input') | |
| otp.dispose() | |
| }, { once: true }) | |
| otpEl.addEventListener('complete.bs.otpInput', () => events.push('complete')) | |
| input.focus() | |
| await userEvent.keyboard('5') | |
| expect(input.value).toEqual('5') | |
| expect(events).toEqual(['input']) | |
| expect(OtpInput.getInstance(otpEl)).toBeNull() | |
| }) | |
| it('should trigger input event with the current value on each change', () => { | |
| return new Promise(resolve => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| new OtpInput(otpEl) // eslint-disable-line no-new | |
| const input = otpEl.querySelector('input') | |
| otpEl.addEventListener('input.bs.otpInput', event => { | |
| expect(event.value).toEqual('12') | |
| resolve() | |
| }) | |
| typeInto(input, '12') | |
| }) | |
| }) | |
| const countEvents = otpEl => { | |
| const counts = { input: 0, complete: 0 } | |
| otpEl.addEventListener('input.bs.otpInput', () => counts.input++) | |
| otpEl.addEventListener('complete.bs.otpInput', () => counts.complete++) | |
| return counts | |
| } | |
| it('should not trigger input or complete events on delete after the last character', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| const counts = countEvents(otpEl) | |
| input.focus() | |
| input.setSelectionRange(6, 6) | |
| await userEvent.keyboard('{Delete}') | |
| expect(input.value).toEqual('123456') | |
| expect(counts).toEqual({ input: 0, complete: 0 }) | |
| }) | |
| it('should not trigger an input event on backspace before the first character', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123') | |
| const counts = countEvents(otpEl) | |
| input.focus() | |
| input.setSelectionRange(0, 0) | |
| await userEvent.keyboard('{Backspace}') | |
| expect(input.value).toEqual('123') | |
| expect(counts).toEqual({ input: 0, complete: 0 }) | |
| }) | |
| it('should trigger one input event when delete removes a character', async () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.setValue('123456') | |
| const counts = countEvents(otpEl) | |
| input.focus() | |
| input.setSelectionRange(2, 3) | |
| await userEvent.keyboard('{Delete}') | |
| expect(input.value).toEqual('12456') | |
| expect(counts).toEqual({ input: 1, complete: 0 }) | |
| }) | |
| }) | |
| describe('dispose', () => { | |
| it('should dispose the instance and remove generated markup', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| expect(OtpInput.getInstance(otpEl)).not.toBeNull() | |
| otp.dispose() | |
| expect(OtpInput.getInstance(otpEl)).toBeNull() | |
| expect(fixtureEl.querySelector('.otp-slots')).toBeNull() | |
| expect(fixtureEl.querySelector('.otp').classList.contains('otp-rendered')).toBeFalse() | |
| }) | |
| it('should update defaults on reinitialization and preserve author values', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const input = otpEl.querySelector('input') | |
| input.setAttribute('spellcheck', 'true') | |
| const otp = new OtpInput(otpEl) | |
| expect(input.inputMode).toEqual('numeric') | |
| input.pattern = '[A-Z]{6}' | |
| otp.dispose() | |
| const alphaOtp = new OtpInput(otpEl, { type: 'alpha' }) | |
| expect(input.inputMode).toEqual('text') | |
| expect(input.pattern).toEqual('[A-Z]{6}') | |
| expect(input.getAttribute('spellcheck')).toEqual('true') | |
| alphaOtp.dispose() | |
| }) | |
| it('should stop intercepting paste after dispose', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.dispose() | |
| const event = new Event('paste', { bubbles: true, cancelable: true }) | |
| event.clipboardData = { getData: () => '123-456' } | |
| input.dispatchEvent(event) | |
| expect(event.defaultPrevented).toBeFalse() | |
| expect(input.value).toEqual('') | |
| }) | |
| it('should stop intercepting beforeinput after dispose', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| const input = otpEl.querySelector('input') | |
| otp.dispose() | |
| // The beforeinput listener is gone, so the event is no longer prevented | |
| const event = new InputEvent('beforeinput', { | |
| inputType: 'insertText', data: '1', bubbles: true, cancelable: true | |
| }) | |
| input.dispatchEvent(event) | |
| expect(event.defaultPrevented).toBeFalse() | |
| }) | |
| }) | |
| describe('getInstance', () => { | |
| it('should return otp-input instance', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| expect(OtpInput.getInstance(otpEl)).toEqual(otp) | |
| expect(OtpInput.getInstance(otpEl)).toBeInstanceOf(OtpInput) | |
| }) | |
| it('should return null when there is no instance', () => { | |
| fixtureEl.innerHTML = '<div></div>' | |
| const div = fixtureEl.querySelector('div') | |
| expect(OtpInput.getInstance(div)).toBeNull() | |
| }) | |
| }) | |
| describe('getOrCreateInstance', () => { | |
| it('should return existing instance', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| const otp = new OtpInput(otpEl) | |
| expect(OtpInput.getOrCreateInstance(otpEl)).toEqual(otp) | |
| expect(OtpInput.getOrCreateInstance(otpEl)).toBeInstanceOf(OtpInput) | |
| }) | |
| it('should create new instance when none exists', () => { | |
| fixtureEl.innerHTML = getOtpHtml() | |
| const otpEl = fixtureEl.querySelector('.otp') | |
| expect(OtpInput.getInstance(otpEl)).toBeNull() | |
| expect(OtpInput.getOrCreateInstance(otpEl)).toBeInstanceOf(OtpInput) | |
| }) | |
| }) | |
| }) |