-
Notifications
You must be signed in to change notification settings - Fork 75
Use component siblings custom hook #611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
imobachgs
merged 7 commits into
agama-project:master
from
balsa-asanovic:use-node-siblings-hook
Jun 21, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f287d5
created custom hook to alter HTML nodes attributes
balsa-asanovic 603fb10
added use of useNodesSiblings hook
balsa-asanovic 0c4496a
Merge branch 'openSUSE:master' into master
balsa-asanovic e44808e
modified desc of the hook; added noops for return
balsa-asanovic 3b7f653
added unit tests for useNodeSiblings hook
balsa-asanovic 37d6bfc
Merge branch 'openSUSE:master' into use-node-siblings-hook
balsa-asanovic 57a3fb5
added desc of changes to cockpit-agama.changes
balsa-asanovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
| import { noop } from "~/utils"; | ||
|
|
||
| /** | ||
| * Function for adding an attribute to a sibling | ||
| * | ||
| * @typedef {function} addAttributeFn | ||
| * @param {string} attribute - attribute name | ||
| * @param {*} value - value to set | ||
| */ | ||
|
|
||
| /** | ||
| * Function for removing an attribute from a sibling | ||
| * | ||
| * @typedef {function} removeAttributeFn | ||
| * @param {string} attribute - attribute name | ||
| */ | ||
|
|
||
| /** | ||
| * A hook for working with siblings of the node passed as parameter | ||
| * | ||
| * It returns an array with exactly two functions: | ||
| * - First for adding given attribute to siblings | ||
| * - Second for removing given attributes from siblings | ||
| * | ||
| * @param {HTMLElement} node | ||
| * @returns {[addAttributeFn, removeAttributeFn]} | ||
| */ | ||
| const useNodeSiblings = (node) => { | ||
| if (!node) return [noop, noop]; | ||
|
|
||
| const siblings = [...node.parentNode.children].filter(n => n !== node); | ||
|
|
||
| const addAttribute = (attribute, value) => { | ||
| siblings.forEach(sibling => { | ||
| sibling.setAttribute(attribute, value); | ||
| }); | ||
| }; | ||
|
|
||
| const removeAttribute = (attribute) => { | ||
| siblings.forEach(sibling => { | ||
| sibling.removeAttribute(attribute); | ||
| }); | ||
| }; | ||
|
|
||
| return [addAttribute, removeAttribute]; | ||
| }; | ||
|
|
||
| export default useNodeSiblings; |
This file contains hidden or 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,54 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
| import useNodeSiblings from './useNodeSiblings'; | ||
|
|
||
| // Mocked HTMLElement for testing | ||
| const mockNode = { | ||
| parentNode: { | ||
| children: [ | ||
| { setAttribute: jest.fn(), removeAttribute: jest.fn() }, // sibling 1 | ||
| { setAttribute: jest.fn(), removeAttribute: jest.fn() }, // sibling 2 | ||
| { setAttribute: jest.fn(), removeAttribute: jest.fn() }, // sibling 3 | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| describe('useNodeSiblings', () => { | ||
| it('should return noop functions when node is not provided', () => { | ||
| const { result } = renderHook(() => useNodeSiblings(null)); | ||
| const [addAttribute, removeAttribute] = result.current; | ||
|
|
||
| expect(addAttribute).toBeInstanceOf(Function); | ||
| expect(removeAttribute).toBeInstanceOf(Function); | ||
| expect(addAttribute).toEqual(expect.any(Function)); | ||
| expect(removeAttribute).toEqual(expect.any(Function)); | ||
|
|
||
| // Call the noop functions to ensure they don't throw any errors | ||
| expect(() => addAttribute('attribute', 'value')).not.toThrow(); | ||
| expect(() => removeAttribute('attribute')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('should add attribute to all siblings when addAttribute is called', () => { | ||
| const { result } = renderHook(() => useNodeSiblings(mockNode)); | ||
| const [addAttribute] = result.current; | ||
| const attributeName = 'attribute'; | ||
| const attributeValue = 'value'; | ||
|
|
||
| addAttribute(attributeName, attributeValue); | ||
|
|
||
| expect(mockNode.parentNode.children[0].setAttribute).toHaveBeenCalledWith(attributeName, attributeValue); | ||
| expect(mockNode.parentNode.children[1].setAttribute).toHaveBeenCalledWith(attributeName, attributeValue); | ||
| expect(mockNode.parentNode.children[2].setAttribute).toHaveBeenCalledWith(attributeName, attributeValue); | ||
| }); | ||
|
|
||
| it('should remove attribute from all siblings when removeAttribute is called', () => { | ||
| const { result } = renderHook(() => useNodeSiblings(mockNode)); | ||
| const [, removeAttribute] = result.current; | ||
| const attributeName = 'attribute'; | ||
|
|
||
| removeAttribute(attributeName); | ||
|
|
||
| expect(mockNode.parentNode.children[0].removeAttribute).toHaveBeenCalledWith(attributeName); | ||
| expect(mockNode.parentNode.children[1].removeAttribute).toHaveBeenCalledWith(attributeName); | ||
| expect(mockNode.parentNode.children[2].removeAttribute).toHaveBeenCalledWith(attributeName); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch 👍 As it is now, it will not become siblings as inert if the component is mounted as
isOpenin the future (for whatever reason, I have no use case in mind). But your change fixes it.