Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 56eb57f

Browse files
authored
Merge pull request #28 from csvenke/test/increase-test-coverage
* test: increase test coverage
2 parents 00377c0 + 3b3443e commit 56eb57f

File tree

11 files changed

+92
-33
lines changed

11 files changed

+92
-33
lines changed

jest.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ module.exports = {
1414
statements: 90,
1515
},
1616
},
17-
collectCoverageFrom: ['src/@(components|utils)/**/*.@(ts|tsx)'],
18-
coveragePathIgnorePatterns: ['index.ts'],
17+
collectCoverageFrom: ['src/@(components|utils)/**/*.@(ts|tsx)']
1918
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
}
100100
},
101101
"lint-staged": {
102-
"src/**/*.{ts,tsx}": [
102+
"*.{ts,tsx}": [
103103
"prettier -c .prettierrc --write",
104104
"tslint -c tslint.json --fix",
105105
"git add"

src/components/List/List.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as PropTypes from 'prop-types';
22
import * as React from 'react';
33

4-
export type ListItemCallbackFn = (
4+
type ListItemCallbackFn = (
55
value?: any,
66
index?: number,
77
array?: any[],
@@ -24,7 +24,7 @@ export interface IListProps {
2424
const List: React.SFC<IListProps> = ({ items, render, children }) => {
2525
const func = children ? children : render;
2626

27-
if (!func) {
27+
if (typeof func !== 'function') {
2828
return null;
2929
}
3030

src/components/Show/Show.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as PropTypes from 'prop-types';
22
import * as React from 'react';
33

4-
import { isEmptyChildren } from '../../utils';
4+
import { getChildrenOrRender } from '../../utils';
55

66
export interface IShowProps {
77
/** Conditional statement. */
@@ -18,17 +18,7 @@ export interface IShowProps {
1818
* Semantic helper component that return content if `when` equals true.
1919
*/
2020
const Show: React.SFC<IShowProps> = ({ when, render, children }) => {
21-
if (when) {
22-
if (children && !isEmptyChildren(children)) {
23-
return React.Children.only(children);
24-
}
25-
26-
if (render) {
27-
return <React.Fragment>{render()}</React.Fragment>;
28-
}
29-
}
30-
31-
return null;
21+
return when ? getChildrenOrRender(children, render) : null;
3222
};
3323

3424
Show.propTypes = {

src/components/Switch/Switch.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ class Switch extends React.Component<ISwitchProps> {
5454
}
5555

5656
private getElementValue = (element: any) => {
57-
if (element && element.props) {
58-
return element.props.value;
59-
}
60-
return undefined;
57+
return element.props.value;
6158
};
6259

6360
private isValidChild = (child: any) => {

src/components/Switch/SwitchCase.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as PropTypes from 'prop-types';
22
import * as React from 'react';
33

4-
import { isEmptyChildren } from '../../utils';
4+
import { getChildrenOrRender } from '../../utils/';
55

66
export interface ISwitchCaseProps {
77
/** Conditional statement. */
@@ -17,18 +17,9 @@ export interface ISwitchCaseProps {
1717
/**
1818
* Semantic helper component that can be accessed from the `Switch` component.
1919
*/
20+
// @ts-ignore `value` prop is used by parent component Switch.
2021
const SwitchCase: React.SFC<ISwitchCaseProps> = ({ value, render, children }) => {
21-
if (value !== undefined) {
22-
if (children && !isEmptyChildren(children)) {
23-
return React.Children.only(children);
24-
}
25-
26-
if (render) {
27-
return <React.Fragment>{render()}</React.Fragment>;
28-
}
29-
}
30-
31-
return null;
22+
return getChildrenOrRender(children, render);
3223
};
3324

3425
SwitchCase.propTypes = {

src/components/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { List, Resolve, Show, Switch } from './index';
2+
3+
test('List', () => {
4+
expect(List).toBeDefined();
5+
});
6+
7+
test('Resolve', () => {
8+
expect(Resolve).toBeDefined();
9+
});
10+
11+
test('Show', () => {
12+
expect(Show).toBeDefined();
13+
});
14+
15+
test('Switch', () => {
16+
expect(Switch).toBeDefined();
17+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { shallow } from 'enzyme';
2+
import * as React from 'react';
3+
4+
import getRenderOrChildren from './getRenderOrChildren';
5+
6+
describe('with children node and render function', () => {
7+
const children = <div>result</div>;
8+
const render = () => <div>result</div>;
9+
const result = '<div>result</div>';
10+
11+
test('should return content from children', () => {
12+
const wrapper = shallow(getRenderOrChildren(children, render));
13+
expect(wrapper.html()).toEqual(result);
14+
});
15+
16+
test('should return content from render', () => {
17+
const wrapper = shallow(getRenderOrChildren(undefined, render));
18+
expect(wrapper.html()).toEqual(result);
19+
});
20+
});
21+
22+
describe('with children function and render function', () => {
23+
const children = () => <div>result</div>;
24+
const render = () => <div>result</div>;
25+
const result = '<div>result</div>';
26+
27+
test('should return content from children', () => {
28+
const wrapper = shallow(getRenderOrChildren(children, render));
29+
expect(wrapper.html()).toEqual(result);
30+
});
31+
32+
test('should return content from render', () => {
33+
const wrapper = shallow(getRenderOrChildren(undefined, render));
34+
expect(wrapper.html()).toEqual(result);
35+
});
36+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as React from 'react';
2+
3+
import isEmptyChildren from '../isEmptyChildren/isEmptyChildren';
4+
5+
const getChildrenOrRender = (children?: any, render?: () => React.ReactNode) => {
6+
if (typeof children === 'function') {
7+
return children();
8+
}
9+
10+
if (children && !isEmptyChildren(children)) {
11+
return React.Children.only(children);
12+
}
13+
14+
if (render) {
15+
return render();
16+
}
17+
18+
return null;
19+
};
20+
21+
export default getChildrenOrRender;

src/utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
export { default as isEmptyChildren } from './isEmptyChildren/isEmptyChildren';
2+
export {
3+
default as getChildrenOrRender,
4+
} from './getRenderOrChildren/getRenderOrChildren';

0 commit comments

Comments
 (0)