import React, { useState } from 'react';
import { render, fireEvent } from '../../../tests/utils';
import Space from '..';
import ConfigProvider from '../../config-provider';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
describe('Space', () => {
mountTest(Space);
rtlTest(Space);
it('should render width empty children', () => {
const { container } = render();
expect(container.children.length).toBe(0);
});
it('should render width ConfigProvider', () => {
const { container } = render(
1
2
1
2
1
2
,
);
expect(container.children).toMatchSnapshot();
});
it('should render width rtl', () => {
const { container } = render(
1
2
1
2
1
2
,
);
expect(container.children).toMatchSnapshot();
});
it('should render width customize size', () => {
const { container } = render(
1
2
,
);
expect(container.querySelector('div.ant-space-item').style.marginRight).toBe('10px');
expect(container.querySelectorAll('div.ant-space-item')[1].style.marginRight).toBe('');
});
it('should render width size 0', () => {
const { container } = render(
1
2
,
);
expect(container.querySelector('div.ant-space-item').style.marginRight).toBe('0px');
});
it('should render vertical space width customize size', () => {
const { container } = render(
1
2
,
);
expect(container.querySelector('div.ant-space-item').style.marginBottom).toBe('10px');
expect(container.querySelectorAll('div.ant-space-item')[1].style.marginBottom).toBe('');
});
it('should render correct with children', () => {
const { container } = render(
text1text1
{/* eslint-disable-next-line react/jsx-no-useless-fragment */}
<>text3>
,
);
expect(container.children[0]).toMatchSnapshot();
});
it('should render with invalidElement', () => {
const { container } = render(
text1text1
text1
,
);
expect(container.querySelectorAll('div.ant-space-item').length).toBe(3);
});
it('should be keep store', () => {
function Demo() {
const [state, setState] = React.useState(1);
return (
{
setState(value => value + 1);
}}
>
{state}
);
}
function SpaceDemo() {
const [visible, setVisible] = useState(true);
function onChange() {
setVisible(!visible);
}
return (
{visible && space
}
Three
);
}
const { container } = render();
expect(container.querySelector('#demo')).toHaveTextContent('1');
fireEvent.click(container.querySelector('#demo'));
expect(container.querySelector('#demo')).toHaveTextContent('2');
fireEvent.click(container.querySelector('p'));
expect(container.querySelector('#demo')).toHaveTextContent('2');
});
it('split', () => {
const { container } = render(
text1text1
{/* eslint-disable-next-line react/jsx-no-useless-fragment */}
<>text3>
,
);
expect(container.children[0]).toMatchSnapshot();
});
// https://github.com/ant-design/ant-design/issues/35305
it('should not throw duplicated key warning', () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined);
render(
,
);
// eslint-disable-next-line no-console
expect(console.error).not.toHaveBeenCalledWith(
expect.stringContaining('Encountered two children with the same key'),
expect.anything(),
expect.anything(),
);
// eslint-disable-next-line no-console
console.error.mockRestore();
});
});