Skip to content

Commit 453029e

Browse files
author
Eric Olkowski
committed
chore(Tooltip): updated unit tests
1 parent 6d3f867 commit 453029e

File tree

10 files changed

+154
-107
lines changed

10 files changed

+154
-107
lines changed

packages/react-core/src/components/Tooltip/__tests__/Generated/TooltipArrow.test.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/react-core/src/components/Tooltip/__tests__/Generated/TooltipContent.test.tsx

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/react-core/src/components/Tooltip/__tests__/Generated/__snapshots__/TooltipArrow.test.tsx.snap

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/react-core/src/components/Tooltip/__tests__/Generated/__snapshots__/TooltipContent.test.tsx.snap

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,68 @@
1-
import * as React from 'react';
2-
import { render } from '@testing-library/react';
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
33
import { Tooltip } from '../Tooltip';
44

5-
test('tooltip renders', () => {
6-
const ref = React.createRef<HTMLDivElement>();
7-
const { asFragment } = render(
8-
<Tooltip
9-
position="top"
10-
triggerRef={ref}
11-
content={
12-
<div>
13-
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id feugiat augue, nec fringilla turpis.
14-
</div>
15-
}
16-
>
17-
<div ref={ref}>Toggle tooltip</div>
18-
</Tooltip>
19-
);
20-
expect(asFragment()).toMatchSnapshot();
21-
});
22-
23-
test('tooltip renders in strict mode', () => {
24-
const consoleError = jest.spyOn(console, 'error');
25-
const ref = React.createRef<HTMLDivElement>();
26-
const { asFragment } = render(
27-
<React.StrictMode>
28-
<Tooltip
29-
position="top"
30-
triggerRef={ref}
31-
content={
32-
<div>
33-
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id feugiat augue, nec fringilla turpis.
34-
</div>
35-
}
36-
>
37-
<div ref={ref}>Toggle tooltip</div>
38-
</Tooltip>
39-
</React.StrictMode>
40-
);
41-
expect(consoleError).not.toHaveBeenCalled();
42-
expect(asFragment()).toMatchSnapshot();
5+
test('Does not render by default', () => {
6+
render(<Tooltip content="Test content" />);
7+
8+
expect(screen.queryByText('Test content')).not.toBeInTheDocument();
9+
});
10+
11+
// Need to pass isVisible and make test callbacks async in order to render tooltip
12+
13+
test('Renders with content', async () => {
14+
render(<Tooltip isVisible content="Test content" />);
15+
16+
const tooltip = await screen.findByText('Test content');
17+
expect(tooltip.parentElement).toBeVisible();
18+
});
19+
20+
test('Renders with class name pf-v5-c-tooltip by default', async () => {
21+
render(<Tooltip isVisible content="Test content" />);
22+
23+
const tooltip = await screen.findByText('Test content');
24+
expect(tooltip.parentElement).toHaveClass('pf-v5-c-tooltip');
25+
});
26+
27+
test('Renders with custom class names provided via prop', async () => {
28+
render(<Tooltip isVisible className="test-class" content="Test content" />);
29+
30+
const tooltip = await screen.findByText('Test content');
31+
expect(tooltip.parentElement).toHaveClass('test-class');
32+
});
33+
34+
test('Renders with role of tooltip', async () => {
35+
render(<Tooltip isVisible content="Test content" />);
36+
37+
const tooltip = await screen.findByText('Test content');
38+
expect(tooltip.parentElement).toHaveAttribute('role', 'tooltip');
39+
});
40+
41+
test('Renders with aria-live of off by default when triggerRef is not passed', async () => {
42+
render(<Tooltip isVisible content="Test content" />);
43+
44+
const tooltip = await screen.findByText('Test content');
45+
expect(tooltip.parentElement).toHaveAttribute('aria-live', 'off');
46+
});
47+
48+
test('Renders with aria-live of polite by default when triggerRef is passed', async () => {
49+
const triggerRef = React.createRef();
50+
render(<Tooltip triggerRef={triggerRef} isVisible content="Test content" />);
51+
52+
const tooltip = await screen.findByText('Test content');
53+
expect(tooltip.parentElement).toHaveAttribute('aria-live', 'polite');
54+
});
55+
56+
test('Renders with value passed to aria-live prop', async () => {
57+
render(<Tooltip aria-live="polite" isVisible content="Test content" />);
58+
59+
const tooltip = await screen.findByText('Test content');
60+
expect(tooltip.parentElement).toHaveAttribute('aria-live', 'polite');
61+
});
62+
63+
test('Renders with value passed to id prop', async () => {
64+
render(<Tooltip id="custom-id" isVisible content="Test content" />);
65+
66+
const tooltip = await screen.findByText('Test content');
67+
expect(tooltip.parentElement).toHaveAttribute('id', 'custom-id');
4368
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
3+
import { render, screen } from '@testing-library/react';
4+
5+
import { TooltipArrow } from '../TooltipArrow';
6+
7+
test('Renders without children', () => {
8+
render(<TooltipArrow data-testid="tooltipArrow" />);
9+
10+
expect(screen.getByTestId('tooltipArrow')).toBeVisible();
11+
});
12+
13+
test('Renders with class name pf-v5-c-tooltip__arrow by default', () => {
14+
render(<TooltipArrow data-testid="tooltipArrow" />);
15+
16+
expect(screen.getByTestId('tooltipArrow')).toHaveClass('pf-v5-c-tooltip__arrow');
17+
});
18+
19+
test('Renders with custom class names provided via prop', () => {
20+
render(<TooltipArrow className="test-class" data-testid="tooltipArrow" />);
21+
22+
expect(screen.getByTestId('tooltipArrow')).toHaveClass('test-class');
23+
});
24+
25+
test('Matches the snapshot', () => {
26+
const { asFragment } = render(<TooltipArrow data-testid="tooltipArrow" />);
27+
expect(asFragment()).toMatchSnapshot();
28+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
3+
import { render, screen } from '@testing-library/react';
4+
5+
import { TooltipContent } from '../TooltipContent';
6+
7+
test('Renders with children', () => {
8+
render(<TooltipContent>Test content</TooltipContent>);
9+
10+
expect(screen.getByText('Test content')).toBeVisible();
11+
});
12+
13+
test('Renders with class name pf-v5-c-tooltip__content by default', () => {
14+
render(<TooltipContent>Test content</TooltipContent>);
15+
16+
expect(screen.getByText('Test content')).toHaveClass('pf-v5-c-tooltip__content');
17+
});
18+
19+
test('Renders with custom class names provided via prop', () => {
20+
render(<TooltipContent className="test-class">Test content</TooltipContent>);
21+
22+
expect(screen.getByText('Test content')).toHaveClass('test-class');
23+
});
24+
25+
test('Does not render with class pf-m-text-align-left when isLeftAligned is not passed', () => {
26+
render(<TooltipContent>Test content</TooltipContent>);
27+
28+
expect(screen.getByText('Test content')).not.toHaveClass('pf-m-text-align-left');
29+
});
30+
31+
test('Renders with class pf-m-text-align-left when isLeftAligned is passed', () => {
32+
render(<TooltipContent isLeftAligned>Test content</TooltipContent>);
33+
34+
expect(screen.getByText('Test content')).toHaveClass('pf-m-text-align-left');
35+
});
36+
37+
test('Matches the snapshot', () => {
38+
const { asFragment } = render(<TooltipContent>Test content</TooltipContent>);
39+
expect(asFragment()).toMatchSnapshot();
40+
});

packages/react-core/src/components/Tooltip/__tests__/__snapshots__/Tooltip.test.tsx.snap

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Matches the snapshot 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="pf-v5-c-tooltip__arrow"
7+
data-testid="tooltipArrow"
8+
/>
9+
</DocumentFragment>
10+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Matches the snapshot 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="pf-v5-c-tooltip__content"
7+
>
8+
Test content
9+
</div>
10+
</DocumentFragment>
11+
`;

0 commit comments

Comments
 (0)