Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
100% test coverage!
Browse files Browse the repository at this point in the history
  • Loading branch information
hharnisc committed Jan 27, 2017
1 parent b7ccb65 commit 3462625
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"plugins": [
"react"
],
"env": {
"jest": true
},
"rules": {
"consistent-return": 0,
"func-names": 0,
Expand Down
5 changes: 2 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint prefer-spread: 0 */
// Inspired by: https://github.com/JedWatson/classnames
const hasOwn = {}.hasOwnProperty;

Expand All @@ -8,7 +7,7 @@ export const classNames = (styles, ...args) => {
if (argType === 'string' || argType === 'number') {
classes.push(arg);
} else if (Array.isArray(arg)) {
classes.push(classNames.apply(null, arg));
classes = classes.concat(arg);
} else if (argType === 'object') {
Object.keys(arg).forEach((key) => {
if (hasOwn.call(arg, key) && arg[key]) {
Expand All @@ -18,7 +17,7 @@ export const classNames = (styles, ...args) => {
}
return classes;
}, []);
return styleKeys.map(key => styles[key]).join(' ');
return [...new Set(styleKeys)].map(key => styles[key]).join(' ');
};

export const cleanProps = (props, propTypes) =>
Expand Down
97 changes: 97 additions & 0 deletions lib/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { classNames } from './utils';

describe('utils', () => {
describe('classNames', () => {
it('should calculate string input class ', () => {
const styles = {
myClass: 'my class',
};
expect(classNames(styles, 'myClass'))
.toBe(styles.myClass);
});

it('should calculate int arg classes', () => {
const styles = {
1: 'uno',
};
expect(classNames(styles, 1))
.toBe(`${styles[1]}`);
});

it('should calculate multiple arg classes', () => {
const styles = {
myClass: 'my class',
myOtherClass: 'my other class',
};

expect(classNames(styles, 'myClass', 'myOtherClass'))
.toBe(`${styles.myClass} ${styles.myOtherClass}`);
});

it('should calculate array of classes', () => {
const styles = {
myClass: 'my class',
myOtherClass: 'my other class',
};
expect(classNames(styles, ['myClass', 'myOtherClass']))
.toBe(`${styles.myClass} ${styles.myOtherClass}`);
});

it('should not duplicate classes', () => {
const styles = {
myClass: 'my class',
};
expect(classNames(styles, 'myClass', 'myClass'))
.toBe(`${styles.myClass}`);
});

it('should handle object of classes', () => {
const styles = {
myClass: 'my class',
myOtherClass: 'my other class',
};
const switches = {
myClass: true,
myOtherClass: true,
};
expect(classNames(styles, switches))
.toBe(`${styles.myClass} ${styles.myOtherClass}`);
});

it('should handle object with some classes switched off', () => {
const styles = {
myClass: 'my class',
myOtherClass: 'my other class',
};
const switches = {
myClass: true,
myOtherClass: false,
};
expect(classNames(styles, switches))
.toBe(`${styles.myClass}`);
});

it('should handle multiple input types', () => {
const styles = {
myClass: 'my class',
myOtherClass: 'my other class',
1: 'uno',
stringClass: 'string class',
arrayItemOne: 'array one',
arrayItemTwo: 'array two',
};
const switches = {
myClass: true,
myOtherClass: true,
};
expect(classNames(
styles,
'stringClass',
1,
['arrayItemOne', 'arrayItemTwo'],
switches,
))
.toBe(`${styles.stringClass} ${styles[1]} ${styles.arrayItemOne} ${styles.arrayItemTwo} ${styles.myClass} ${styles.myOtherClass}`);
});
});
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
},
"coverageThreshold": {
"global": {
"branches": 93,
"branches": 100,
"functions": 100,
"lines": 99,
"statements": 99
"lines": 100,
"statements": 100
}
}
},
Expand Down

0 comments on commit 3462625

Please sign in to comment.