-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtooltip.test.tsx
More file actions
83 lines (72 loc) · 2.44 KB
/
Copy pathtooltip.test.tsx
File metadata and controls
83 lines (72 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { render, screen, waitFor } from '@testing-library/react';
import { act } from 'react';
import { Tooltip } from './tooltip';
describe('Tooltip', () => {
it('should render child', () => {
render(
<Tooltip content='text'>
<button>Button</button>
</Tooltip>,
);
const tooltipTrigger = screen.getByRole('button');
expect(tooltipTrigger).toBeInTheDocument();
vi.waitFor(
() => expect(tooltipTrigger).toHaveAttribute('aria-description', 'text'), // Let MutationObserver run first
);
});
it('should render tooltip on hover', async () => {
const content = 'Unique content for hover-test';
render(
<Tooltip content={content}>
<button>Button</button>
</Tooltip>,
);
const tooltipTrigger = screen.getByRole('button');
expect(screen.queryByText(content)).not.toBeInTheDocument();
const hover = new MouseEvent('mouseover', { bubbles: true });
await act(async () => tooltipTrigger.dispatchEvent(hover));
const tooltip = await screen.findByText(content);
expect(tooltip).toBeVisible();
expect(screen.getByText(content)).toBeVisible();
});
it('should render tooltip on focus', async () => {
const content = 'Unique content for focus-test';
render(
<Tooltip content={content}>
<button>Button</button>
</Tooltip>,
);
expect(screen.queryByText(content)).not.toBeInTheDocument();
await act(async () => screen.getByRole('button').focus());
const tooltip = screen.getByText(content);
expect(tooltip).toBeVisible();
});
it('should render span when children is a string', () => {
const children = 'My string child';
render(<Tooltip content='text'>{children}</Tooltip>);
const tooltipTrigger = screen.getByText(children);
expect(tooltipTrigger.tagName).toBe('SPAN');
});
it('should be aria-describedby when there is text in the trigger', () => {
render(
<Tooltip content='text'>
<button>text</button>
</Tooltip>,
);
const trigger = screen.getByRole('button');
waitFor(() =>
expect(trigger.getAttribute('aria-description')).not.toBeNullable(),
);
});
it('should be aria-labelledby when there is no text in the trigger', () => {
render(
<Tooltip content='text'>
<button />
</Tooltip>,
);
const trigger = screen.getByRole('button');
waitFor(() =>
expect(trigger.getAttribute('aria-label')).not.toBeNullable(),
);
});
});