Skip to content

Commit 9011e64

Browse files
danielbariongabrieljablonski
authored andcommitted
test: add a few tests to the Tooltip test suite
1 parent 6bef837 commit 9011e64

File tree

4 files changed

+221
-47
lines changed

4 files changed

+221
-47
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`tooltip attributes basic tooltip 1`] = `
4+
<div>
5+
<span
6+
data-tooltip-content="Hello World!"
7+
id="basic-example-attr"
8+
>
9+
Lorem Ipsum
10+
</span>
11+
<div
12+
class="react-tooltip"
13+
role="tooltip"
14+
>
15+
Hello World!
16+
<div
17+
class="react-tooltip-arrow"
18+
/>
19+
</div>
20+
</div>
21+
`;
22+
23+
exports[`tooltip attributes tooltip with place 1`] = `
24+
<div>
25+
<span
26+
data-tooltip-content="Hello World!"
27+
data-tooltip-place="right"
28+
id="example-place-attr"
29+
>
30+
Lorem Ipsum
31+
</span>
32+
<div
33+
class="react-tooltip"
34+
role="tooltip"
35+
>
36+
Hello World!
37+
<div
38+
class="react-tooltip-arrow"
39+
/>
40+
</div>
41+
</div>
42+
`;
43+
44+
exports[`tooltip attributes tooltip without element reference 1`] = `
45+
<div>
46+
<span
47+
data-tooltip-content="Hello World!"
48+
>
49+
Lorem Ipsum
50+
</span>
51+
</div>
52+
`;

src/test/__snapshots__/tooltip.spec.js.snap renamed to src/test/__snapshots__/tooltip-props.spec.js.snap

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`tooltip attributes basic tooltip 1`] = `
3+
exports[`tooltip props basic tooltip 1`] = `
44
<div>
55
<span
6-
data-tooltip-content="Hello World!"
7-
id="basic-example-attr"
6+
id="basic-example"
87
>
98
Lorem Ipsum
109
</span>
@@ -20,12 +19,33 @@ exports[`tooltip attributes basic tooltip 1`] = `
2019
</div>
2120
`;
2221

23-
exports[`tooltip attributes tooltip with place 1`] = `
22+
exports[`tooltip props clickable tooltip 1`] = `
23+
<div>
24+
<span
25+
id="example-clickable"
26+
>
27+
Lorem Ipsum
28+
</span>
29+
<div
30+
class="react-tooltip undefined"
31+
role="tooltip"
32+
style="left: 5px; top: -10px;"
33+
>
34+
<button>
35+
button
36+
</button>
37+
<div
38+
class="react-tooltip-arrow"
39+
style="left: 5px; bottom: -4px;"
40+
/>
41+
</div>
42+
</div>
43+
`;
44+
45+
exports[`tooltip props tooltip with custom position 1`] = `
2446
<div>
2547
<span
26-
data-tooltip-content="Hello World!"
27-
data-tooltip-place="right"
28-
id="example-place-attr"
48+
id="example-place"
2949
>
3050
Lorem Ipsum
3151
</span>
@@ -41,20 +61,20 @@ exports[`tooltip attributes tooltip with place 1`] = `
4161
</div>
4262
`;
4363

44-
exports[`tooltip attributes tooltip without element reference 1`] = `
64+
exports[`tooltip props tooltip with delay hide 1`] = `
4565
<div>
4666
<span
47-
data-tooltip-content="Hello World!"
67+
id="example-delay-hide"
4868
>
4969
Lorem Ipsum
5070
</span>
5171
</div>
5272
`;
5373

54-
exports[`tooltip props basic tooltip 1`] = `
74+
exports[`tooltip props tooltip with delay show 1`] = `
5575
<div>
5676
<span
57-
id="basic-example"
77+
id="example-delay-show"
5878
>
5979
Lorem Ipsum
6080
</span>
@@ -70,24 +90,20 @@ exports[`tooltip props basic tooltip 1`] = `
7090
</div>
7191
`;
7292

73-
exports[`tooltip props clickable tooltip 1`] = `
93+
exports[`tooltip props tooltip with float 1`] = `
7494
<div>
7595
<span
76-
id="example-clickable"
96+
id="example-float"
7797
>
7898
Lorem Ipsum
7999
</span>
80100
<div
81-
class="react-tooltip undefined"
101+
class="react-tooltip"
82102
role="tooltip"
83-
style="left: 5px; top: -10px;"
84103
>
85-
<button>
86-
button
87-
</button>
104+
Hello World!
88105
<div
89106
class="react-tooltip-arrow"
90-
style="left: 5px; bottom: -4px;"
91107
/>
92108
</div>
93109
</div>

src/test/tooltip-attributes.spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { render, screen, waitFor } from '@testing-library/react'
2+
import userEvent from '@testing-library/user-event'
3+
import '@testing-library/jest-dom'
4+
import { TooltipController as Tooltip } from '../components/TooltipController'
5+
6+
// Tell Jest to mock all timeout functions
7+
jest.useRealTimers()
8+
9+
// eslint-disable-next-line react/prop-types
10+
const TooltipAttrs = ({ id, ...anchorParams }) => (
11+
<>
12+
<span id={id} {...anchorParams}>
13+
Lorem Ipsum
14+
</span>
15+
<Tooltip anchorId={id} />
16+
</>
17+
)
18+
19+
describe('tooltip attributes', () => {
20+
test('tooltip without element reference', async () => {
21+
const { container } = render(<TooltipAttrs data-tooltip-content="Hello World!" />)
22+
const anchorElement = screen.getByText('Lorem Ipsum')
23+
24+
await userEvent.hover(anchorElement)
25+
26+
await waitFor(() => {
27+
expect(screen.queryByText('Hello World!')).not.toBeInTheDocument()
28+
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
29+
})
30+
31+
expect(container).toMatchSnapshot()
32+
})
33+
34+
test('basic tooltip', async () => {
35+
const { container } = render(
36+
<TooltipAttrs id="basic-example-attr" data-tooltip-content="Hello World!" />,
37+
)
38+
const anchorElement = screen.getByText('Lorem Ipsum')
39+
40+
await userEvent.hover(anchorElement)
41+
42+
let tooltip = null
43+
44+
await waitFor(() => {
45+
tooltip = screen.getByRole('tooltip')
46+
})
47+
48+
expect(anchorElement).toHaveAttribute('data-tooltip-content')
49+
expect(tooltip).toBeInTheDocument()
50+
expect(container).toMatchSnapshot()
51+
})
52+
53+
test('tooltip with place', async () => {
54+
const { container } = render(
55+
<TooltipAttrs
56+
id="example-place-attr"
57+
data-tooltip-content="Hello World!"
58+
data-tooltip-place="right"
59+
/>,
60+
)
61+
const anchorElement = screen.getByText('Lorem Ipsum')
62+
63+
await userEvent.hover(anchorElement)
64+
65+
let tooltip = null
66+
67+
await waitFor(() => {
68+
tooltip = screen.getByRole('tooltip')
69+
})
70+
71+
expect(anchorElement).toHaveAttribute('data-tooltip-place')
72+
expect(anchorElement).toHaveAttribute('data-tooltip-content')
73+
expect(tooltip).toBeInTheDocument()
74+
expect(container).toMatchSnapshot()
75+
})
76+
})

src/test/tooltip.spec.js renamed to src/test/tooltip-props.spec.js

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ const TooltipProps = ({ id, ...tooltipParams }) => (
1313
<Tooltip anchorId={id} {...tooltipParams} />
1414
</>
1515
)
16-
// eslint-disable-next-line react/prop-types
17-
const TooltipAttrs = ({ id, ...anchorParams }) => (
18-
<>
19-
<span id={id} {...anchorParams}>
20-
Lorem Ipsum
21-
</span>
22-
<Tooltip anchorId={id} />
23-
</>
24-
)
2516

2617
describe('tooltip props', () => {
2718
test('tooltip without element reference', async () => {
@@ -119,26 +110,74 @@ describe('tooltip props', () => {
119110
expect(mockCallBack).toHaveBeenCalled()
120111
expect(container).toMatchSnapshot()
121112
})
122-
})
123113

124-
describe('tooltip attributes', () => {
125-
test('tooltip without element reference', async () => {
126-
const { container } = render(<TooltipAttrs data-tooltip-content="Hello World!" />)
114+
test('tooltip with delay show', async () => {
115+
const { container } = render(
116+
<TooltipProps id="example-delay-show" content="Hello World!" delayShow={300} />,
117+
)
127118
const anchorElement = screen.getByText('Lorem Ipsum')
128119

129120
await userEvent.hover(anchorElement)
130121

122+
let tooltip = null
123+
124+
await waitFor(
125+
() => {
126+
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
127+
},
128+
{
129+
timeout: 250,
130+
},
131+
)
132+
133+
await userEvent.unhover(anchorElement)
134+
131135
await waitFor(() => {
132-
expect(screen.queryByText('Hello World!')).not.toBeInTheDocument()
133-
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
136+
tooltip = screen.getByRole('tooltip')
134137
})
135138

139+
expect(tooltip).toBeInTheDocument()
136140
expect(container).toMatchSnapshot()
137141
})
138142

139-
test('basic tooltip', async () => {
143+
test('tooltip with delay hide', async () => {
144+
const { container } = render(
145+
<TooltipProps id="example-delay-hide" content="Hello World!" delayHide={300} />,
146+
)
147+
const anchorElement = screen.getByText('Lorem Ipsum')
148+
149+
await userEvent.hover(anchorElement)
150+
151+
await waitFor(() => {
152+
expect(screen.queryByRole('tooltip')).toBeInTheDocument()
153+
})
154+
155+
await userEvent.unhover(anchorElement)
156+
157+
await waitFor(
158+
() => {
159+
expect(screen.queryByRole('tooltip')).toBeInTheDocument()
160+
},
161+
{
162+
timeout: 200,
163+
},
164+
)
165+
166+
await waitFor(
167+
() => {
168+
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
169+
},
170+
{
171+
timeout: 500,
172+
},
173+
)
174+
175+
expect(container).toMatchSnapshot()
176+
})
177+
178+
test('tooltip with custom position', async () => {
140179
const { container } = render(
141-
<TooltipAttrs id="basic-example-attr" data-tooltip-content="Hello World!" />,
180+
<TooltipProps id="example-place" content="Hello World!" position={{ x: 0, y: 0 }} />,
142181
)
143182
const anchorElement = screen.getByText('Lorem Ipsum')
144183

@@ -150,19 +189,12 @@ describe('tooltip attributes', () => {
150189
tooltip = screen.getByRole('tooltip')
151190
})
152191

153-
expect(anchorElement).toHaveAttribute('data-tooltip-content')
154192
expect(tooltip).toBeInTheDocument()
155193
expect(container).toMatchSnapshot()
156194
})
157195

158-
test('tooltip with place', async () => {
159-
const { container } = render(
160-
<TooltipAttrs
161-
id="example-place-attr"
162-
data-tooltip-content="Hello World!"
163-
data-tooltip-place="right"
164-
/>,
165-
)
196+
test('tooltip with float', async () => {
197+
const { container } = render(<TooltipProps id="example-float" content="Hello World!" float />)
166198
const anchorElement = screen.getByText('Lorem Ipsum')
167199

168200
await userEvent.hover(anchorElement)
@@ -173,8 +205,6 @@ describe('tooltip attributes', () => {
173205
tooltip = screen.getByRole('tooltip')
174206
})
175207

176-
expect(anchorElement).toHaveAttribute('data-tooltip-place')
177-
expect(anchorElement).toHaveAttribute('data-tooltip-content')
178208
expect(tooltip).toBeInTheDocument()
179209
expect(container).toMatchSnapshot()
180210
})

0 commit comments

Comments
 (0)