mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 02:59:58 +08:00
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:
parent
d634184b58
commit
316b925f94
8
components/_util/__tests__/unreachableException.test.js
Normal file
8
components/_util/__tests__/unreachableException.test.js
Normal 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\\""`);
|
||||
});
|
||||
});
|
5
components/_util/unreachableException.ts
Normal file
5
components/_util/unreachableException.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export default class UnreachableException {
|
||||
constructor(value: never) {
|
||||
return new Error(`unreachable case: ${JSON.stringify(value)}`);
|
||||
}
|
||||
}
|
@ -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"
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user