You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.7 KiB
137 lines
3.7 KiB
4 years ago
|
import { SmileOutlined } from '@ant-design/icons';
|
||
2 years ago
|
import React, { useState } from 'react';
|
||
2 years ago
|
import type { ConfigConsumerProps } from '..';
|
||
4 years ago
|
import ConfigProvider, { ConfigContext } from '..';
|
||
2 years ago
|
import mountTest from '../../../tests/shared/mountTest';
|
||
2 years ago
|
import { act, fireEvent, render } from '../../../tests/utils';
|
||
6 years ago
|
import Button from '../../button';
|
||
5 years ago
|
import Input from '../../input';
|
||
2 years ago
|
import Table from '../../table';
|
||
6 years ago
|
|
||
|
describe('ConfigProvider', () => {
|
||
5 years ago
|
mountTest(() => (
|
||
|
<ConfigProvider>
|
||
|
<div />
|
||
|
</ConfigProvider>
|
||
|
));
|
||
5 years ago
|
|
||
6 years ago
|
it('Content Security Policy', () => {
|
||
2 years ago
|
jest.useFakeTimers();
|
||
|
|
||
6 years ago
|
const csp = { nonce: 'test-antd' };
|
||
2 years ago
|
const { container } = render(
|
||
6 years ago
|
<ConfigProvider csp={csp}>
|
||
|
<Button />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('button')!);
|
||
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
|
const styles = Array.from(document.body.querySelectorAll<HTMLStyleElement>('style'));
|
||
|
expect(styles[styles.length - 1].nonce).toEqual(csp.nonce);
|
||
|
|
||
|
jest.useRealTimers();
|
||
6 years ago
|
});
|
||
6 years ago
|
|
||
6 years ago
|
it('autoInsertSpaceInButton', () => {
|
||
2 years ago
|
const text = '确定';
|
||
|
const { container } = render(
|
||
6 years ago
|
<ConfigProvider autoInsertSpaceInButton={false}>
|
||
2 years ago
|
<Button>{text}</Button>
|
||
6 years ago
|
</ConfigProvider>,
|
||
|
);
|
||
2 years ago
|
expect(container.querySelector('span')?.innerHTML).toBe(text);
|
||
6 years ago
|
});
|
||
5 years ago
|
|
||
|
it('renderEmpty', () => {
|
||
2 years ago
|
const text = 'empty placeholder';
|
||
|
const { container } = render(
|
||
|
<ConfigProvider renderEmpty={() => <div>{text}</div>}>
|
||
5 years ago
|
<Table />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
2 years ago
|
expect(container.querySelector('.ant-table-placeholder')?.querySelector('div')?.innerHTML).toBe(
|
||
|
text,
|
||
|
);
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
it('nest prefixCls', () => {
|
||
2 years ago
|
const { container } = render(
|
||
5 years ago
|
<ConfigProvider prefixCls="bamboo">
|
||
|
<ConfigProvider>
|
||
|
<Button />
|
||
|
</ConfigProvider>
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
|
|
||
2 years ago
|
expect(container.querySelector('button.bamboo-btn')).toBeTruthy();
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
it('dynamic prefixCls', () => {
|
||
2 years ago
|
const DynamicPrefixCls: React.FC = () => {
|
||
4 years ago
|
const [prefixCls, setPrefixCls] = useState('bamboo');
|
||
|
return (
|
||
|
<div>
|
||
|
<Button onClick={() => setPrefixCls('light')} className="toggle-button">
|
||
|
toggle
|
||
|
</Button>
|
||
|
<ConfigProvider prefixCls={prefixCls}>
|
||
|
<ConfigProvider>
|
||
|
<Button />
|
||
|
</ConfigProvider>
|
||
|
</ConfigProvider>
|
||
|
</div>
|
||
|
);
|
||
|
};
|
||
|
|
||
3 years ago
|
const { container } = render(<DynamicPrefixCls />);
|
||
4 years ago
|
|
||
3 years ago
|
expect(container.querySelector('button.bamboo-btn')).toBeTruthy();
|
||
2 years ago
|
fireEvent.click(container.querySelector('.toggle-button')!);
|
||
3 years ago
|
expect(container.querySelector('button.light-btn')).toBeTruthy();
|
||
4 years ago
|
});
|
||
|
|
||
4 years ago
|
it('iconPrefixCls', () => {
|
||
2 years ago
|
const { container } = render(
|
||
4 years ago
|
<ConfigProvider iconPrefixCls="bamboo">
|
||
|
<SmileOutlined />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
|
|
||
2 years ago
|
expect(container.querySelector('[role="img"]')).toHaveClass('bamboo');
|
||
|
expect(container.querySelector('[role="img"]')).toHaveClass('bamboo-smile');
|
||
4 years ago
|
});
|
||
|
|
||
5 years ago
|
it('input autoComplete', () => {
|
||
2 years ago
|
const { container } = render(
|
||
5 years ago
|
<ConfigProvider input={{ autoComplete: 'off' }}>
|
||
|
<Input />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
2 years ago
|
expect(container.querySelector('input')?.autocomplete).toEqual('off');
|
||
5 years ago
|
});
|
||
4 years ago
|
|
||
|
it('render empty', () => {
|
||
3 years ago
|
let rendered = false;
|
||
|
let cacheRenderEmpty;
|
||
|
|
||
2 years ago
|
const App: React.FC = () => {
|
||
|
const { renderEmpty } = React.useContext<ConfigConsumerProps>(ConfigContext);
|
||
3 years ago
|
rendered = true;
|
||
|
cacheRenderEmpty = renderEmpty;
|
||
|
return null;
|
||
4 years ago
|
};
|
||
3 years ago
|
|
||
|
render(
|
||
4 years ago
|
<ConfigProvider>
|
||
|
<App />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
|
|
||
3 years ago
|
expect(rendered).toBeTruthy();
|
||
|
expect(cacheRenderEmpty).toBeFalsy();
|
||
4 years ago
|
});
|
||
6 years ago
|
});
|