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

fix(Field.Number): decimalLimit={0} when currency should work #4144

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,12 @@ describe('InputMasked component with currency_mask', () => {
expect(document.querySelector('input').value).toBe('1 234 NOK')
})

it('should not append a comma when entering a comma', () => {
render(<InputMasked value="12345,1" as_currency decimalLimit={0} />)

expect(document.querySelector('input').value).toBe('12 345 kr')
})

it('should not show mask if placeholder is set', () => {
const placeholderText = 'Placeholder-text'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { axeComponent } from '../../../../../core/jest/jestSetup'
import { render } from '@testing-library/react'
import { Field } from '../../..'
import { Provider } from '../../../../../shared'
import userEvent from '@testing-library/user-event'

describe('Field.Currency', () => {
it('defaults to "kr" and use "NOK" when locale is en-GB', () => {
Expand Down Expand Up @@ -124,6 +125,22 @@ describe('Field.Currency', () => {
expect(input).toHaveAttribute('inputmode', 'decimal')
})

it('should work with decimal limit 0', async () => {
render(<Field.Currency decimalLimit={0} />)

const input = document.querySelector('.dnb-input__input')

expect(input).toHaveValue('')

await userEvent.type(document.querySelector('input'), '1')

expect(input).toHaveValue('1 kr')

await userEvent.type(document.querySelector('input'), ',')

expect(input).toHaveValue('1 kr')
})

describe('ARIA', () => {
it('should validate with ARIA rules', async () => {
const result = render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ describe('Field.Number', () => {
)
expect(document.querySelector('input')).toHaveValue('1 234,56 %')
})

it('formats with percent and decimalLimit 0', () => {
render(
<Field.Number value={1234.56789} percent decimalLimit={0} />
)
expect(document.querySelector('input')).toHaveValue('1 234 %')
})
})

describe('currency', () => {
Expand All @@ -197,6 +204,13 @@ describe('Field.Number', () => {
expect(document.querySelector('input')).toHaveValue('1 234,56 kr')
})

it('formats with currency and decimalLimit 0', () => {
render(
<Field.Number value={1234.56789} currency decimalLimit={0} />
)
expect(document.querySelector('input')).toHaveValue('1 234 kr')
})

it('formats in different locale', () => {
render(
<Provider locale="en-GB">
Expand Down Expand Up @@ -267,6 +281,11 @@ describe('Field.Number', () => {
render(<Field.Number value={123.456} decimalLimit={4} />)
expect(screen.getByDisplayValue('123,456')).toBeInTheDocument()
})

it('formats with decimal limit 0', () => {
render(<Field.Number value={123.456} decimalLimit={0} />)
expect(screen.getByDisplayValue('123')).toBeInTheDocument()
})
})

it('should align input correctly', () => {
Expand Down
Loading