2022-09-05 19:41:32 +08:00
|
|
|
/* eslint-disable no-console */
|
2020-08-15 16:29:47 +08:00
|
|
|
import React, { useState } from 'react';
|
2023-09-04 19:59:02 +08:00
|
|
|
|
2020-03-22 11:38:02 +08:00
|
|
|
import Space from '..';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2021-04-12 11:20:57 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
|
|
|
import ConfigProvider from '../../config-provider';
|
2020-03-22 11:38:02 +08:00
|
|
|
|
|
|
|
describe('Space', () => {
|
|
|
|
mountTest(Space);
|
2021-04-12 11:20:57 +08:00
|
|
|
rtlTest(Space);
|
2020-03-22 11:38:02 +08:00
|
|
|
|
|
|
|
it('should render width empty children', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(<Space />);
|
2020-03-22 11:38:02 +08:00
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.children.length).toBe(0);
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render width ConfigProvider', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2020-03-22 11:38:02 +08:00
|
|
|
<ConfigProvider space={{ size: 'large' }}>
|
|
|
|
<Space>
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>
|
|
|
|
<Space size="middle">
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>
|
|
|
|
<Space size="large">
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.children).toMatchSnapshot();
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|
|
|
|
|
2024-06-04 17:56:54 +08:00
|
|
|
it('should render width ConfigProvider support 0', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider space={{ size: 0 }}>
|
|
|
|
<Space>
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const item = container.querySelector('.ant-space-gap-row-small.ant-space-gap-col-small');
|
|
|
|
expect(item).toBe(null);
|
|
|
|
});
|
|
|
|
|
2021-04-30 14:03:05 +08:00
|
|
|
it('should render width rtl', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2021-04-30 14:03:05 +08:00
|
|
|
<ConfigProvider direction="rtl">
|
|
|
|
<Space>
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>
|
|
|
|
<Space size="middle">
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>
|
|
|
|
<Space size="large">
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.children).toMatchSnapshot();
|
2021-04-30 14:03:05 +08:00
|
|
|
});
|
|
|
|
|
2020-03-22 11:38:02 +08:00
|
|
|
it('should render width customize size', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2020-03-22 11:38:02 +08:00
|
|
|
<Space size={10}>
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
2023-09-04 19:59:02 +08:00
|
|
|
const items = container.querySelectorAll<HTMLDivElement>('div.ant-space-item');
|
2023-09-05 10:17:56 +08:00
|
|
|
expect(items[0]?.style.marginRight).toBe('');
|
2023-09-04 19:59:02 +08:00
|
|
|
expect(items[1]?.style.marginRight).toBe('');
|
2021-04-12 11:20:57 +08:00
|
|
|
});
|
|
|
|
|
2020-03-22 11:38:02 +08:00
|
|
|
it('should render vertical space width customize size', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2020-03-22 11:38:02 +08:00
|
|
|
<Space size={10} direction="vertical">
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
2023-09-04 19:59:02 +08:00
|
|
|
const items = container.querySelectorAll<HTMLDivElement>('div.ant-space-item');
|
2023-09-05 10:17:56 +08:00
|
|
|
expect(items[0]?.style.marginBottom).toBe('');
|
2023-09-04 19:59:02 +08:00
|
|
|
expect(items[1]?.style.marginBottom).toBe('');
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render correct with children', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2020-03-22 11:38:02 +08:00
|
|
|
<Space>
|
|
|
|
text1<span>text1</span>
|
2021-11-26 12:18:21 +08:00
|
|
|
{/* eslint-disable-next-line react/jsx-no-useless-fragment */}
|
2020-03-22 11:38:02 +08:00
|
|
|
<>text3</>
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.children[0]).toMatchSnapshot();
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render with invalidElement', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2020-03-22 11:38:02 +08:00
|
|
|
<Space>
|
|
|
|
text1<span>text1</span>
|
|
|
|
text1
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.querySelectorAll('div.ant-space-item').length).toBe(3);
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|
2020-08-15 16:29:47 +08:00
|
|
|
|
|
|
|
it('should be keep store', () => {
|
|
|
|
function Demo() {
|
|
|
|
const [state, setState] = React.useState(1);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
id="demo"
|
|
|
|
onClick={() => {
|
2022-11-19 13:47:33 +08:00
|
|
|
setState((value) => value + 1);
|
2020-08-15 16:29:47 +08:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{state}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
function SpaceDemo() {
|
|
|
|
const [visible, setVisible] = useState(true);
|
|
|
|
function onChange() {
|
|
|
|
setVisible(!visible);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Space>
|
|
|
|
{visible && <div>space</div>}
|
|
|
|
<Demo />
|
|
|
|
<p onClick={onChange}>Three</p>
|
|
|
|
</Space>
|
|
|
|
);
|
|
|
|
}
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(<SpaceDemo />);
|
2020-08-15 16:29:47 +08:00
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.querySelector('#demo')).toHaveTextContent('1');
|
2020-08-15 16:29:47 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('#demo')!);
|
2020-08-15 16:29:47 +08:00
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.querySelector('#demo')).toHaveTextContent('2');
|
2020-08-15 16:29:47 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('p')!);
|
2020-08-15 16:29:47 +08:00
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.querySelector('#demo')).toHaveTextContent('2');
|
2020-08-15 16:29:47 +08:00
|
|
|
});
|
2020-10-08 18:41:40 +08:00
|
|
|
|
|
|
|
it('split', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2020-10-08 18:41:40 +08:00
|
|
|
<Space split="-">
|
|
|
|
text1<span>text1</span>
|
2021-11-26 12:18:21 +08:00
|
|
|
{/* eslint-disable-next-line react/jsx-no-useless-fragment */}
|
2020-10-08 18:41:40 +08:00
|
|
|
<>text3</>
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
2022-05-04 20:44:37 +08:00
|
|
|
expect(container.children[0]).toMatchSnapshot();
|
2020-10-08 18:41:40 +08:00
|
|
|
});
|
2022-04-28 22:23:06 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/35305
|
|
|
|
it('should not throw duplicated key warning', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-05-04 20:44:37 +08:00
|
|
|
render(
|
2022-04-28 22:23:06 +08:00
|
|
|
<Space>
|
|
|
|
<div key="1" />
|
|
|
|
<div />
|
|
|
|
<div key="3" />
|
|
|
|
<div />
|
|
|
|
</Space>,
|
|
|
|
);
|
2022-11-28 14:19:25 +08:00
|
|
|
expect(spy).not.toHaveBeenCalledWith(
|
2022-04-28 22:23:06 +08:00
|
|
|
expect.stringContaining('Encountered two children with the same key'),
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
2022-11-28 14:19:25 +08:00
|
|
|
spy.mockRestore();
|
2022-04-28 22:23:06 +08:00
|
|
|
});
|
2023-02-05 19:58:29 +08:00
|
|
|
|
|
|
|
it('should render the hidden empty item wrapper', () => {
|
2023-09-04 19:59:02 +08:00
|
|
|
const Null: React.FC = () => null;
|
2023-02-05 19:58:29 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Space>
|
|
|
|
<Null />
|
|
|
|
</Space>,
|
|
|
|
);
|
2023-09-04 19:59:02 +08:00
|
|
|
const element = container.querySelector<HTMLDivElement>('div.ant-space-item')!;
|
|
|
|
expect(element).toBeEmptyDOMElement();
|
|
|
|
expect(getComputedStyle(element).display).toBe('none');
|
2023-02-05 19:58:29 +08:00
|
|
|
});
|
2023-05-11 12:00:02 +08:00
|
|
|
|
|
|
|
it('should ref work', () => {
|
|
|
|
const ref = React.createRef<HTMLDivElement>();
|
|
|
|
const { container } = render(
|
|
|
|
<Space ref={ref}>
|
|
|
|
<span>Text1</span>
|
|
|
|
<span>Text2</span>
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(ref.current).toBe(container.firstChild);
|
|
|
|
});
|
2023-05-31 13:43:38 +08:00
|
|
|
|
|
|
|
it('should classNames work', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Space classNames={{ item: 'test-classNames' }}>
|
|
|
|
<span>Text1</span>
|
|
|
|
<span>Text2</span>
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
2023-09-04 19:59:02 +08:00
|
|
|
expect(container.querySelector<HTMLDivElement>('.ant-space-item.test-classNames')).toBeTruthy();
|
2023-05-31 13:43:38 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should styles work', () => {
|
|
|
|
const { container } = render(
|
2023-09-04 19:59:02 +08:00
|
|
|
<Space styles={{ item: { color: 'red' } }}>
|
2023-05-31 13:43:38 +08:00
|
|
|
<span>Text1</span>
|
|
|
|
<span>Text2</span>
|
|
|
|
</Space>,
|
|
|
|
);
|
2023-09-04 19:59:02 +08:00
|
|
|
expect(
|
|
|
|
container.querySelector<HTMLDivElement>('.ant-space-item')?.getAttribute('style'),
|
2023-09-05 10:17:56 +08:00
|
|
|
).toEqual('color: red;');
|
2023-05-31 13:43:38 +08:00
|
|
|
});
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|