/
useId.test.js
132 lines (112 loc) · 2.75 KB
/
useId.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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>'
);
});
});