Skip to content

Commit

Permalink
fix(Forms): ensure Value.* component's transformIn can changes it…
Browse files Browse the repository at this point in the history
…s value
  • Loading branch information
tujoworker committed Sep 19, 2024
1 parent a8a41ad commit 153fc12
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,54 @@ import nbNO from '../../constants/locales/nb-NO'
const nb = nbNO['nb-NO']

describe('useValueProps', () => {
it('should call use "transformIn" to prepare value', () => {
const value = 1
describe('transformIn', () => {
it('should prepare value', () => {
const value = 1

const transformIn = (value) => value + 1
const { result } = renderHook(() =>
useValueProps({ value, transformIn })
)
const transformIn = (value) => value + 1
const { result } = renderHook(() =>
useValueProps({ value, transformIn })
)

expect(result.current.value).toBe(2)
expect(result.current.value).toBe(2)
})

it('should prepare value when function instance changes', () => {
const { result, rerender } = renderHook(useValueProps, {
initialProps: {
value: 1,
transformIn: (value: number) => value + 1,
},
})

expect(result.current.value).toBe(2)

rerender({ value: 2, transformIn: (value: number) => value + 2 })

expect(result.current.value).toBe(4)
})

it('should prepare value from context', () => {
const path = '/contextValue'

const transformIn = (value) => value + 1
const { result } = renderHook(
() => useValueProps({ path, transformIn }),
{
wrapper: ({ children }) => (
<Provider
data={{
contextValue: 1,
}}
>
{children}
</Provider>
),
}
)

expect(result.current.value).toBe(2)
})
})

it('should call use "toInput" to prepare value', () => {
Expand Down Expand Up @@ -135,28 +174,6 @@ describe('useValueProps', () => {
expect(result.current.value).toBe(2)
})

it('should use "transformIn" to prepare value from context', () => {
const path = '/contextValue'

const transformIn = (value) => value + 1
const { result } = renderHook(
() => useValueProps({ path, transformIn }),
{
wrapper: ({ children }) => (
<Provider
data={{
contextValue: 1,
}}
>
{children}
</Provider>
),
}
)

expect(result.current.value).toBe(2)
})

it('should get value from data context', () => {
const path = '/contextValue'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ export default function useValueProps<
)

const value = shouldBeVisible(path)
? transformers.current.transformIn(
transformers.current.toInput(externalValue)
)
? transformIn(toInput(externalValue))
: undefined

const label =
Expand Down

0 comments on commit 153fc12

Please sign in to comment.