2022-06-22 14:57:09 +08:00
|
|
|
import React, { useState } from 'react';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { SmileOutlined } from '@ant-design/icons';
|
|
|
|
|
2023-09-12 12:18:54 +08:00
|
|
|
import type { ConfigConsumerProps, RenderEmptyHandler } from '..';
|
2020-06-29 14:52:46 +08:00
|
|
|
import ConfigProvider, { ConfigContext } from '..';
|
2022-06-22 14:57:09 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2022-12-28 23:20:22 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2018-12-05 19:12:18 +08:00
|
|
|
import Button from '../../button';
|
2020-04-22 10:38:43 +08:00
|
|
|
import Input from '../../input';
|
2022-12-14 10:22:17 +08:00
|
|
|
import Select from '../../select';
|
2023-06-02 15:03:29 +08:00
|
|
|
import Table from '../../table';
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
describe('ConfigProvider', () => {
|
2019-09-02 10:47:32 +08:00
|
|
|
mountTest(() => (
|
|
|
|
<ConfigProvider>
|
|
|
|
<div />
|
|
|
|
</ConfigProvider>
|
|
|
|
));
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2019-01-10 11:47:11 +08:00
|
|
|
it('autoInsertSpaceInButton', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const text = '确定';
|
|
|
|
const { container } = render(
|
2019-01-10 11:47:11 +08:00
|
|
|
<ConfigProvider autoInsertSpaceInButton={false}>
|
2022-08-22 22:54:38 +08:00
|
|
|
<Button>{text}</Button>
|
2019-01-10 11:47:11 +08:00
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('span')?.innerHTML).toBe(text);
|
2018-12-05 19:12:18 +08:00
|
|
|
});
|
2020-02-14 19:02:53 +08:00
|
|
|
|
|
|
|
it('renderEmpty', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const text = 'empty placeholder';
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider renderEmpty={() => <div>{text}</div>}>
|
2020-02-14 19:02:53 +08:00
|
|
|
<Table />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('.ant-table-placeholder')?.querySelector('div')?.innerHTML).toBe(
|
|
|
|
text,
|
|
|
|
);
|
2020-02-14 19:02:53 +08:00
|
|
|
});
|
2020-04-21 11:16:33 +08:00
|
|
|
|
|
|
|
it('nest prefixCls', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const { container } = render(
|
2020-04-21 11:16:33 +08:00
|
|
|
<ConfigProvider prefixCls="bamboo">
|
|
|
|
<ConfigProvider>
|
|
|
|
<Button />
|
|
|
|
</ConfigProvider>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('button.bamboo-btn')).toBeTruthy();
|
2020-04-21 11:16:33 +08:00
|
|
|
});
|
2020-04-22 10:38:43 +08:00
|
|
|
|
2021-05-24 11:46:12 +08:00
|
|
|
it('dynamic prefixCls', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const DynamicPrefixCls: React.FC = () => {
|
2021-05-24 11:46:12 +08:00
|
|
|
const [prefixCls, setPrefixCls] = useState('bamboo');
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Button onClick={() => setPrefixCls('light')} className="toggle-button">
|
|
|
|
toggle
|
|
|
|
</Button>
|
|
|
|
<ConfigProvider prefixCls={prefixCls}>
|
|
|
|
<ConfigProvider>
|
|
|
|
<Button />
|
|
|
|
</ConfigProvider>
|
|
|
|
</ConfigProvider>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-17 22:36:36 +08:00
|
|
|
const { container } = render(<DynamicPrefixCls />);
|
2021-05-24 11:46:12 +08:00
|
|
|
|
2022-04-17 22:36:36 +08:00
|
|
|
expect(container.querySelector('button.bamboo-btn')).toBeTruthy();
|
2022-08-22 22:54:38 +08:00
|
|
|
fireEvent.click(container.querySelector('.toggle-button')!);
|
2022-04-17 22:36:36 +08:00
|
|
|
expect(container.querySelector('button.light-btn')).toBeTruthy();
|
2021-05-24 11:46:12 +08:00
|
|
|
});
|
|
|
|
|
2021-01-19 17:33:05 +08:00
|
|
|
it('iconPrefixCls', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const { container } = render(
|
2021-01-19 17:33:05 +08:00
|
|
|
<ConfigProvider iconPrefixCls="bamboo">
|
|
|
|
<SmileOutlined />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('[role="img"]')).toHaveClass('bamboo');
|
|
|
|
expect(container.querySelector('[role="img"]')).toHaveClass('bamboo-smile');
|
2021-01-19 17:33:05 +08:00
|
|
|
});
|
|
|
|
|
2020-04-22 10:38:43 +08:00
|
|
|
it('input autoComplete', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const { container } = render(
|
2020-04-22 10:38:43 +08:00
|
|
|
<ConfigProvider input={{ autoComplete: 'off' }}>
|
|
|
|
<Input />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('input')?.autocomplete).toEqual('off');
|
2020-04-22 10:38:43 +08:00
|
|
|
});
|
2020-06-29 14:52:46 +08:00
|
|
|
|
2022-12-14 10:22:17 +08:00
|
|
|
it('select showSearch', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider select={{ showSearch: true }}>
|
|
|
|
<Select />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-select-show-search').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2020-06-29 14:52:46 +08:00
|
|
|
it('render empty', () => {
|
2022-05-16 16:34:42 +08:00
|
|
|
let rendered = false;
|
2023-09-12 12:18:54 +08:00
|
|
|
let cacheRenderEmpty: RenderEmptyHandler | undefined;
|
2022-05-16 16:34:42 +08:00
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const { renderEmpty } = React.useContext<ConfigConsumerProps>(ConfigContext);
|
2022-05-16 16:34:42 +08:00
|
|
|
rendered = true;
|
|
|
|
cacheRenderEmpty = renderEmpty;
|
|
|
|
return null;
|
2020-07-01 10:11:39 +08:00
|
|
|
};
|
2022-05-16 16:34:42 +08:00
|
|
|
|
|
|
|
render(
|
2020-06-29 14:52:46 +08:00
|
|
|
<ConfigProvider>
|
|
|
|
<App />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
2022-05-16 16:34:42 +08:00
|
|
|
expect(rendered).toBeTruthy();
|
|
|
|
expect(cacheRenderEmpty).toBeFalsy();
|
2020-06-29 14:52:46 +08:00
|
|
|
});
|
2018-12-05 19:12:18 +08:00
|
|
|
});
|