-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #611 from balsa-asanovic/use-node-siblings-hook
Use component siblings custom hook
- Loading branch information
Showing
4 changed files
with
128 additions
and
30 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
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 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); | ||
}); | ||
}); |