Skip to content

Commit

Permalink
100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
laggingreflex committed Jul 16, 2018
1 parent 8dced6b commit 1010403
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
13 changes: 4 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,8 @@ module.exports = (reviver, opts = {}) => {
}

function sortProps(props, component, ...children) {
if (props && props.class) {
if (typeof props.class === 'string') {
//
} else if (Array.isArray(props.class)) {
props.class = props.class.join(' ')
} else {
console.error(props.class);
throw new Error(`This shouldn't have happened. Class is neither string nor Array`)
}
if (props && props.class && Array.isArray(props.class)) {
props.class = props.class.join(' ')
}
applyKeyMap(props, component, ...children);
return props;
Expand Down Expand Up @@ -141,6 +134,8 @@ module.exports = (reviver, opts = {}) => {
delete props[key];
} else if (typeof map === 'function') {
map(props, ...rest);
} else {
throw new Error(`Invalid opts.keyMap Expected a string or a function, got ${typeof map}`);
}
}
return props;
Expand Down
50 changes: 49 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const deepIt = (ex, eq) => it(ex, () => assert[typeof eq === 'string' ? 'equal'
const _deepIt = deepIt;

/* Basic */
it('should throw without reviver', () => assert.throws(hyperchain));
it('should throw without component', () => assert.throws(h));
deepIt(`h('div')`, {
component: 'div',
props: null,
Expand Down Expand Up @@ -141,6 +143,11 @@ deepIt(`h.div({class: {object: true, class: false}})`, {
props: { class: 'object' },
children: []
});
deepIt(`h.div({class: 'prop'})`, {
component: 'div',
props: { class: 'prop' },
children: []
});

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


describe('opts.elementMap', () => {
const h = hyperchain(r, { elementMap: { div: 'p' } });
const h = hyperchain(r, { elementMap: { div: 'p', null: null } });
const deepIt = eval(String(_deepIt));

deepIt(`h.div()`, {
component: 'p',
props: null,
children: []
});

assert.throws(() => h.null());
});

describe('opts.keyMap', () => {
Expand All @@ -254,6 +263,36 @@ describe('opts.keyMap', () => {
props: { className: 'class' },
children: []
});
describe('function', () => {
const h = hyperchain(r, {
keyMap: {
class: (props, component, ...children) => {
if (component !== 'fragment') {
props.className = props.class;
}
delete props.class;
}
}
});
const deepIt = eval(String(_deepIt));

deepIt(`h.div.class()`, {
component: 'div',
props: { className: 'class' },
children: []
});
deepIt(`h.fragment.class()`, {
component: 'fragment',
props: {},
children: []
});
});
describe('other', () => {
const h = hyperchain(r, { keyMap: { class: 1 } });
const deepIt = eval(String(_deepIt));

assert.throws(() => h.div.class());
});
});

describe('opts.style', () => {
Expand All @@ -265,6 +304,11 @@ describe('opts.style', () => {
props: { class: 'hashed' },
children: []
});
deepIt(`h.div.other()`, {
component: 'div',
props: { class: 'other' },
children: []
});
});

describe('opts.stylePreserveNames', () => {
Expand All @@ -288,4 +332,8 @@ describe('text', () => {
deepIt(`h.div('a', 'b')`, '<div>ab</div>');
deepIt(`h.div(h.div('a'), 'b', h.div('c'))`, '<div><div>a</div>b<div>c</div></div>');
deepIt(`h.div({id:'app'})`, '<div id="app"></div>');
deepIt(`h.div({bool:false})`, '<div bool="false"></div>');
deepIt(`h.div({num:1})`, '<div num=1></div>');
deepIt(`h.div()`, '<div></div>');
deepIt(`h.div([['a']])`, '<div>a</div>');
});
5 changes: 3 additions & 2 deletions text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ function toString(tag, ...args) {
.map(prop => {
const val = props[prop];
switch (typeof val) {
case 'boolean':
case 'number':
case 'string':
return ` ${prop}=${JSON.stringify(val)}`;
case 'boolean':
return ` ${prop}="${JSON.stringify(val)}"`;
}
})
.filter(Boolean)
.join('')
+ `>`
+ (_.flat(children || [])).join('')
+ (_.flat(children)).join('')
+ `</${tag}>`;
}

0 comments on commit 1010403

Please sign in to comment.