chore: Use unreachable exception in favor of type safety (#22933)

* Use unreachable exception in favor of type safety

* Add tests

* add test
This commit is contained in:
Eric Wang 2020-04-05 23:18:28 +10:00 committed by GitHub
parent d634184b58
commit 316b925f94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 9 deletions

View File

@ -0,0 +1,8 @@
import UnreachableException from '../unreachableException';
describe('UnreachableException', () => {
it('error thrown matches snapshot', () => {
const exception = new UnreachableException('some value');
expect(exception.message).toMatchInlineSnapshot(`"unreachable case: \\"some value\\""`);
});
});

View File

@ -0,0 +1,5 @@
export default class UnreachableException {
constructor(value: never) {
return new Error(`unreachable case: ${JSON.stringify(value)}`);
}
}

View File

@ -254,6 +254,12 @@ exports[`Button rtl render component should be rendered correctly in RTL directi
/>
`;
exports[`Button rtl render component should be rendered correctly in RTL direction 7`] = `
<div
class="ant-btn-group ant-btn-group-rtl"
/>
`;
exports[`Button should has click wave effect 1`] = `
<button
ant-click-animating-without-extra-node="true"

View File

@ -15,6 +15,7 @@ describe('Button', () => {
mountTest(Button.Group);
mountTest(() => <Button.Group size="large" />);
mountTest(() => <Button.Group size="small" />);
mountTest(() => <Button.Group size="middle" />);
rtlTest(Button);
rtlTest(() => <Button size="large" />);
@ -22,6 +23,7 @@ describe('Button', () => {
rtlTest(Button.Group);
rtlTest(() => <Button.Group size="large" />);
rtlTest(() => <Button.Group size="small" />);
rtlTest(() => <Button.Group size="middle" />);
it('renders correctly', () => {
const wrapper = render(<Button>Follow</Button>);
@ -32,6 +34,16 @@ describe('Button', () => {
expect(() => renderer.create(<Button>Follow</Button>)).not.toThrow();
});
it('warns if size is wrong', () => {
const mockWarn = jest.fn();
jest.spyOn(console, 'warn').mockImplementation(mockWarn);
render(<Button.Group size="who am I" />);
expect(mockWarn).toHaveBeenCalledTimes(1);
expect(mockWarn.mock.calls[0][0]).toMatchObject({
message: 'unreachable case: "who am I"',
});
});
it('renders Chinese characters correctly', () => {
const wrapper = render(<Button>按钮</Button>);
expect(wrapper).toMatchSnapshot();
@ -189,10 +201,7 @@ describe('Button', () => {
it('should has click wave effect', async () => {
const wrapper = mount(<Button type="primary">button</Button>);
wrapper
.find('.ant-btn')
.getDOMNode()
.click();
wrapper.find('.ant-btn').getDOMNode().click();
await new Promise(resolve => setTimeout(resolve, 0));
expect(wrapper.render()).toMatchSnapshot();
});
@ -265,9 +274,6 @@ describe('Button', () => {
throw new Error('Should not called!!!');
},
});
wrapper
.find('Button')
.instance()
.forceUpdate();
wrapper.find('Button').instance().forceUpdate();
});
});

View File

@ -2,6 +2,7 @@ import * as React from 'react';
import classNames from 'classnames';
import { SizeType } from '../config-provider/SizeContext';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import UnreachableException from '../_util/unreachableException';
export interface ButtonGroupProps {
size?: SizeType;
@ -26,8 +27,11 @@ const ButtonGroup: React.FC<ButtonGroupProps> = props => (
case 'small':
sizeCls = 'sm';
break;
default:
case 'middle':
case undefined:
break;
default:
console.warn(new UnreachableException(size));
}
const classes = classNames(