Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
904a698
commit b218d27
Showing
4 changed files
with
173 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/dnb-eufemia/src/components/number-format/__tests__/useNumberFormat.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Hook Test | ||
* | ||
*/ | ||
|
||
import React from 'react' | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import useNumberFormat from '../useNumberFormat' | ||
import Provider from '../../../shared/Provider' | ||
|
||
describe('useNumberFormat', () => { | ||
it('will render without provider', () => { | ||
const { result } = renderHook(() => | ||
useNumberFormat(1234, { currency: true }) | ||
) | ||
|
||
expect(result.current).toBe('1 234,00 kr') | ||
}) | ||
|
||
it('will return object when returnAria is true', () => { | ||
const { result } = renderHook(() => | ||
useNumberFormat(1234, { currency: true, returnAria: true }) | ||
) | ||
|
||
expect(result.current).toEqual( | ||
expect.objectContaining({ | ||
aria: '1 234,00 norske kroner', | ||
cleanedValue: '1234,00 kr', | ||
locale: 'nb-NO', | ||
number: '1 234,00 kr', | ||
type: 'currency', | ||
value: 1234, | ||
}) | ||
) | ||
}) | ||
|
||
it('will inherit NumberFormat props from provider', () => { | ||
const wrapper = ({ children }) => ( | ||
<Provider | ||
locale="nb-NO" // should get overwritte by the NumberFormat props | ||
NumberFormat={{ | ||
currency: true, | ||
locale: 'en-GB', | ||
}} | ||
> | ||
{children} | ||
</Provider> | ||
) | ||
const { result } = renderHook(() => useNumberFormat(1234), { wrapper }) | ||
|
||
expect(result.current).toBe('NOK 1 234.00') | ||
}) | ||
|
||
it('will inherit locale from provider', () => { | ||
const wrapper = ({ children }) => ( | ||
<Provider | ||
locale="en-GB" | ||
NumberFormat={{ | ||
currency: true, | ||
}} | ||
> | ||
{children} | ||
</Provider> | ||
) | ||
const { result } = renderHook(() => useNumberFormat(1234), { wrapper }) | ||
|
||
expect(result.current).toBe('NOK 1 234.00') | ||
}) | ||
}) |
21 changes: 21 additions & 0 deletions
21
packages/dnb-eufemia/src/components/number-format/useNumberFormat.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useContext } from 'react' | ||
import { format } from './NumberUtils' | ||
import Context from '../../shared/Context' | ||
import usePropsWithContext from '../../shared/hooks/usePropsWithContext' | ||
import type { formatOptionParams, formatValue } from './NumberUtils' | ||
|
||
function useNumberFormat( | ||
value: formatValue, | ||
options: formatOptionParams = {} | ||
) { | ||
const context = useContext(Context) | ||
const params = usePropsWithContext( | ||
options, | ||
{ locale: context.locale }, | ||
context.NumberFormat | ||
) | ||
|
||
return format(value, params) | ||
} | ||
|
||
export default useNumberFormat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters