Skip to content

Commit

Permalink
fix(Forms): ensure pathUndefined on Form.Visibility does render whe…
Browse files Browse the repository at this point in the history
…n value is `undefined`
  • Loading branch information
tujoworker committed Feb 6, 2025
1 parent 4f99680 commit a789ea2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ describe('useVisibility', () => {
)
expect(result.current.check()).toBe(true)
})

it('should return false when value exists but is "undefined"', () => {
const { result } = renderHook(useVisibility, {
wrapper: ({ children }) => (
<Provider data={{ isUndefined: undefined }}>{children}</Provider>
),
})
expect(
result.current.check({
pathUndefined: '/isUndefined',
})
).toBe(true)
})
})

describe('pathTruthy', () => {
Expand Down Expand Up @@ -760,6 +773,28 @@ describe('useVisibility', () => {
)
expect(result.current.check()).toBe(true)
})

it('should return true when value exists but is "undefined"', () => {
const { result } = renderHook(
() =>
useVisibility({
withinIterate: true,
pathUndefined: '/isUndefined',
}),
{
wrapper: ({ children }) => (
<Provider
data={{
myList: [{ isUndefined: undefined }],
}}
>
<Iterate.Array path="/myList">{children}</Iterate.Array>
</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})
})

describe('pathTruthy', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ export default function useVisibility(props?: Partial<Props>) {
if (pathDefined) {
return getValue(makeLocalPath(pathDefined)) !== undefined
}
if (
pathUndefined &&
pointer.has(data, makeLocalPath(pathUndefined))
) {
return false
if (pathUndefined) {
return getValue(makeLocalPath(pathUndefined)) === undefined
}

if (pathTrue && getValue(makeLocalPath(pathTrue)) !== true) {
Expand All @@ -136,6 +133,7 @@ export default function useVisibility(props?: Partial<Props>) {
if (pathFalse && getValue(makeLocalPath(pathFalse)) !== false) {
return false
}

if (
pathTruthy &&
Boolean(getValue(makeLocalPath(pathTruthy))) === false
Expand All @@ -148,6 +146,7 @@ export default function useVisibility(props?: Partial<Props>) {
) {
return false
}

if (inferData && !inferData(data)) {
return false
}
Expand Down

0 comments on commit a789ea2

Please sign in to comment.