|
1 | 1 | import React from 'react'; |
2 | 2 | import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
3 | 4 | import { Tooltip } from '../Tooltip'; |
4 | 5 |
|
| 6 | +jest.mock('../../../helpers/Popper/Popper'); |
| 7 | + |
5 | 8 | test('Does not render by default', () => { |
6 | 9 | render(<Tooltip content="Test content" />); |
7 | 10 |
|
8 | | - expect(screen.queryByText('Test content')).not.toBeInTheDocument(); |
| 11 | + expect(screen.queryByRole('tooltip')).not.toBeInTheDocument(); |
9 | 12 | }); |
10 | 13 |
|
11 | | -// Need to pass isVisible and make test callbacks async in order to render tooltip |
12 | | - |
13 | | -test('Renders with content', async () => { |
| 14 | +test('Renders when isVisible is true', async () => { |
14 | 15 | render(<Tooltip isVisible content="Test content" />); |
15 | 16 |
|
16 | | - const tooltip = await screen.findByText('Test content'); |
17 | | - expect(tooltip.parentElement).toBeVisible(); |
| 17 | + const tooltip = await screen.findByRole('tooltip'); |
| 18 | + expect(tooltip).toBeVisible(); |
18 | 19 | }); |
19 | 20 |
|
20 | 21 | test('Renders with class name pf-v5-c-tooltip by default', async () => { |
21 | 22 | render(<Tooltip isVisible content="Test content" />); |
22 | 23 |
|
23 | | - const tooltip = await screen.findByText('Test content'); |
24 | | - expect(tooltip.parentElement).toHaveClass('pf-v5-c-tooltip'); |
| 24 | + const tooltip = await screen.findByRole('tooltip'); |
| 25 | + expect(tooltip).toHaveClass('pf-v5-c-tooltip'); |
25 | 26 | }); |
26 | 27 |
|
27 | 28 | test('Renders with custom class names provided via prop', async () => { |
28 | 29 | render(<Tooltip isVisible className="test-class" content="Test content" />); |
29 | 30 |
|
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'); |
| 31 | + const tooltip = await screen.findByRole('tooltip'); |
| 32 | + expect(tooltip).toHaveClass('test-class'); |
39 | 33 | }); |
40 | 34 |
|
41 | 35 | test('Renders with aria-live of off by default when triggerRef is not passed', async () => { |
42 | 36 | render(<Tooltip isVisible content="Test content" />); |
43 | 37 |
|
44 | | - const tooltip = await screen.findByText('Test content'); |
45 | | - expect(tooltip.parentElement).toHaveAttribute('aria-live', 'off'); |
| 38 | + const tooltip = await screen.findByRole('tooltip'); |
| 39 | + expect(tooltip).toHaveAttribute('aria-live', 'off'); |
46 | 40 | }); |
47 | 41 |
|
48 | 42 | test('Renders with aria-live of polite by default when triggerRef is passed', async () => { |
49 | 43 | const triggerRef = React.createRef(); |
50 | 44 | render(<Tooltip triggerRef={triggerRef} isVisible content="Test content" />); |
51 | 45 |
|
52 | | - const tooltip = await screen.findByText('Test content'); |
53 | | - expect(tooltip.parentElement).toHaveAttribute('aria-live', 'polite'); |
| 46 | + const tooltip = await screen.findByRole('tooltip'); |
| 47 | + expect(tooltip).toHaveAttribute('aria-live', 'polite'); |
54 | 48 | }); |
55 | 49 |
|
56 | 50 | test('Renders with value passed to aria-live prop', async () => { |
57 | 51 | render(<Tooltip aria-live="polite" isVisible content="Test content" />); |
58 | 52 |
|
59 | | - const tooltip = await screen.findByText('Test content'); |
60 | | - expect(tooltip.parentElement).toHaveAttribute('aria-live', 'polite'); |
| 53 | + const tooltip = await screen.findByRole('tooltip'); |
| 54 | + expect(tooltip).toHaveAttribute('aria-live', 'polite'); |
61 | 55 | }); |
62 | 56 |
|
63 | 57 | test('Renders with value passed to id prop', async () => { |
64 | 58 | render(<Tooltip id="custom-id" isVisible content="Test content" />); |
65 | 59 |
|
66 | | - const tooltip = await screen.findByText('Test content'); |
67 | | - expect(tooltip.parentElement).toHaveAttribute('id', 'custom-id'); |
| 60 | + const tooltip = await screen.findByRole('tooltip'); |
| 61 | + expect(tooltip).toHaveAttribute('id', 'custom-id'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('Passes zIndex to Popper', async () => { |
| 65 | + render(<Tooltip zIndex={10} content="Test content" />); |
| 66 | + |
| 67 | + const contentPassedToPopper = await screen.findByText('zIndex: 10'); |
| 68 | + expect(contentPassedToPopper).toBeVisible(); |
| 69 | +}); |
| 70 | + |
| 71 | +test('Passes enableFlip to Popper', async () => { |
| 72 | + render(<Tooltip enableFlip content="Test content" />); |
| 73 | + |
| 74 | + const contentPassedToPopper = await screen.findByText('enableFlip: true'); |
| 75 | + expect(contentPassedToPopper).toBeVisible(); |
| 76 | +}); |
| 77 | + |
| 78 | +test('Passes position as placement to Popper', async () => { |
| 79 | + render(<Tooltip position="left-start" content="Test content" />); |
| 80 | + |
| 81 | + const contentPassedToPopper = await screen.findByText('placement: left-start'); |
| 82 | + expect(contentPassedToPopper).toBeVisible(); |
| 83 | +}); |
| 84 | + |
| 85 | +test('Passes appendTo to Popper', async () => { |
| 86 | + render(<Tooltip appendTo={() => document.body} content="Test content" />); |
| 87 | + |
| 88 | + const contentPassedToPopper = await screen.findByText('appendTo: () => document.body'); |
| 89 | + expect(contentPassedToPopper).toBeVisible(); |
| 90 | +}); |
| 91 | + |
| 92 | +test('Passes distance to Popper', async () => { |
| 93 | + render(<Tooltip distance={5} content="Test content" />); |
| 94 | + |
| 95 | + const contentPassedToPopper = await screen.findByText('distance: 5'); |
| 96 | + expect(contentPassedToPopper).toBeVisible(); |
| 97 | +}); |
| 98 | + |
| 99 | +test('Passes flipBehavior to Popper', async () => { |
| 100 | + render(<Tooltip flipBehavior="flip" content="Test content" />); |
| 101 | + |
| 102 | + const contentPassedToPopper = await screen.findByText('flipBehavior: flip'); |
| 103 | + expect(contentPassedToPopper).toBeVisible(); |
| 104 | +}); |
| 105 | + |
| 106 | +test('Passes minWidth to Popper', async () => { |
| 107 | + render(<Tooltip minWidth="100px" content="Test content" />); |
| 108 | + |
| 109 | + const contentPassedToPopper = await screen.findByText('minWidth: 100px'); |
| 110 | + expect(contentPassedToPopper).toBeVisible(); |
| 111 | +}); |
| 112 | + |
| 113 | +test('Matches snapshot', async () => { |
| 114 | + render(<Tooltip id="custom-id" isVisible content="Test content" />); |
| 115 | + |
| 116 | + const tooltip = await screen.findByRole('tooltip'); |
| 117 | + expect(tooltip).toMatchSnapshot(); |
68 | 118 | }); |
0 commit comments