Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolm-kee committed Feb 9, 2019
1 parent a555139 commit 837ba58
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"test:coverage": "npm run test -- --coverage"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
26 changes: 26 additions & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const joinString = (delimiter, ...params) => {
const results = [];
for (let param of params) {
if (!param) {
continue;
}

const paramType = typeof param;

if (paramType == 'string' || paramType == 'number') {
results.push(param);
continue;
}

if (Array.isArray(param) && param.length > 0) {
const innerResult = joinString(delimiter, ...param);
if (innerResult) {
results.push(innerResult);
}
}
}

return results.join(delimiter);
};

export const classNames = joinString.bind(null, ' ');
9 changes: 9 additions & 0 deletions src/lib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { classNames } from './lib';

test('classNames', () => {
expect(classNames('btn', 'btn--default')).toBe('btn btn--default');
expect(
classNames('btn', true && 'btn--default', false && 'btn--raised', null)
).toBe('btn btn--default');
expect(classNames(['btn', null, 'btn--default'])).toBe('btn btn--default');
});

0 comments on commit 837ba58

Please sign in to comment.