Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit c8153f5

Browse files
committed
test: test coverage for accordion, tooltip and tabs
1 parent 741faee commit c8153f5

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { cleanup, render, screen } from '@testing-library/react';
3+
import CustomAccordion from '..';
4+
import userEvent from '@testing-library/user-event';
5+
6+
const mock_accordion_items = [
7+
{ header: 'header_1', content: 'content 1' },
8+
{ header: 'header_2', content: 'content 2' },
9+
];
10+
11+
describe('CustomAccordion', () => {
12+
beforeEach(() => {
13+
render(<CustomAccordion items={mock_accordion_items} />);
14+
});
15+
16+
afterEach(() => {
17+
cleanup();
18+
});
19+
20+
it('should render the custom accordion', () => {
21+
const header = screen.getByText('header_1');
22+
expect(header).toBeInTheDocument();
23+
});
24+
25+
it('should open accordion content on click', async () => {
26+
const header = screen.getByText('header_2');
27+
await userEvent.click(header);
28+
const content = screen.getByText('content 2');
29+
expect(content).toBeInTheDocument();
30+
});
31+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { cleanup, render, screen } from '@testing-library/react';
3+
import CustomTabs from '..';
4+
import userEvent from '@testing-library/user-event';
5+
6+
const mock_tabs = [
7+
{ label: 'tab_1', content: 'content 1' },
8+
{ label: 'tab_2', content: 'content 2' },
9+
];
10+
11+
describe('CustomTabs', () => {
12+
beforeEach(() => {
13+
render(<CustomTabs tabs={mock_tabs}></CustomTabs>);
14+
});
15+
16+
afterEach(() => {
17+
cleanup();
18+
});
19+
20+
it('should render the custom tabs', () => {
21+
const tab = screen.getByText('tab_1');
22+
expect(tab).toBeInTheDocument();
23+
});
24+
25+
it('should change tab content on different tab click', async () => {
26+
const tab = screen.getByText('tab_2');
27+
await userEvent.click(tab);
28+
const content = screen.getByText('content 2');
29+
expect(content).toBeInTheDocument();
30+
});
31+
});

src/components/CustomTabs/index.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ const CustomTabs: React.FC<{
99
}> = ({ tabs }) => {
1010
const [activeTab, setActiveTab] = useState(0);
1111

12-
const handleTabClick = (index) => {
13-
setActiveTab(index);
14-
};
15-
1612
return (
1713
<div className='tabs'>
1814
<div className='tabs_header'>
@@ -21,7 +17,7 @@ const CustomTabs: React.FC<{
2117
<div
2218
key={index}
2319
className={`tabs_header__item ${activeTab === index ? 'active' : ''}`}
24-
onClick={() => handleTabClick(index)}
20+
onClick={() => setActiveTab(index)}
2521
>
2622
{tab.label}
2723
</div>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import { cleanup, render, screen } from '@testing-library/react';
3+
import CustomTooltip from '..';
4+
import userEvent from '@testing-library/user-event';
5+
6+
describe('CustomTooltip', () => {
7+
beforeEach(() => {
8+
render(
9+
<CustomTooltip text='tooltip text'>
10+
<div>outer text</div>
11+
</CustomTooltip>,
12+
);
13+
});
14+
15+
afterEach(() => {
16+
cleanup();
17+
});
18+
19+
it('should render the custom tooltip with children', () => {
20+
const text = screen.getByText('outer text');
21+
expect(text).toBeInTheDocument();
22+
});
23+
24+
it('should render the tooltip text on hover', async () => {
25+
const text = screen.getByText('outer text');
26+
await userEvent.hover(text);
27+
const tooltip_text = screen.getAllByText('tooltip text');
28+
expect(tooltip_text[0]).toBeInTheDocument();
29+
});
30+
});

0 commit comments

Comments
 (0)