2023-01-07 21:15:59 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2019-08-05 11:27:32 +08:00
|
|
|
import ConfigProvider from '..';
|
2023-06-07 11:54:50 +08:00
|
|
|
import { act, fireEvent, render } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import DatePicker from '../../date-picker';
|
2023-06-07 11:54:50 +08:00
|
|
|
import { closePicker, openPicker, selectCell } from '../../date-picker/__tests__/utils';
|
2022-12-09 15:04:08 +08:00
|
|
|
import type { Locale } from '../../locale';
|
|
|
|
import LocaleProvider from '../../locale';
|
2019-09-09 11:44:54 +08:00
|
|
|
import enUS from '../../locale/en_US';
|
2022-06-22 14:57:09 +08:00
|
|
|
import zhCN from '../../locale/zh_CN';
|
2019-09-09 11:44:54 +08:00
|
|
|
import Modal from '../../modal';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Pagination from '../../pagination';
|
|
|
|
import TimePicker from '../../time-picker';
|
2019-08-05 11:27:32 +08:00
|
|
|
|
|
|
|
describe('ConfigProvider.Locale', () => {
|
2022-10-06 18:53:06 +08:00
|
|
|
function $$(selector: string): NodeListOf<Element> {
|
|
|
|
return document.body.querySelectorAll(selector);
|
2019-09-09 11:44:54 +08:00
|
|
|
}
|
|
|
|
|
2019-08-05 11:27:32 +08:00
|
|
|
it('not throw', () => {
|
2022-08-18 17:40:47 +08:00
|
|
|
render(
|
|
|
|
<ConfigProvider locale={{} as Locale}>
|
2019-08-05 11:27:32 +08:00
|
|
|
<span />
|
|
|
|
<span />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
});
|
2019-08-06 00:27:47 +08:00
|
|
|
|
2019-09-09 11:44:54 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/18731
|
|
|
|
it('should not reset locale for Modal', () => {
|
2023-01-07 21:15:59 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [showButton, setShowButton] = useState<boolean>(false);
|
|
|
|
useEffect(() => {
|
|
|
|
setShowButton(true);
|
|
|
|
}, []);
|
|
|
|
const openConfirm = () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-08-18 17:40:47 +08:00
|
|
|
Modal.confirm({ title: 'title', content: 'Some descriptions' });
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-08-08 13:31:55 +08:00
|
|
|
});
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2019-09-09 11:44:54 +08:00
|
|
|
};
|
2023-01-07 21:15:59 +08:00
|
|
|
return (
|
|
|
|
<ConfigProvider locale={zhCN}>
|
|
|
|
{showButton ? (
|
|
|
|
<ConfigProvider locale={enUS}>
|
|
|
|
<button type="button" onClick={openConfirm}>
|
|
|
|
open
|
|
|
|
</button>
|
|
|
|
</ConfigProvider>
|
|
|
|
) : null}
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
|
|
|
};
|
2022-08-18 17:40:47 +08:00
|
|
|
const wrapper = render(<App />);
|
|
|
|
fireEvent.click(wrapper.container.querySelector('button')!);
|
2019-09-09 11:44:54 +08:00
|
|
|
expect($$('.ant-btn-primary')[0].textContent).toBe('OK');
|
|
|
|
});
|
|
|
|
|
2021-08-04 13:45:35 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/31592
|
|
|
|
it('should not reset the component state when switching locale', () => {
|
2022-08-18 17:40:47 +08:00
|
|
|
const wrapper = render(
|
2022-04-18 21:02:11 +08:00
|
|
|
<ConfigProvider locale={zhCN}>
|
|
|
|
<DatePicker />
|
|
|
|
<Pagination total={50} />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2021-08-04 13:45:35 +08:00
|
|
|
|
2022-08-18 17:40:47 +08:00
|
|
|
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(
|
2021-08-04 13:45:35 +08:00
|
|
|
'ant-pagination-item-active',
|
|
|
|
);
|
|
|
|
|
|
|
|
openPicker(wrapper);
|
|
|
|
selectCell(wrapper, 10);
|
|
|
|
closePicker(wrapper);
|
|
|
|
|
2022-08-18 17:40:47 +08:00
|
|
|
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')!);
|
2021-08-04 13:45:35 +08:00
|
|
|
|
2022-08-18 17:40:47 +08:00
|
|
|
const datepicker = wrapper.container.querySelector<HTMLInputElement>('.ant-picker-input input');
|
2021-08-04 13:45:35 +08:00
|
|
|
|
2022-08-18 17:40:47 +08:00
|
|
|
expect(datepicker?.placeholder).not.toBe('请选择日期');
|
|
|
|
expect(datepicker?.value).not.toBe('');
|
|
|
|
expect(datepicker?.value).toContain('-10');
|
2021-08-04 13:45:35 +08:00
|
|
|
|
2022-08-18 17:40:47 +08:00
|
|
|
expect(wrapper.container.querySelector('.ant-pagination-item-3')?.className).toContain(
|
2021-08-04 13:45:35 +08:00
|
|
|
'ant-pagination-item-active',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-08-06 00:27:47 +08:00
|
|
|
describe('support legacy LocaleProvider', () => {
|
2022-08-18 17:40:47 +08:00
|
|
|
function testLocale(wrapper: ReturnType<typeof render>): void {
|
|
|
|
expect(wrapper.container.querySelector('input')?.placeholder).toBe(
|
|
|
|
zhCN.TimePicker?.placeholder,
|
|
|
|
);
|
2019-08-06 00:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it('LocaleProvider', () => {
|
2022-08-18 17:40:47 +08:00
|
|
|
testLocale(
|
|
|
|
render(
|
|
|
|
<LocaleProvider locale={zhCN}>
|
|
|
|
<TimePicker />
|
|
|
|
</LocaleProvider>,
|
|
|
|
),
|
2019-08-06 00:27:47 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('LocaleProvider > ConfigProvider', () => {
|
2022-08-18 17:40:47 +08:00
|
|
|
testLocale(
|
|
|
|
render(
|
|
|
|
<LocaleProvider locale={zhCN}>
|
|
|
|
<ConfigProvider>
|
|
|
|
<TimePicker />
|
|
|
|
</ConfigProvider>
|
|
|
|
</LocaleProvider>,
|
|
|
|
),
|
2019-08-06 00:27:47 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ConfigProvider > ConfigProvider', () => {
|
2022-08-18 17:40:47 +08:00
|
|
|
testLocale(
|
|
|
|
render(
|
|
|
|
<ConfigProvider locale={zhCN}>
|
|
|
|
<ConfigProvider>
|
|
|
|
<TimePicker />
|
|
|
|
</ConfigProvider>
|
|
|
|
</ConfigProvider>,
|
|
|
|
),
|
2019-08-06 00:27:47 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-08-05 11:27:32 +08:00
|
|
|
});
|