Skip to content

Commit 4e25f56

Browse files
dnlkochsimonseyock
authored andcommitted
refactor: to function component
1 parent 9a2d062 commit 4e25f56

33 files changed

+1599
-2159
lines changed
+26-28
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1-
import TestUtil from '../../Util/TestUtil';
2-
import SimpleButton, {SimpleButtonProps} from './SimpleButton';
1+
import { fireEvent,render, screen } from '@testing-library/react';
2+
import React from 'react';
3+
4+
import SimpleButton from './SimpleButton';
35

46
describe('<SimpleButton />', () => {
7+
58
it('is defined', () => {
6-
expect(SimpleButton).not.toBeUndefined();
9+
expect(SimpleButton).toBeDefined();
710
});
811

9-
it('can be rendered', () => {
10-
const wrapper = TestUtil.mountComponent(SimpleButton);
11-
expect(wrapper).not.toBeUndefined();
12+
it('can rendered', () => {
13+
render(<SimpleButton />);
14+
expect(screen.getByRole('button')).toBeVisible();
1215
});
1316

1417
it('allows to set some props', () => {
15-
const wrapper = TestUtil.mountComponent(SimpleButton);
16-
17-
wrapper.setProps({
18-
type: 'secondary',
19-
shape: 'circle',
20-
size: 'small',
21-
disabled: true
22-
});
23-
24-
const props = wrapper.props() as SimpleButtonProps;
25-
expect(props.type).toBe('secondary');
26-
expect(props.shape).toBe('circle');
27-
expect(props.size).toBe('small');
28-
expect(props.disabled).toBe(true);
29-
30-
expect(wrapper.find('button.ant-btn-secondary').length).toBe(1);
31-
expect(wrapper.find('button.ant-btn-circle').length).toBe(1);
32-
expect(wrapper.find('button.ant-btn-sm').length).toBe(1);
33-
expect(wrapper.find('button').length).toBe(1);
18+
render(
19+
<SimpleButton
20+
type="default"
21+
shape="circle"
22+
size="small"
23+
disabled={true}
24+
/>
25+
);
26+
27+
const button = screen.getByRole('button');
28+
expect(button).toHaveClass('ant-btn-default');
29+
expect(button).toHaveClass('ant-btn-circle');
30+
expect(button).toHaveClass('ant-btn-sm');
31+
expect(button).toBeDisabled();
3432
});
3533

3634
it('calls a given click callback method onClick', () => {
3735
const onClick = jest.fn();
38-
const wrapper = TestUtil.mountComponent(SimpleButton, {onClick});
36+
render(<SimpleButton onClick={onClick} />);
3937

40-
wrapper.find('button').simulate('click');
38+
const button = screen.getByRole('button');
39+
fireEvent.click(button);
4140

4241
expect(onClick).toHaveBeenCalledTimes(1);
4342
});
44-
4543
});
+38-66
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
import { Button, Tooltip } from 'antd';
22
import { ButtonProps } from 'antd/lib/button';
3-
import { AbstractTooltipProps,TooltipPlacement } from 'antd/lib/tooltip';
3+
import { AbstractTooltipProps, TooltipPlacement } from 'antd/lib/tooltip';
44
import * as React from 'react';
55

66
import { CSS_PREFIX } from '../../constants';
77

8-
interface DefaultProps {
9-
type: 'default' | 'primary' | 'ghost' | 'dashed' | 'danger' | 'link';
8+
export type OwnProps = {
109
/**
1110
* Additional [antd tooltip](https://ant.design/components/tooltip/)
1211
* properties to pass to the tooltip component. Note: The props `title`
1312
* and `placement` will override the props `tooltip` and `tooltipPlacement`
1413
* of this component!
1514
*/
16-
tooltipProps: AbstractTooltipProps;
17-
}
18-
19-
interface BaseProps {
15+
tooltipProps?: AbstractTooltipProps;
2016
/**
2117
* The tooltip to be shown on hover.
2218
*/
@@ -25,65 +21,41 @@ interface BaseProps {
2521
* The position of the tooltip.
2622
*/
2723
tooltipPlacement?: TooltipPlacement;
28-
}
29-
30-
export type SimpleButtonProps = BaseProps & Partial<DefaultProps> & ButtonProps;
31-
32-
/**
33-
* The SimpleButton.
34-
*
35-
* @class The SimpleButton
36-
* @extends React.Component
37-
*/
38-
class SimpleButton extends React.Component<SimpleButtonProps> {
39-
40-
/**
41-
* The default properties.
42-
*/
43-
static defaultProps: DefaultProps = {
44-
type: 'primary',
45-
tooltipProps: {
46-
mouseEnterDelay: 1.5
47-
}
48-
};
49-
50-
/**
51-
* The className added to this component.
52-
* @private
53-
*/
54-
className = `${CSS_PREFIX}simplebutton`;
55-
56-
/**
57-
* The render function.
58-
*/
59-
render() {
60-
const {
61-
className,
62-
tooltip,
63-
tooltipPlacement,
64-
tooltipProps,
65-
...antBtnProps
66-
} = this.props;
67-
68-
const finalClassName = className
69-
? `${className} ${this.className}`
70-
: this.className;
71-
72-
return (
73-
<Tooltip
74-
title={tooltip}
75-
placement={tooltipPlacement}
76-
{...tooltipProps}
24+
};
25+
26+
export type SimpleButtonProps = OwnProps & ButtonProps;
27+
28+
const defaultClassName = `${CSS_PREFIX}simplebutton`;
29+
30+
const SimpleButton: React.FC<SimpleButtonProps> = ({
31+
className,
32+
type = 'primary',
33+
tooltip,
34+
tooltipPlacement,
35+
tooltipProps = {
36+
mouseEnterDelay: 1.5
37+
},
38+
...passThroughProps
39+
}) => {
40+
41+
const finalClassName = className
42+
? `${className} ${defaultClassName}`
43+
: `${defaultClassName}`;
44+
45+
return (
46+
<Tooltip
47+
title={tooltip}
48+
placement={tooltipPlacement}
49+
{...tooltipProps}
50+
>
51+
<Button
52+
className={finalClassName}
53+
{...passThroughProps}
7754
>
78-
<Button
79-
className={finalClassName}
80-
{...antBtnProps}
81-
>
82-
{antBtnProps.children}
83-
</Button>
84-
</Tooltip>
85-
);
86-
}
87-
}
55+
{passThroughProps.children}
56+
</Button>
57+
</Tooltip>
58+
);
59+
};
8860

8961
export default SimpleButton;
+29-17
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1-
import TestUtil from '../../Util/TestUtil';
1+
import { fireEvent,render, screen } from '@testing-library/react';
2+
import * as React from 'react';
3+
24
import UploadButton from './UploadButton';
35

46
describe('<UploadButton />', () => {
57
it('is defined', () => {
6-
expect(UploadButton).not.toBeUndefined();
8+
expect(UploadButton).toBeDefined();
79
});
810

911
it('can be rendered', () => {
10-
const wrapper = TestUtil.mountComponent(UploadButton);
11-
expect(wrapper).not.toBeUndefined();
12+
const { container } = render(<UploadButton />);
13+
expect(container).toBeVisible();
1214
});
1315

14-
it('renders an inputfield', () => {
15-
const wrapper = TestUtil.mountComponent(UploadButton);
16-
expect(wrapper.find('input').length).toBe(1);
17-
});
16+
it('applies inputProps to the input field', () => {
17+
render(
18+
<UploadButton
19+
inputProps={{
20+
role: 'test'
21+
}}
22+
/>
23+
);
1824

19-
it('applies inputProps to the inputfield', () => {
20-
const wrapper = TestUtil.mountComponent(UploadButton, {inputProps: {multiple: true}});
21-
expect(wrapper.find('input[multiple=true]').length).toBe(1);
25+
expect(screen.getByRole('test')).toBeInTheDocument();
2226
});
2327

24-
it('renders a simplebutton if no children are given', () => {
25-
const wrapper = TestUtil.mountComponent(UploadButton);
26-
expect(wrapper.find('button').length).toBe(1);
28+
it('renders a simple button if no children are given', () => {
29+
render(<UploadButton />);
30+
expect(screen.getByRole('button')).toBeInTheDocument();
2731
});
2832

2933
it('calls a given click callback method onChange', () => {
3034
const onChange = jest.fn();
31-
const wrapper = TestUtil.mountComponent(UploadButton, {onChange});
32-
wrapper.find('input').simulate('change');
35+
render(
36+
<UploadButton
37+
onChange={onChange}
38+
inputProps={{
39+
role: 'test'
40+
}}
41+
/>
42+
);
43+
44+
fireEvent.change(screen.getByRole('test'));
45+
3346
expect(onChange).toHaveBeenCalledTimes(1);
3447
});
35-
3648
});

src/Button/UploadButton/UploadButton.tsx

+31-41
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as React from 'react';
55
import { CSS_PREFIX } from '../../constants';
66
import SimpleButton, { SimpleButtonProps } from '../SimpleButton/SimpleButton';
77

8-
interface BaseProps {
8+
export type OwnProps = {
99
/**
1010
* The className which should be added.
1111
*/
@@ -18,55 +18,45 @@ interface BaseProps {
1818
* The onChange handler for the upload input field.
1919
*/
2020
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
21-
}
21+
};
2222

23-
export type UploadButtonProps = BaseProps & SimpleButtonProps;
23+
export type UploadButtonProps = OwnProps & SimpleButtonProps;
2424

2525
/**
26-
* Class representing an upload button. Can be used as wrapper if children
26+
* Component representing an upload button. Can be used as wrapper if children
2727
* are given. Otherwise a Simplebutton will be rendered.
2828
*
2929
* To use a text with the UploadButton provide a SimpleButton as children.
3030
*
3131
* This automatically supports uploads via drag and drop from the operating
3232
* system.
33-
*
34-
* @class The UploadButton
35-
* @extends React.Component
3633
*/
37-
class UploadButton extends React.Component<UploadButtonProps> {
38-
39-
/**
40-
* The className added to this component.
41-
* @private
42-
*/
43-
_className = `${CSS_PREFIX}uploadbutton`;
44-
45-
/**
46-
* The render function.
47-
*/
48-
render() {
49-
const {
50-
className,
51-
children,
52-
onChange,
53-
inputProps,
54-
...passThroughProps
55-
} = this.props;
56-
57-
const finalClassName = className
58-
? `${className} ${this._className}`
59-
: this._className;
60-
61-
const button = <SimpleButton {...passThroughProps} />;
62-
63-
return (
64-
<div className={finalClassName}>
65-
{children || button}
66-
<input type="file" onChange={onChange} {...inputProps} />
67-
</div>
68-
);
69-
}
70-
}
34+
const UploadButton: React.FC<UploadButtonProps> = ({
35+
className,
36+
children,
37+
onChange,
38+
inputProps,
39+
...passThroughProps
40+
}) => {
41+
42+
const finalClassName = className
43+
? `${className} ${CSS_PREFIX}uploadbutton`
44+
: `${CSS_PREFIX}uploadbutton`;
45+
46+
const button = <SimpleButton {...passThroughProps} />;
47+
48+
return (
49+
<div
50+
className={finalClassName}
51+
>
52+
{children || button}
53+
<input
54+
type="file"
55+
onChange={onChange}
56+
{...inputProps}
57+
/>
58+
</div>
59+
);
60+
};
7161

7262
export default UploadButton;

0 commit comments

Comments
 (0)