Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEK-902: Coffeebox -> Main #1665

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 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 Expand Up @@ -276,13 +322,11 @@ describe('inntekt-utils', () => {
).toBeFalsy()
expect(
validateInntekt('-25', updateValidationErrorMessageMock)
).toBeTruthy()
expect(
validateInntekt('-', updateValidationErrorMessageMock)
).toBeTruthy()
).toBeFalsy()
expect(validateInntekt('-', updateValidationErrorMessageMock)).toBeFalsy()
expect(
validateInntekt('123.43', updateValidationErrorMessageMock)
).toBeTruthy()
).toBeFalsy()
expect(updateValidationErrorMessageMock).toHaveBeenNthCalledWith(
1,
'inntekt.endre_inntekt_modal.textfield.validation_error.type'
Expand Down
21 changes: 13 additions & 8 deletions src/utils/inntekt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const validateInntekt = (
return isValid
}

if (!/^[0-9\s\-.]+$/.test(input)) {
if (!/^[0-9\s]+$/.test(input)) {
isValid = false
if (updateValidationErrorMessage) {
updateValidationErrorMessage(
Expand Down 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
Loading