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.
152 lines
4.3 KiB
152 lines
4.3 KiB
2 years ago
|
import React from 'react';
|
||
2 years ago
|
import { closePicker, openPicker, selectCell } from '../../date-picker/__tests__/utils';
|
||
5 years ago
|
import ConfigProvider from '..';
|
||
2 years ago
|
import DatePicker from '../../date-picker';
|
||
2 years ago
|
import type { Locale } from '../../locale-provider';
|
||
5 years ago
|
import LocaleProvider from '../../locale-provider';
|
||
5 years ago
|
import enUS from '../../locale/en_US';
|
||
2 years ago
|
import zhCN from '../../locale/zh_CN';
|
||
5 years ago
|
import Modal from '../../modal';
|
||
2 years ago
|
import Pagination from '../../pagination';
|
||
|
import TimePicker from '../../time-picker';
|
||
2 years ago
|
import { act, render, fireEvent } from '../../../tests/utils';
|
||
5 years ago
|
|
||
|
describe('ConfigProvider.Locale', () => {
|
||
2 years ago
|
function $$(className: string): NodeListOf<Element> {
|
||
5 years ago
|
return document.body.querySelectorAll(className);
|
||
|
}
|
||
|
|
||
5 years ago
|
it('not throw', () => {
|
||
2 years ago
|
render(
|
||
|
<ConfigProvider locale={{} as Locale}>
|
||
5 years ago
|
<span />
|
||
|
<span />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
|
});
|
||
5 years ago
|
|
||
5 years ago
|
// https://github.com/ant-design/ant-design/issues/18731
|
||
|
it('should not reset locale for Modal', () => {
|
||
|
class App extends React.Component {
|
||
2 years ago
|
state = { showButton: false };
|
||
5 years ago
|
|
||
|
componentDidMount() {
|
||
2 years ago
|
this.setState({ showButton: true });
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
// eslint-disable-next-line class-methods-use-this
|
||
5 years ago
|
openConfirm = () => {
|
||
5 years ago
|
jest.useFakeTimers();
|
||
2 years ago
|
Modal.confirm({ title: 'title', content: 'Some descriptions' });
|
||
2 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
5 years ago
|
jest.useRealTimers();
|
||
5 years ago
|
};
|
||
|
|
||
|
render() {
|
||
|
return (
|
||
|
<ConfigProvider locale={zhCN}>
|
||
|
{this.state.showButton ? (
|
||
|
<ConfigProvider locale={enUS}>
|
||
|
<button type="button" onClick={this.openConfirm}>
|
||
|
open
|
||
|
</button>
|
||
|
</ConfigProvider>
|
||
|
) : null}
|
||
|
</ConfigProvider>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const wrapper = render(<App />);
|
||
|
fireEvent.click(wrapper.container.querySelector('button')!);
|
||
5 years ago
|
expect($$('.ant-btn-primary')[0].textContent).toBe('OK');
|
||
|
});
|
||
|
|
||
3 years ago
|
// https://github.com/ant-design/ant-design/issues/31592
|
||
|
it('should not reset the component state when switching locale', () => {
|
||
2 years ago
|
const wrapper = render(
|
||
3 years ago
|
<ConfigProvider locale={zhCN}>
|
||
|
<DatePicker />
|
||
|
<Pagination total={50} />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
3 years ago
|
|
||
2 years ago
|
const datepicke = wrapper.container.querySelector<HTMLInputElement>('.ant-picker-input input');
|
||
|
expect(datepicke?.value).toBe('');
|
||
|
expect(datepicke?.placeholder).toBe('请选择日期');
|
||
|
expect(wrapper.container.querySelector('.ant-pagination-item-1')?.className).toContain(
|
||
3 years ago
|
'ant-pagination-item-active',
|
||
|
);
|
||
|
|
||
|
openPicker(wrapper);
|
||
|
selectCell(wrapper, 10);
|
||
|
closePicker(wrapper);
|
||
|
|
||
2 years ago
|
expect(
|
||
|
wrapper.container.querySelector<HTMLInputElement>('.ant-picker-input input')?.value,
|
||
|
).not.toBe('');
|
||
|
wrapper.rerender(
|
||
|
<ConfigProvider locale={{} as Locale}>
|
||
|
<DatePicker />
|
||
|
<Pagination total={50} />
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
|
|
||
|
fireEvent.click(wrapper.container.querySelector('.ant-pagination-item-3')!);
|
||
3 years ago
|
|
||
2 years ago
|
const datepicker = wrapper.container.querySelector<HTMLInputElement>('.ant-picker-input input');
|
||
3 years ago
|
|
||
2 years ago
|
expect(datepicker?.placeholder).not.toBe('请选择日期');
|
||
|
expect(datepicker?.value).not.toBe('');
|
||
|
expect(datepicker?.value).toContain('-10');
|
||
3 years ago
|
|
||
2 years ago
|
expect(wrapper.container.querySelector('.ant-pagination-item-3')?.className).toContain(
|
||
3 years ago
|
'ant-pagination-item-active',
|
||
|
);
|
||
|
});
|
||
|
|
||
5 years ago
|
describe('support legacy LocaleProvider', () => {
|
||
2 years ago
|
function testLocale(wrapper: ReturnType<typeof render>): void {
|
||
|
expect(wrapper.container.querySelector('input')?.placeholder).toBe(
|
||
|
zhCN.TimePicker?.placeholder,
|
||
|
);
|
||
5 years ago
|
}
|
||
|
|
||
|
it('LocaleProvider', () => {
|
||
2 years ago
|
testLocale(
|
||
|
render(
|
||
|
<LocaleProvider locale={zhCN}>
|
||
|
<TimePicker />
|
||
|
</LocaleProvider>,
|
||
|
),
|
||
5 years ago
|
);
|
||
|
});
|
||
|
|
||
|
it('LocaleProvider > ConfigProvider', () => {
|
||
2 years ago
|
testLocale(
|
||
|
render(
|
||
|
<LocaleProvider locale={zhCN}>
|
||
|
<ConfigProvider>
|
||
|
<TimePicker />
|
||
|
</ConfigProvider>
|
||
|
</LocaleProvider>,
|
||
|
),
|
||
5 years ago
|
);
|
||
|
});
|
||
|
|
||
|
it('ConfigProvider > ConfigProvider', () => {
|
||
2 years ago
|
testLocale(
|
||
|
render(
|
||
|
<ConfigProvider locale={zhCN}>
|
||
|
<ConfigProvider>
|
||
|
<TimePicker />
|
||
|
</ConfigProvider>
|
||
|
</ConfigProvider>,
|
||
|
),
|
||
5 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
});
|