Skip to content

Commit

Permalink
Merge branch 'bugfix/PEK-902' into coffeebox
Browse files Browse the repository at this point in the history
  • Loading branch information
martinstorvoll committed Dec 19, 2024
2 parents d4098df + 83d9f21 commit 5db4e00
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
50 changes: 48 additions & 2 deletions src/utils/__tests__/inntekt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('inntekt-utils', () => {
const updateInntektMock = vi.fn()
const updateValideringsfeilMock = vi.fn()
const inputHtmlElement = {
selectionStart: 3,
selectionStart: 4,
setSelectionRange: setSelectionRangeMock,
} as unknown as HTMLInputElement

Expand All @@ -206,11 +206,57 @@ describe('inntekt-utils', () => {
expect(updateInntektMock).toHaveBeenCalled()
expect(updateValideringsfeilMock).toHaveBeenCalled()
await waitFor(() => {
expect(setSelectionRangeMock).toHaveBeenCalledWith(4, 4)
expect(setSelectionRangeMock).toHaveBeenCalledWith(5, 5)
})
})
})

it('når input elementet er funnet og at ny input prøver å fjerne et mellomrom, vil caret beholde sin nye posisjon, og inntekt og valideringsfeil oppdateres', async () => {
const setSelectionRangeMock = vi.fn()
const updateInntektMock = vi.fn()
const updateValideringsfeilMock = vi.fn()
const inputHtmlElement = {
selectionStart: 3,
setSelectionRange: setSelectionRangeMock,
} as unknown as HTMLInputElement

updateAndFormatInntektFromInputField(
inputHtmlElement,
'123000', // denne strenges skal formateres til 123 000, altså ett karakter mer
updateInntektMock,
updateValideringsfeilMock
)

expect(updateInntektMock).toHaveBeenCalled()
expect(updateValideringsfeilMock).toHaveBeenCalled()
await waitFor(() => {
expect(setSelectionRangeMock).toHaveBeenCalledWith(3, 3)
})
})

it('når input elementet er funnet og at ny input sletter første tegn slik at formatert verdi blir mindre, vil caret gå til posisjon 0, og inntekt og valideringsfeil oppdateres', async () => {
const setSelectionRangeMock = vi.fn()
const updateInntektMock = vi.fn()
const updateValideringsfeilMock = vi.fn()
const inputHtmlElement = {
selectionStart: 0,
setSelectionRange: setSelectionRangeMock,
} as unknown as HTMLInputElement

updateAndFormatInntektFromInputField(
inputHtmlElement,
' 123 000', // denne strenges skal formateres til 123 000, med ett tegn mindre
updateInntektMock,
updateValideringsfeilMock
)

expect(updateInntektMock).toHaveBeenCalled()
expect(updateValideringsfeilMock).toHaveBeenCalled()
await waitFor(() => {
expect(setSelectionRangeMock).toHaveBeenCalledWith(0, 0)
})
})

describe('validateInntekt', () => {
afterEach(() => {
vi.clearAllMocks()
Expand Down
19 changes: 12 additions & 7 deletions src/utils/inntekt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,27 @@ export const updateAndFormatInntektFromInputField = (
const antallTegnBefore = inntekt.length
const formatertInntekt = formatInntekt(inntekt)
const antallTegnAfter = formatertInntekt.length
const charAtCaret = formatertInntekt[Math.max(caretPosition, 0)]

updateInntekt(formatertInntekt)
updateValidationErrors('')

setTimeout(() => {
const updatedCaretPosition =
antallTegnAfter > antallTegnBefore
? caretPosition + 1
: antallTegnAfter < antallTegnBefore
? caretPosition - 1
: caretPosition
let updatedCaretPosition = caretPosition

if (antallTegnAfter > antallTegnBefore && charAtCaret === '\u00A0') {
updatedCaretPosition = caretPosition
} else if (antallTegnAfter > antallTegnBefore) {
updatedCaretPosition = caretPosition + 1
} else if (antallTegnAfter < antallTegnBefore) {
updatedCaretPosition = Math.max(caretPosition - 1, 0)
}

inputElement?.setSelectionRange(
updatedCaretPosition,
updatedCaretPosition
)
}, 10)
}, 0)
} else {
updateInntekt(inntekt)
updateValidationErrors('')
Expand Down

0 comments on commit 5db4e00

Please sign in to comment.