Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* alternative use id * reliable tagging * skip fragments * remove mask var * reduce diff * types * use hash func" * less bytes * fix inheritance bug * optimize * tweak logic * fix copy pate * less bytes * add compat * types * add consideration * add useId test highlighting subsequent render dangers * fix client side renders * revert to prior implementation * optim
- Loading branch information
1 parent
1427d58
commit 803dbb5
Showing
6 changed files
with
176 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
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,132 @@ | ||
import { createElement, render } from 'preact'; | ||
import { useId, useState } from 'preact/hooks'; | ||
import { setupRerender } from 'preact/test-utils'; | ||
import { setupScratch, teardown } from '../../../test/_util/helpers'; | ||
|
||
/** @jsx createElement */ | ||
|
||
describe('useId', () => { | ||
/** @type {HTMLDivElement} */ | ||
let scratch, rerender; | ||
|
||
beforeEach(() => { | ||
scratch = setupScratch(); | ||
rerender = setupRerender(); | ||
}); | ||
|
||
afterEach(() => { | ||
teardown(scratch); | ||
}); | ||
|
||
it('keeps the id consistent after an update', () => { | ||
function Comp() { | ||
const id = useId(); | ||
return <div id={id} />; | ||
} | ||
|
||
render(<Comp />, scratch); | ||
const id = scratch.firstChild.getAttribute('id'); | ||
expect(scratch.firstChild.getAttribute('id')).to.equal(id); | ||
|
||
render(<Comp />, scratch); | ||
expect(scratch.firstChild.getAttribute('id')).to.equal(id); | ||
}); | ||
|
||
it('ids are unique according to dom-depth', () => { | ||
function Child() { | ||
const id = useId(); | ||
const spanId = useId(); | ||
return ( | ||
<div id={id}> | ||
<span id={spanId}>h</span> | ||
</div> | ||
); | ||
} | ||
|
||
function Comp() { | ||
const id = useId(); | ||
return ( | ||
<div id={id}> | ||
<Child /> | ||
</div> | ||
); | ||
} | ||
|
||
render(<Comp />, scratch); | ||
expect(scratch.innerHTML).to.equal( | ||
'<div id="P481"><div id="P15361"><span id="P15362">h</span></div></div>' | ||
); | ||
|
||
render(<Comp />, scratch); | ||
expect(scratch.innerHTML).to.equal( | ||
'<div id="P481"><div id="P15361"><span id="P15362">h</span></div></div>' | ||
); | ||
}); | ||
|
||
it('ids are unique across siblings', () => { | ||
function Child() { | ||
const id = useId(); | ||
return <span id={id}>h</span>; | ||
} | ||
|
||
function Comp() { | ||
const id = useId(); | ||
return ( | ||
<div id={id}> | ||
<Child /> | ||
<Child /> | ||
<Child /> | ||
</div> | ||
); | ||
} | ||
|
||
render(<Comp />, scratch); | ||
expect(scratch.innerHTML).to.equal( | ||
'<div id="P481"><span id="P15361">h</span><span id="P15671">h</span><span id="P15981">h</span></div>' | ||
); | ||
|
||
render(<Comp />, scratch); | ||
expect(scratch.innerHTML).to.equal( | ||
'<div id="P481"><span id="P15361">h</span><span id="P15671">h</span><span id="P15981">h</span></div>' | ||
); | ||
}); | ||
|
||
it('correctly handles new elements', () => { | ||
let set; | ||
function Child() { | ||
const id = useId(); | ||
return <span id={id}>h</span>; | ||
} | ||
|
||
function Stateful() { | ||
const [state, setState] = useState(false); | ||
set = setState; | ||
return ( | ||
<div> | ||
<Child /> | ||
{state && <Child />} | ||
</div> | ||
); | ||
} | ||
|
||
function Comp() { | ||
const id = useId(); | ||
return ( | ||
<div id={id}> | ||
<Stateful /> | ||
</div> | ||
); | ||
} | ||
|
||
render(<Comp />, scratch); | ||
expect(scratch.innerHTML).to.equal( | ||
'<div id="P481"><div><span id="P476641">h</span></div></div>' | ||
); | ||
|
||
set(true); | ||
rerender(); | ||
expect(scratch.innerHTML).to.equal( | ||
'<div id="P481"><div><span id="P476641">h</span><span id="P486251">h</span></div></div>' | ||
); | ||
}); | ||
}); |