Skip to content

Commit 1010403

Browse files
committed
100% coverage
1 parent 8dced6b commit 1010403

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,8 @@ module.exports = (reviver, opts = {}) => {
6161
}
6262

6363
function sortProps(props, component, ...children) {
64-
if (props && props.class) {
65-
if (typeof props.class === 'string') {
66-
//
67-
} else if (Array.isArray(props.class)) {
68-
props.class = props.class.join(' ')
69-
} else {
70-
console.error(props.class);
71-
throw new Error(`This shouldn't have happened. Class is neither string nor Array`)
72-
}
64+
if (props && props.class && Array.isArray(props.class)) {
65+
props.class = props.class.join(' ')
7366
}
7467
applyKeyMap(props, component, ...children);
7568
return props;
@@ -141,6 +134,8 @@ module.exports = (reviver, opts = {}) => {
141134
delete props[key];
142135
} else if (typeof map === 'function') {
143136
map(props, ...rest);
137+
} else {
138+
throw new Error(`Invalid opts.keyMap Expected a string or a function, got ${typeof map}`);
144139
}
145140
}
146141
return props;

test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const deepIt = (ex, eq) => it(ex, () => assert[typeof eq === 'string' ? 'equal'
88
const _deepIt = deepIt;
99

1010
/* Basic */
11+
it('should throw without reviver', () => assert.throws(hyperchain));
12+
it('should throw without component', () => assert.throws(h));
1113
deepIt(`h('div')`, {
1214
component: 'div',
1315
props: null,
@@ -141,6 +143,11 @@ deepIt(`h.div({class: {object: true, class: false}})`, {
141143
props: { class: 'object' },
142144
children: []
143145
});
146+
deepIt(`h.div({class: 'prop'})`, {
147+
component: 'div',
148+
props: { class: 'prop' },
149+
children: []
150+
});
144151

145152
describe('separation', () => {
146153
const red = h.div.red;
@@ -235,14 +242,16 @@ describe('opts.filterFalseyChildren', () => {
235242

236243

237244
describe('opts.elementMap', () => {
238-
const h = hyperchain(r, { elementMap: { div: 'p' } });
245+
const h = hyperchain(r, { elementMap: { div: 'p', null: null } });
239246
const deepIt = eval(String(_deepIt));
240247

241248
deepIt(`h.div()`, {
242249
component: 'p',
243250
props: null,
244251
children: []
245252
});
253+
254+
assert.throws(() => h.null());
246255
});
247256

248257
describe('opts.keyMap', () => {
@@ -254,6 +263,36 @@ describe('opts.keyMap', () => {
254263
props: { className: 'class' },
255264
children: []
256265
});
266+
describe('function', () => {
267+
const h = hyperchain(r, {
268+
keyMap: {
269+
class: (props, component, ...children) => {
270+
if (component !== 'fragment') {
271+
props.className = props.class;
272+
}
273+
delete props.class;
274+
}
275+
}
276+
});
277+
const deepIt = eval(String(_deepIt));
278+
279+
deepIt(`h.div.class()`, {
280+
component: 'div',
281+
props: { className: 'class' },
282+
children: []
283+
});
284+
deepIt(`h.fragment.class()`, {
285+
component: 'fragment',
286+
props: {},
287+
children: []
288+
});
289+
});
290+
describe('other', () => {
291+
const h = hyperchain(r, { keyMap: { class: 1 } });
292+
const deepIt = eval(String(_deepIt));
293+
294+
assert.throws(() => h.div.class());
295+
});
257296
});
258297

259298
describe('opts.style', () => {
@@ -265,6 +304,11 @@ describe('opts.style', () => {
265304
props: { class: 'hashed' },
266305
children: []
267306
});
307+
deepIt(`h.div.other()`, {
308+
component: 'div',
309+
props: { class: 'other' },
310+
children: []
311+
});
268312
});
269313

270314
describe('opts.stylePreserveNames', () => {
@@ -288,4 +332,8 @@ describe('text', () => {
288332
deepIt(`h.div('a', 'b')`, '<div>ab</div>');
289333
deepIt(`h.div(h.div('a'), 'b', h.div('c'))`, '<div><div>a</div>b<div>c</div></div>');
290334
deepIt(`h.div({id:'app'})`, '<div id="app"></div>');
335+
deepIt(`h.div({bool:false})`, '<div bool="false"></div>');
336+
deepIt(`h.div({num:1})`, '<div num=1></div>');
337+
deepIt(`h.div()`, '<div></div>');
338+
deepIt(`h.div([['a']])`, '<div>a</div>');
291339
});

text.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ function toString(tag, ...args) {
1111
.map(prop => {
1212
const val = props[prop];
1313
switch (typeof val) {
14-
case 'boolean':
1514
case 'number':
1615
case 'string':
1716
return ` ${prop}=${JSON.stringify(val)}`;
17+
case 'boolean':
18+
return ` ${prop}="${JSON.stringify(val)}"`;
1819
}
1920
})
2021
.filter(Boolean)
2122
.join('')
2223
+ `>`
23-
+ (_.flat(children || [])).join('')
24+
+ (_.flat(children)).join('')
2425
+ `</${tag}>`;
2526
}

0 commit comments

Comments
 (0)