-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Forms): console log a warning when Value.SummaryList gets an inva…
…lid child (#4249) More info [here](https://dnb-it.slack.com/archives/C07JEJVSHJR/p1730882739880279?thread_ts=1730737161.151109&cid=C07JEJVSHJR). It verifies that only allowed children are passed. It does this by comparing the amount of given children components vs the amount of the actual wanted components (verified via a context call). When there are more other children than the wanted ones, we show a warning⚠️ I'm not sure if there are smarter ways to do that. But as of now, I think this may work.
- Loading branch information
1 parent
505dbc1
commit 011e9eb
Showing
6 changed files
with
252 additions
and
2 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
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
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
131 changes: 131 additions & 0 deletions
131
...s/dnb-eufemia/src/extensions/forms/Value/SummaryList/__tests__/useVerifyChildren.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,131 @@ | ||
import React from 'react' | ||
import { render, act } from '@testing-library/react' | ||
import { useVerifyChildren, countChildren } from '../useVerifyChildren' | ||
|
||
describe('useVerifyChildren', () => { | ||
it('should not warn when no children are provided', () => { | ||
const log = jest.spyOn(console, 'log').mockImplementation() | ||
|
||
const TestComponent = () => { | ||
const { verifyChild } = useVerifyChildren({ | ||
children: null, | ||
message: 'Warning message', | ||
}) | ||
return <button onClick={verifyChild}>Verify</button> | ||
} | ||
|
||
render(<TestComponent />) | ||
|
||
expect(log).not.toHaveBeenCalled() | ||
|
||
log.mockRestore() | ||
}) | ||
|
||
it('should warn if verifyChild is not called enough times for children', () => { | ||
const log = jest.spyOn(console, 'log').mockImplementation() | ||
|
||
const TestComponent = () => { | ||
const { verifyChild } = useVerifyChildren({ | ||
children: ( | ||
<> | ||
<span>Child 1</span> | ||
<span>Child 2</span> | ||
</> | ||
), | ||
message: 'Warning message', | ||
}) | ||
return <button onClick={() => verifyChild()}>Verify</button> | ||
} | ||
|
||
const { getByText } = render(<TestComponent />) | ||
const button = getByText('Verify') | ||
|
||
act(() => button.click()) | ||
|
||
expect(log).toHaveBeenCalledWith(expect.any(String), 'Warning message') | ||
|
||
log.mockRestore() | ||
}) | ||
|
||
it('should ignore specified types', () => { | ||
const log = jest.spyOn(console, 'log').mockImplementation() | ||
|
||
const IgnoredComponent = () => <>Ignored</> | ||
|
||
const TestComponent = () => { | ||
const { verifyChild } = useVerifyChildren({ | ||
children: ( | ||
<> | ||
<IgnoredComponent /> | ||
<span>Child</span> | ||
</> | ||
), | ||
message: 'Warning message', | ||
ignoreTypes: ['IgnoredComponent'], | ||
}) | ||
|
||
verifyChild() // Call verifyChild once | ||
|
||
return <button onClick={() => verifyChild()}>Verify</button> | ||
} | ||
|
||
render(<TestComponent />) | ||
|
||
expect(log).not.toHaveBeenCalled() | ||
|
||
log.mockRestore() | ||
}) | ||
}) | ||
|
||
describe('countChildren', () => { | ||
it('should count valid children elements', () => { | ||
const children = ( | ||
<> | ||
<span>Child 1</span> | ||
{null} | ||
<span>Child 2</span> | ||
</> | ||
) | ||
|
||
const count = countChildren(children) | ||
expect(count).toBe(2) | ||
}) | ||
|
||
it('should not count fragments or ignored types', () => { | ||
const children = ( | ||
<> | ||
<span>Child 1</span> | ||
<React.Fragment> | ||
<span>Child 2</span> | ||
</React.Fragment> | ||
<span>Child 3</span> | ||
</> | ||
) | ||
|
||
const count = countChildren(children, ['span']) | ||
expect(count).toBe(3) | ||
}) | ||
|
||
it('should handle deeply nested structures', () => { | ||
const children = ( | ||
<> | ||
<span>Child 1</span> | ||
<> | ||
<span>Child 2</span> | ||
<> | ||
<span>Child 3</span> | ||
</> | ||
</> | ||
</> | ||
) | ||
|
||
const count = countChildren(children) | ||
expect(count).toBe(3) | ||
}) | ||
|
||
it('should return zero for primitive children', () => { | ||
const children = 'Primitive Text Node' | ||
const count = countChildren(children) | ||
expect(count).toBe(0) | ||
}) | ||
}) |
61 changes: 61 additions & 0 deletions
61
packages/dnb-eufemia/src/extensions/forms/Value/SummaryList/useVerifyChildren.ts
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,61 @@ | ||
import { | ||
Children, | ||
Fragment, | ||
isValidElement, | ||
useCallback, | ||
useEffect, | ||
useRef, | ||
} from 'react' | ||
import { warn } from '../../../../shared/helpers' | ||
|
||
export function useVerifyChildren({ | ||
children, | ||
message, | ||
ignoreTypes, | ||
}: { | ||
ignoreTypes?: Array<string> | ||
children: React.ReactNode | ||
message: string | ||
}) { | ||
const verifyCount = useRef(0) | ||
verifyCount.current = 0 | ||
const verifyChild = useCallback(() => { | ||
verifyCount.current += 1 | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
const count = countChildren(children, ignoreTypes) | ||
if (count > 0 && count > verifyCount.current) { | ||
warn(message) | ||
} | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [children, message]) | ||
|
||
return { verifyChild } | ||
} | ||
|
||
/** | ||
* Count the children of a React node, | ||
* without counting React.Fragment or primitive nodes. | ||
*/ | ||
export const countChildren = ( | ||
children: React.ReactNode, | ||
ignoreTypes?: Array<string>, | ||
count = 0 | ||
) => { | ||
return Children.toArray(children).reduce((count: number, child) => { | ||
if (child?.['type'] === Fragment) { | ||
return countChildren(child['props']?.children, ignoreTypes, count) | ||
} | ||
|
||
return ( | ||
count + | ||
(isValidElement(child) && | ||
!ignoreTypes?.includes(child?.type?.['name']) | ||
? 1 | ||
: 0) | ||
) | ||
}, count) | ||
} |
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