Skip to content

Commit

Permalink
fix(InputMasked): on custom mask – avoid interaction stall after focus (
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed May 31, 2023
1 parent 2914a0c commit 215b3c0
Showing 1 changed file with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ describe('InputMasked component', () => {
'​ kr' // includes a hidden space: invisibleSpace
)

await wait(2)
await wait(2) // because of the delayed requestAnimationFrame

expect(setSelectionRange).toBeCalledTimes(1)
expect(setSelectionRange).toHaveBeenCalledWith(0, 0)
Expand All @@ -665,7 +665,7 @@ describe('InputMasked component', () => {
'Prefix ​ kr' // includes a hidden space: invisibleSpace
)

await wait(2)
await wait(2) // because of the delayed requestAnimationFrame

expect(setSelectionRange).toBeCalledTimes(2)
expect(setSelectionRange).toHaveBeenCalledWith(8, 8)
Expand Down Expand Up @@ -1518,6 +1518,45 @@ describe('InputMasked component as_currency', () => {
'dnb-input--vertical',
])
})

it('should set correct cursor position on focus and mouseUp', async () => {
render(
<Component value={12} mask={[/\d/, /\d/, '–', '–', /\d/, /\d/]} />
)

const element = document.querySelector('input')

const preventDefault = jest.fn()
element.setSelectionRange = jest.fn()

// 1. Test first focus
fireEvent.focus(element, {
target: {
selectionStart: 6,
},
preventDefault,
})

await wait(2) // because of the delayed requestAnimationFrame

expect(element.setSelectionRange).toHaveBeenCalledTimes(1)
expect(element.setSelectionRange).toHaveBeenNthCalledWith(1, 4, 4)

// 2. Then test mouse up
fireEvent.mouseUp(element, {
target: {
selectionStart: 6,
},
preventDefault,
})

await wait(2) // because of the delayed requestAnimationFrame

expect(element.setSelectionRange).toHaveBeenCalledTimes(2)
expect(element.setSelectionRange).toHaveBeenNthCalledWith(2, 4, 4)

expect(element.value).toBe('12––​​')
})
})

describe('InputMasked scss', () => {
Expand Down

0 comments on commit 215b3c0

Please sign in to comment.