Skip to content

Commit

Permalink
fix(Forms): ensure pathDefined on Form.Visibility does not render w…
Browse files Browse the repository at this point in the history
…hen value is `undefined` (#4539)
  • Loading branch information
tujoworker authored Feb 6, 2025
1 parent c524b71 commit 4f99680
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ describe('useVisibility', () => {
)
expect(result.current.check()).toBe(false)
})

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({
pathDefined: '/isUndefined',
})
).toBe(false)
})
})

describe('pathUndefined', () => {
Expand Down Expand Up @@ -679,6 +692,28 @@ describe('useVisibility', () => {
)
expect(result.current.check()).toBe(false)
})

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

describe('pathUndefined', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ export default function useVisibility(props?: Partial<Props>) {
}
}

if (pathDefined && !pointer.has(data, makeLocalPath(pathDefined))) {
return false
const getValue = (path: Path) => {
if (pointer.has(data, path)) {
return pointer.get(data, path)
}
}

if (pathDefined) {
return getValue(makeLocalPath(pathDefined)) !== undefined
}
if (
pathUndefined &&
Expand All @@ -124,12 +130,6 @@ export default function useVisibility(props?: Partial<Props>) {
return false
}

const getValue = (path: Path) => {
if (pointer.has(data, path)) {
return pointer.get(data, path)
}
}

if (pathTrue && getValue(makeLocalPath(pathTrue)) !== true) {
return false
}
Expand Down

0 comments on commit 4f99680

Please sign in to comment.