Skip to content

Commit 6bef837

Browse files
danielbariongabrieljablonski
authored andcommitted
test: add click test to Tooltip test suite
1 parent 633cf72 commit 6bef837

File tree

4 files changed

+94
-33
lines changed

4 files changed

+94
-33
lines changed

src/App.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ function WithProviderMinimal() {
1919
</TooltipWrapper>
2020
</p>
2121
<Tooltip clickable>
22-
<button>button</button>
22+
<button
23+
onClick={() => {
24+
console.log('button clicked')
25+
}}
26+
>
27+
button
28+
</button>
2329
</Tooltip>
2430
</section>
2531
)

src/test/__snapshots__/tooltip.spec.js.snap

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`tooltip attributes basic tooltip component 1`] = `
3+
exports[`tooltip attributes basic tooltip 1`] = `
44
<div>
55
<span
66
data-tooltip-content="Hello World!"
@@ -20,17 +20,7 @@ exports[`tooltip attributes basic tooltip component 1`] = `
2020
</div>
2121
`;
2222

23-
exports[`tooltip attributes tooltip component - without element reference 1`] = `
24-
<div>
25-
<span
26-
data-tooltip-content="Hello World!"
27-
>
28-
Lorem Ipsum
29-
</span>
30-
</div>
31-
`;
32-
33-
exports[`tooltip attributes tooltip component with place 1`] = `
23+
exports[`tooltip attributes tooltip with place 1`] = `
3424
<div>
3525
<span
3626
data-tooltip-content="Hello World!"
@@ -51,7 +41,17 @@ exports[`tooltip attributes tooltip component with place 1`] = `
5141
</div>
5242
`;
5343

54-
exports[`tooltip props basic tooltip component 1`] = `
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+
`;
53+
54+
exports[`tooltip props basic tooltip 1`] = `
5555
<div>
5656
<span
5757
id="basic-example"
@@ -70,15 +70,30 @@ exports[`tooltip props basic tooltip component 1`] = `
7070
</div>
7171
`;
7272

73-
exports[`tooltip props tooltip component - without element reference 1`] = `
73+
exports[`tooltip props clickable tooltip 1`] = `
7474
<div>
75-
<span>
75+
<span
76+
id="example-clickable"
77+
>
7678
Lorem Ipsum
7779
</span>
80+
<div
81+
class="react-tooltip undefined"
82+
role="tooltip"
83+
style="left: 5px; top: -10px;"
84+
>
85+
<button>
86+
button
87+
</button>
88+
<div
89+
class="react-tooltip-arrow"
90+
style="left: 5px; bottom: -4px;"
91+
/>
92+
</div>
7893
</div>
7994
`;
8095

81-
exports[`tooltip props tooltip component with html 1`] = `
96+
exports[`tooltip props tooltip with html 1`] = `
8297
<div>
8398
<span
8499
id="example-html"
@@ -102,7 +117,7 @@ exports[`tooltip props tooltip component with html 1`] = `
102117
</div>
103118
`;
104119

105-
exports[`tooltip props tooltip component with place 1`] = `
120+
exports[`tooltip props tooltip with place 1`] = `
106121
<div>
107122
<span
108123
id="example-place"
@@ -120,3 +135,11 @@ exports[`tooltip props tooltip component with place 1`] = `
120135
</div>
121136
</div>
122137
`;
138+
139+
exports[`tooltip props tooltip without element reference 1`] = `
140+
<div>
141+
<span>
142+
Lorem Ipsum
143+
</span>
144+
</div>
145+
`;

src/test/tooltip-wrapper.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('tooltip props', () => {
2121
)
2222
const anchorElement = screen.getByText('Minimal 1')
2323

24-
userEvent.hover(anchorElement)
24+
await userEvent.hover(anchorElement)
2525

2626
let tooltip = null
2727

src/test/tooltip.spec.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ const TooltipAttrs = ({ id, ...anchorParams }) => (
2424
)
2525

2626
describe('tooltip props', () => {
27-
test('tooltip component - without element reference', async () => {
27+
test('tooltip without element reference', async () => {
2828
const { container } = render(<TooltipProps content="Hello World!" />)
2929
const anchorElement = screen.getByText('Lorem Ipsum')
3030

31-
userEvent.hover(anchorElement)
31+
await userEvent.hover(anchorElement)
3232

3333
await waitFor(() => {
3434
expect(screen.queryByText('Hello World!')).not.toBeInTheDocument()
@@ -38,11 +38,11 @@ describe('tooltip props', () => {
3838
expect(container).toMatchSnapshot()
3939
})
4040

41-
test('basic tooltip component', async () => {
41+
test('basic tooltip', async () => {
4242
const { container } = render(<TooltipProps id="basic-example" content="Hello World!" />)
4343
const anchorElement = screen.getByText('Lorem Ipsum')
4444

45-
userEvent.hover(anchorElement)
45+
await userEvent.hover(anchorElement)
4646

4747
let tooltip = null
4848

@@ -54,13 +54,13 @@ describe('tooltip props', () => {
5454
expect(container).toMatchSnapshot()
5555
})
5656

57-
test('tooltip component with place', async () => {
57+
test('tooltip with place', async () => {
5858
const { container } = render(
5959
<TooltipProps id="example-place" content="Hello World!" place="right" />,
6060
)
6161
const anchorElement = screen.getByText('Lorem Ipsum')
6262

63-
userEvent.hover(anchorElement)
63+
await userEvent.hover(anchorElement)
6464

6565
let tooltip = null
6666

@@ -72,11 +72,11 @@ describe('tooltip props', () => {
7272
expect(container).toMatchSnapshot()
7373
})
7474

75-
test('tooltip component with html', async () => {
75+
test('tooltip with html', async () => {
7676
const { container } = render(<TooltipProps id="example-html" html="<div>Hello World!<div>" />)
7777
const anchorElement = screen.getByText('Lorem Ipsum')
7878

79-
userEvent.hover(anchorElement)
79+
await userEvent.hover(anchorElement)
8080

8181
let tooltip = null
8282

@@ -87,14 +87,46 @@ describe('tooltip props', () => {
8787
expect(tooltip).toBeInTheDocument()
8888
expect(container).toMatchSnapshot()
8989
})
90+
91+
test('clickable tooltip', async () => {
92+
const id = 'example-clickable'
93+
94+
const mockCallBack = jest.fn()
95+
const { container } = render(
96+
<>
97+
<span id={id}>Lorem Ipsum</span>
98+
<Tooltip anchorId={id} clickable>
99+
<button onClick={mockCallBack}>button</button>
100+
</Tooltip>
101+
</>,
102+
)
103+
const anchorElement = screen.getByText('Lorem Ipsum')
104+
105+
await userEvent.hover(anchorElement)
106+
107+
let tooltip = null
108+
let button = null
109+
110+
await waitFor(() => {
111+
tooltip = screen.getByRole('tooltip')
112+
button = screen.getByRole('button')
113+
})
114+
115+
await userEvent.click(button)
116+
117+
expect(tooltip).toBeInTheDocument()
118+
expect(button).toBeInTheDocument()
119+
expect(mockCallBack).toHaveBeenCalled()
120+
expect(container).toMatchSnapshot()
121+
})
90122
})
91123

92124
describe('tooltip attributes', () => {
93-
test('tooltip component - without element reference', async () => {
125+
test('tooltip without element reference', async () => {
94126
const { container } = render(<TooltipAttrs data-tooltip-content="Hello World!" />)
95127
const anchorElement = screen.getByText('Lorem Ipsum')
96128

97-
userEvent.hover(anchorElement)
129+
await userEvent.hover(anchorElement)
98130

99131
await waitFor(() => {
100132
expect(screen.queryByText('Hello World!')).not.toBeInTheDocument()
@@ -104,13 +136,13 @@ describe('tooltip attributes', () => {
104136
expect(container).toMatchSnapshot()
105137
})
106138

107-
test('basic tooltip component', async () => {
139+
test('basic tooltip', async () => {
108140
const { container } = render(
109141
<TooltipAttrs id="basic-example-attr" data-tooltip-content="Hello World!" />,
110142
)
111143
const anchorElement = screen.getByText('Lorem Ipsum')
112144

113-
userEvent.hover(anchorElement)
145+
await userEvent.hover(anchorElement)
114146

115147
let tooltip = null
116148

@@ -123,7 +155,7 @@ describe('tooltip attributes', () => {
123155
expect(container).toMatchSnapshot()
124156
})
125157

126-
test('tooltip component with place', async () => {
158+
test('tooltip with place', async () => {
127159
const { container } = render(
128160
<TooltipAttrs
129161
id="example-place-attr"
@@ -133,7 +165,7 @@ describe('tooltip attributes', () => {
133165
)
134166
const anchorElement = screen.getByText('Lorem Ipsum')
135167

136-
userEvent.hover(anchorElement)
168+
await userEvent.hover(anchorElement)
137169

138170
let tooltip = null
139171

0 commit comments

Comments
 (0)