Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(collapse): add collapse related tests #1280 #1361

Merged
merged 2 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/collapse/CollapsePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { useRef, useEffect } from 'react';
import classnames from 'classnames';
import { CSSTransition } from 'react-transition-group';
import { useCollapseContext } from './CollapseContext';
Expand Down Expand Up @@ -45,9 +45,16 @@ const CollapsePanel = (props: CollapsePanelProps) => {
const bodyRef = useRef<HTMLDivElement>();
const isDisabled = disabled || disableAll;

if (defaultExpandAll) {
updateCollapseValue(innerValue);
}
const initDefaultExpandAll = useRef(false);
useEffect(() => {
if (initDefaultExpandAll) {
return;
}
initDefaultExpandAll.current = true;
if (defaultExpandAll) {
updateCollapseValue(innerValue);
}
}, [updateCollapseValue, defaultExpandAll, innerValue]);

const isActive = Array.isArray(collapseValue) ? collapseValue.includes(innerValue) : collapseValue === innerValue;

Expand Down
36 changes: 32 additions & 4 deletions src/collapse/__tests__/collapse.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import { testExamples, render } from '@test/utils';
import Collapse from '../Collapse';
import CollapsePanal from '../CollapsePanel';
import { testExamples, render, act, waitFor, fireEvent } from '@test/utils';

import { Collapse } from '..';
import { TdCollapseProps, TdCollapsePanelProps } from '../type';

const { Panel } = Collapse;

// 测试组件代码 Example 快照
testExamples(__dirname);

Expand All @@ -15,14 +17,40 @@ const panelContent =
'这部分是每个折叠面板折叠或展开的内容,可根据不同业务或用户的使用诉求,进行自定义填充。可以是纯文本、图文、子列表等内容形式。';
const CollapseTestComponent = (props: TestComponentProps) => (
<Collapse {...props?.collapse}>
<CollapsePanal {...props?.panel}>{panelContent}</CollapsePanal>
<Panel {...props?.panel}>{panelContent}</Panel>
</Collapse>
);

describe('Collapse Unit Test', () => {
// 渲染测试
test('create', async () => {
const { container, queryByText } = render(<CollapseTestComponent />);
expect(container.firstChild.classList.contains('t-collapse')).toBeTruthy();
expect(queryByText(panelContent)).toBeInTheDocument();
});

// 测试点击事件
test('click event', async () => {
const CollapseTest = () => (
<Collapse defaultExpandAll defaultValue="default">
<Panel header="这是一个折叠标题" value="default">
这部分是每个折叠面板折叠或展开的内容,可根据不同业务或用户的使用诉求,进行自定义填充。可以是纯文本、图文、子列表等内容形式。
</Panel>
</Collapse>
);
await act(async () => {
render(<CollapseTest />);
// 获取 element
const element = await waitFor(() => document.querySelector('.t-collapse-panel__header'));
expect(element).not.toBeNull();

// 默认展开,点击关闭
fireEvent.click(element);
expect(document.querySelector('.t-collapse-panel__body--collapsed')).not.toBeNull();

// 当前关闭,点击展开
fireEvent.click(element);
expect(document.querySelector('.t-collapse-panel__body--collapsed')).toBeNull();
});
});
});