Skip to content

Commit

Permalink
Add support to work within Iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Feb 4, 2025
1 parent b98f8b1 commit 0512db1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,12 @@ describe('useVisibility', () => {
it('does not render children when target path does not have a value other than "undefined"', () => {
const { result } = renderHook(useVisibility, {
wrapper: ({ children }) => (
<Provider data={{ isDefined: 'foo' }}>{children}</Provider>
<Provider data={{ isUndefined: undefined }}>{children}</Provider>
),
})
expect(
result.current.check({
valueDefined: '/notDefined',
valueDefined: '/isUndefined',
})
).toBe(false)
})
Expand Down Expand Up @@ -879,5 +879,51 @@ describe('useVisibility', () => {
).toBe(false)
})
})

describe('valueDefined', () => {
it('renders children when target path is defined', () => {
const { result } = renderHook(
() =>
useVisibility({
withinIterate: true,
valueDefined: '/isDefined',
}),
{
wrapper: ({ children }) => (
<Provider
data={{
myList: [{ isDefined: 'foo' }],
}}
>
<Iterate.Array path="/myList">{children}</Iterate.Array>
</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('does not render children when target path is not defined', () => {
const { result } = renderHook(
() =>
useVisibility({
withinIterate: true,
valueDefined: '/isUndefined',
}),
{
wrapper: ({ children }) => (
<Provider
data={{
myList: [{ isUndefined: undefined }],
}}
>
<Iterate.Array path="/myList">{children}</Iterate.Array>
</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export default function useVisibility(props?: Partial<Props>) {
originalData

if (valueDefined) {
const hasPath = pointer.has(data, valueDefined)
const hasPath = pointer.has(data, makeLocalPath(valueDefined))
if (hasPath) {
const value = pointer.get(data, valueDefined)
const value = pointer.get(data, makeLocalPath(valueDefined))
if (value !== undefined) {
return true
}
Expand Down

0 comments on commit 0512db1

Please sign in to comment.