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

Commit 0831c7c

Browse files
committed
feat: add tests to utility functions
1 parent b8b01fc commit 0831c7c

File tree

11 files changed

+106
-17
lines changed

11 files changed

+106
-17
lines changed

src/components/List/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const List = (props: IListProps): Output => {
2222
const { items, children, render } = props;
2323
const renderProp = getRenderProp(children, render);
2424

25-
if (!!renderProp && isFunction(renderProp)) {
25+
if (!!renderProp && isFunction<Map>(renderProp)) {
2626
return <React.Fragment>{items.map(renderProp)}</React.Fragment>;
2727
}
2828

src/components/Switch/Switch.tsx

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

44
import { Output } from '../../types';
5-
import { renderIf } from '../../utils';
5+
import { isElement, renderIf } from '../../utils';
66
import SwitchCase from './SwitchCase';
77
import SwitchDefault from './SwitchDefault';
88

@@ -14,16 +14,9 @@ interface ISwitchProps {
1414
children: React.ReactNode;
1515
}
1616

17-
const isSwitchChild = (child: any, element: any) => {
18-
return child.type.prototype === element.prototype;
19-
};
20-
21-
const isValidSwitchChild = (child: any) => {
22-
return (
23-
React.isValidElement(child) &&
24-
(isSwitchChild(child, SwitchCase) || isSwitchChild(child, SwitchDefault))
25-
);
26-
};
17+
const isValidSwitchChild = (child: any) =>
18+
React.isValidElement(child) &&
19+
(isElement(child, SwitchCase) || isElement(child, SwitchDefault));
2720

2821
/**
2922
* Renders content from first `Switch.Case` that matches `value`, else `Switch.Default` if it exists.
@@ -43,7 +36,7 @@ const Switch = (props: ISwitchProps): Output => {
4336
});
4437

4538
// No match found, return default if it exists.
46-
if (!match && isSwitchChild(child, SwitchDefault)) {
39+
if (!match && isElement(child, SwitchDefault)) {
4740
return React.cloneElement(child);
4841
}
4942

src/utils/getChildrenOrRender/getChildrenOrRender.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import getRenderProp from '../getRenderProp/getRenderProp';
55
import isEmptyChildren from '../isEmptyChildren/isEmptyChildren';
66
import isFunction from '../isFunction/isFunction';
77

8-
const getChildrenOrRender = (children?: unknown, render?: unknown): Output => {
8+
const getChildrenOrRender = (children?: any, render?: any): Output => {
99
const result = getRenderProp(children, render);
1010

1111
if (result) {
12-
if (isFunction(result)) {
12+
if (isFunction<() => Output>(result)) {
1313
return result();
1414
}
1515

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react';
2+
3+
import getRenderProp from '.';
4+
5+
const case1 = <div>children</div>;
6+
const case2 = () => <div>render</div>;
7+
8+
test('should return children', () => {
9+
// prettier-ignore
10+
const cases = [
11+
[case1, case2],
12+
[case1, undefined],
13+
[case1, null],
14+
];
15+
16+
cases.forEach(item => {
17+
const result = getRenderProp(item[0], item[1]);
18+
expect(result).toEqual(case1);
19+
});
20+
});
21+
22+
test('should return render', () => {
23+
const result = getRenderProp(undefined, case2)();
24+
expect(result).toEqual(case2());
25+
});
26+
27+
test('should return null', () => {
28+
// prettier-ignore
29+
const cases = [
30+
[undefined, undefined],
31+
[null, null],
32+
];
33+
34+
cases.forEach(item => {
35+
const result = getRenderProp(item[0], item[1]);
36+
expect(result).toEqual(null);
37+
});
38+
});

src/utils/getRenderProp/getRenderProp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import renderIf from '../renderIf/renderIf';
22

3-
const getRenderProp = (children?: unknown, render?: unknown) => {
3+
const getRenderProp = (children?: any, render?: any) => {
44
const result = children ? children : render;
55
return renderIf(!!result)(result);
66
};

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as getChildrenOrRender } from './getChildrenOrRender';
22
export { default as getRenderProp } from './getRenderProp';
3+
export { default as isElement } from './isElement';
34
export { default as isEmptyChildren } from './isEmptyChildren';
45
export { default as isFunction } from './isFunction';
56
export { default as renderIf } from './renderIf';

src/utils/isElement/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './isElement';

src/utils/isElement/isElement.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as React from 'react';
2+
3+
const isElement = (child: any, element: any): child is React.ReactElement<{}> =>
4+
child.type.prototype === element.prototype;
5+
6+
export default isElement;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as React from 'react';
2+
3+
import isFunction from '.';
4+
5+
test('should return true', () => {
6+
const result = isFunction(() => 'something');
7+
expect(result).toEqual(true);
8+
});
9+
10+
test('should return false', () => {
11+
const cases = [
12+
'something',
13+
false,
14+
true,
15+
undefined,
16+
null,
17+
<div>something</div>,
18+
[1, 2, 3],
19+
{ a: 'a', b: 'b' },
20+
];
21+
22+
cases.forEach(item => {
23+
const result = isFunction(item);
24+
expect(result).toEqual(false);
25+
});
26+
});

src/utils/isFunction/isFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const isFunction = (input: any): input is () => any => typeof input === 'function';
1+
const isFunction = <T>(input: any): input is T => typeof input === 'function';
22

33
export default isFunction;

0 commit comments

Comments
 (0)