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';
|
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
|
|
|
});
|
|
|
|
|
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>,
|
|
|
|
);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(container.querySelector<HTMLDivElement>('div.ant-space-item')?.style.marginRight).toBe(
|
|
|
|
'10px',
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
container.querySelectorAll<HTMLDivElement>('div.ant-space-item')[1]?.style.marginRight,
|
|
|
|
).toBe('');
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|
|
|
|
|
2021-04-12 11:20:57 +08:00
|
|
|
it('should render width size 0', () => {
|
2022-05-04 20:44:37 +08:00
|
|
|
const { container } = render(
|
2021-04-12 11:20:57 +08:00
|
|
|
<Space size={NaN}>
|
|
|
|
<span>1</span>
|
|
|
|
<span>2</span>
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(container.querySelector<HTMLDivElement>('div.ant-space-item')?.style.marginRight).toBe(
|
|
|
|
'0px',
|
|
|
|
);
|
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>,
|
|
|
|
);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(container.querySelector<HTMLDivElement>('div.ant-space-item')?.style.marginBottom).toBe(
|
|
|
|
'10px',
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
container.querySelectorAll<HTMLDivElement>('div.ant-space-item')[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', () => {
|
2022-11-28 14:19:25 +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', () => {
|
|
|
|
const Null = () => null;
|
|
|
|
const { container } = render(
|
|
|
|
<Space>
|
|
|
|
<Null />
|
|
|
|
</Space>,
|
|
|
|
);
|
|
|
|
const item = container.querySelector('div.ant-space-item') as HTMLElement;
|
|
|
|
|
|
|
|
expect(item).toBeEmptyDOMElement();
|
|
|
|
expect(getComputedStyle(item).display).toBe('none');
|
|
|
|
});
|
2020-03-22 11:38:02 +08:00
|
|
|
});
|