2018-04-20 11:23:37 +08:00
|
|
|
import React from 'react';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { TimelineProps } from '..';
|
2018-04-20 11:23:37 +08:00
|
|
|
import TimeLine from '..';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-05-20 19:47:36 +08:00
|
|
|
import { render } from '../../../tests/utils';
|
2018-04-20 11:23:37 +08:00
|
|
|
|
|
|
|
const { Item } = TimeLine;
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
const renderFactory = (
|
|
|
|
timeLineProps: TimelineProps = {},
|
|
|
|
labelItems: TimelineProps['children'] = null,
|
|
|
|
) =>
|
2022-05-20 19:47:36 +08:00
|
|
|
render(
|
2022-09-05 19:41:32 +08:00
|
|
|
<TimeLine {...timeLineProps}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<Item key="1">foo</Item>
|
|
|
|
<Item key="2">bar</Item>
|
|
|
|
<Item key="3">baz</Item>
|
2020-02-26 17:21:44 +08:00
|
|
|
{labelItems}
|
2018-12-07 16:17:45 +08:00
|
|
|
</TimeLine>,
|
|
|
|
);
|
2018-04-20 11:23:37 +08:00
|
|
|
|
|
|
|
describe('TimeLine', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(TimeLine);
|
|
|
|
mountTest(TimeLine.Item);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(TimeLine);
|
|
|
|
rtlTest(TimeLine.Item);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2022-05-20 19:47:36 +08:00
|
|
|
it('renders items without passing any props correctly', () => {
|
|
|
|
const { container } = renderFactory();
|
2018-04-20 11:23:37 +08:00
|
|
|
|
2022-05-20 19:47:36 +08:00
|
|
|
// has 3 timeline item
|
|
|
|
expect(container.querySelectorAll('li.ant-timeline-item')).toHaveLength(3);
|
2018-04-20 11:23:37 +08:00
|
|
|
|
2022-05-20 19:47:36 +08:00
|
|
|
// has only 1 timeline item is marked as the last item
|
|
|
|
expect(container.querySelectorAll('li.ant-timeline-item-last')).toHaveLength(1);
|
2018-04-20 11:23:37 +08:00
|
|
|
|
2022-05-20 19:47:36 +08:00
|
|
|
// its last item is marked as the last item
|
|
|
|
expect(container.querySelectorAll('li.ant-timeline-item')[2]).toHaveClass(
|
|
|
|
'ant-timeline-item-last',
|
|
|
|
);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('renders pending item', () => {
|
|
|
|
const pending = <div>pending...</div>;
|
|
|
|
const pendingDot = <i>dot</i>;
|
|
|
|
|
|
|
|
it('has one extra timeline item', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ pending });
|
|
|
|
expect(container.querySelectorAll('li.ant-timeline-item')).toHaveLength(4);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('has extra pending timeline item', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ pending });
|
|
|
|
expect(container.querySelectorAll('li.ant-timeline-item-pending')).toHaveLength(1);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
it("renders the pending timeline item as long as it receive a truthy prop value to 'pending'", () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ pending: true });
|
|
|
|
expect(container.querySelector('li.ant-timeline-item-pending')).toBeTruthy();
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('its last item is marked as the pending item', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ pending });
|
|
|
|
const items = container.querySelectorAll('li.ant-timeline-item');
|
|
|
|
expect(items[items.length - 1]).toHaveClass('ant-timeline-item-pending');
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('its second to last item is marked as the last item', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ pending });
|
|
|
|
const items = container.querySelectorAll('li.ant-timeline-item');
|
|
|
|
expect(items[items.length - 2]).toHaveClass('ant-timeline-item-last');
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('has the correct pending node', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container, getByText } = renderFactory({ pending });
|
|
|
|
expect(container.querySelector('li.ant-timeline-item-pending')).toContainElement(
|
|
|
|
getByText('pending...'),
|
|
|
|
);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('has the correct pending dot node', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container, getByText } = renderFactory({ pending, pendingDot });
|
|
|
|
expect(container.querySelector('li.ant-timeline-item-pending')).toContainElement(
|
|
|
|
getByText('dot'),
|
|
|
|
);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
it("has no pending dot if without passing a truthy 'pending' prop", () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { queryByText } = renderFactory({ pendingDot });
|
|
|
|
expect(queryByText('dot')).toBeFalsy();
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('the item rendering sequence is controlled by reverse', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const getTextContents = (nodeList: NodeListOf<HTMLDivElement>) =>
|
|
|
|
Array.from(nodeList).map(node => node?.textContent);
|
2022-05-20 19:47:36 +08:00
|
|
|
|
2018-04-20 11:23:37 +08:00
|
|
|
it('items is in order when prop reverse is false', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ reverse: false });
|
|
|
|
const textContents = getTextContents(
|
|
|
|
container.querySelectorAll('.ant-timeline-item-content'),
|
|
|
|
);
|
|
|
|
expect(textContents).toEqual(['foo', 'bar', 'baz']);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('items is reversed when prop reverse is true', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ reverse: true });
|
|
|
|
const textContents = getTextContents(
|
|
|
|
container.querySelectorAll('.ant-timeline-item-content'),
|
|
|
|
);
|
|
|
|
expect(textContents).toEqual(['baz', 'bar', 'foo']);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('renders items reversely and with pending item', () => {
|
|
|
|
const pending = <div>pending...</div>;
|
|
|
|
|
|
|
|
it('its last item is marked as the last item', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ pending, reverse: true });
|
|
|
|
const items = container.querySelectorAll('li.ant-timeline-item');
|
|
|
|
expect(items[items.length - 1]).toHaveClass('ant-timeline-item-last');
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('its first item is marked as the pending item', () => {
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory({ pending, reverse: true });
|
|
|
|
expect(container.querySelector('li.ant-timeline-item')).toHaveClass(
|
|
|
|
'ant-timeline-item-pending',
|
|
|
|
);
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|
|
|
|
});
|
2020-02-26 17:21:44 +08:00
|
|
|
|
|
|
|
it('renders Timeline item with label correctly', () => {
|
|
|
|
const label = '2020-01-01';
|
2022-05-20 19:47:36 +08:00
|
|
|
const { container } = renderFactory(
|
2020-02-26 17:21:44 +08:00
|
|
|
{},
|
|
|
|
<Item key="1" label={label}>
|
|
|
|
foo
|
|
|
|
</Item>,
|
|
|
|
);
|
2022-05-20 19:47:36 +08:00
|
|
|
expect(container.querySelectorAll('.ant-timeline-label')).toHaveLength(1);
|
|
|
|
expect(container.querySelector('.ant-timeline-item-label')).toHaveTextContent(label);
|
2020-02-26 17:21:44 +08:00
|
|
|
});
|
2018-04-20 11:23:37 +08:00
|
|
|
});
|