Skip to content

Commit 0fbc216

Browse files
committed
test: Finish up refactor
1 parent 522436a commit 0fbc216

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed

test/browser/index.test.jsx

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ import { useContext } from 'preact/hooks';
44
import { act } from 'preact/test-utils';
55
import registerElement from '../../src/index';
66

7-
/** @param {string} name */
8-
function createTestElement(name) {
9-
const el = document.createElement(name);
10-
const child1 = document.createElement('p');
11-
child1.textContent = 'Child 1';
12-
const child2 = document.createElement('p');
13-
child2.textContent = 'Child 2';
14-
el.appendChild(child1);
15-
el.appendChild(child2);
16-
return el;
17-
}
18-
197
describe('web components', () => {
208
/** @type {HTMLDivElement} */
219
let root;
@@ -29,13 +17,13 @@ describe('web components', () => {
2917
document.body.removeChild(root);
3018
});
3119

32-
function Clock({ time }) {
33-
return <span>{time}</span>;
34-
}
20+
it('renders ok, updates on attr change', () => {
21+
function Clock({ time }) {
22+
return <span>{time}</span>;
23+
}
3524

36-
registerElement(Clock, 'x-clock', ['time']);
25+
registerElement(Clock, 'x-clock', ['time']);
3726

38-
it('renders ok, updates on attr change', () => {
3927
const el = document.createElement('x-clock');
4028
el.setAttribute('time', '10:28:57 PM');
4129

@@ -52,14 +40,14 @@ describe('web components', () => {
5240
);
5341
});
5442

55-
function NullProps({ size = 'md' }) {
56-
return <div>{size.toUpperCase()}</div>;
57-
}
58-
59-
registerElement(NullProps, 'x-null-props', ['size'], { shadow: true });
60-
6143
// #50
6244
it('remove attributes without crashing', () => {
45+
function NullProps({ size = 'md' }) {
46+
return <div>{size.toUpperCase()}</div>;
47+
}
48+
49+
registerElement(NullProps, 'x-null-props', ['size'], { shadow: true });
50+
6351
const el = document.createElement('x-null-props');
6452
assert.doesNotThrow(() => (el.size = 'foo'));
6553
root.appendChild(el);
@@ -69,23 +57,29 @@ describe('web components', () => {
6957

7058
describe('DOM properties', () => {
7159
it('passes property changes to props', () => {
72-
const el = document.createElement('x-clock');
60+
function Clock({ time }) {
61+
return <span>{time}</span>;
62+
}
63+
64+
registerElement(Clock, 'x-clock-props', ['time']);
65+
66+
const el = document.createElement('x-clock-props');
7367

7468
el.time = '10:28:57 PM';
7569
assert.equal(el.time, '10:28:57 PM');
7670

7771
root.appendChild(el);
7872
assert.equal(
7973
root.innerHTML,
80-
'<x-clock time="10:28:57 PM"><span>10:28:57 PM</span></x-clock>'
74+
'<x-clock-props time="10:28:57 PM"><span>10:28:57 PM</span></x-clock-props>'
8175
);
8276

8377
el.time = '11:01:10 AM';
8478
assert.equal(el.time, '11:01:10 AM');
8579

8680
assert.equal(
8781
root.innerHTML,
88-
'<x-clock time="11:01:10 AM"><span>11:01:10 AM</span></x-clock>'
82+
'<x-clock-props time="11:01:10 AM"><span>11:01:10 AM</span></x-clock-props>'
8983
);
9084
});
9185

@@ -210,40 +204,54 @@ describe('web components', () => {
210204
);
211205
});
212206

213-
const kebabName = 'custom-date-long-name';
214-
const camelName = 'customDateLongName';
215-
const lowerName = camelName.toLowerCase();
216-
function PropNameTransform(props) {
217-
return (
218-
<span>
219-
{props[kebabName]} {props[lowerName]} {props[camelName]}
220-
</span>
221-
);
222-
}
223-
registerElement(PropNameTransform, 'x-prop-name-transform', [
224-
kebabName,
225-
camelName,
226-
]);
207+
describe('attribute/property name transformation', () => {
208+
const kebabName = 'custom-date-long-name';
209+
const camelName = 'customDateLongName';
210+
const lowerName = camelName.toLowerCase();
211+
function PropNameTransform(props) {
212+
return (
213+
<span>
214+
{props[kebabName]} {props[lowerName]} {props[camelName]}
215+
</span>
216+
);
217+
}
218+
registerElement(PropNameTransform, 'x-prop-name-transform', [
219+
kebabName,
220+
camelName,
221+
]);
227222

228-
it('handles kebab-case attributes with passthrough', () => {
229-
const el = document.createElement('x-prop-name-transform');
230-
el.setAttribute(kebabName, '11/11/2011');
231-
el.setAttribute(camelName, 'pretended to be camel');
223+
it('handles kebab-case attributes with passthrough', () => {
224+
const el = document.createElement('x-prop-name-transform');
225+
el.setAttribute(kebabName, '11/11/2011');
226+
el.setAttribute(camelName, 'pretended to be camel');
232227

233-
root.appendChild(el);
234-
assert.equal(
235-
root.innerHTML,
236-
`<x-prop-name-transform ${kebabName}="11/11/2011" ${lowerName}="pretended to be camel"><span>11/11/2011 pretended to be camel 11/11/2011</span></x-prop-name-transform>`
237-
);
228+
root.appendChild(el);
229+
assert.equal(
230+
root.innerHTML,
231+
`<x-prop-name-transform ${kebabName}="11/11/2011" ${lowerName}="pretended to be camel"><span>11/11/2011 pretended to be camel 11/11/2011</span></x-prop-name-transform>`
232+
);
238233

239-
el.setAttribute(kebabName, '01/01/2001');
240-
assert.equal(
241-
root.innerHTML,
242-
`<x-prop-name-transform ${kebabName}="01/01/2001" ${lowerName}="pretended to be camel"><span>01/01/2001 pretended to be camel 01/01/2001</span></x-prop-name-transform>`
243-
);
234+
el.setAttribute(kebabName, '01/01/2001');
235+
assert.equal(
236+
root.innerHTML,
237+
`<x-prop-name-transform ${kebabName}="01/01/2001" ${lowerName}="pretended to be camel"><span>01/01/2001 pretended to be camel 01/01/2001</span></x-prop-name-transform>`
238+
);
239+
});
244240
});
245241

246242
describe('children', () => {
243+
/** @param {string} name */
244+
function createTestElement(name) {
245+
const el = document.createElement(name);
246+
const child1 = document.createElement('p');
247+
child1.textContent = 'Child 1';
248+
const child2 = document.createElement('p');
249+
child2.textContent = 'Child 2';
250+
el.appendChild(child1);
251+
el.appendChild(child2);
252+
return el;
253+
}
254+
247255
it('supports controlling light DOM children', () => {
248256
function LightDomChildren({ children }) {
249257
return (

0 commit comments

Comments
 (0)