1
+ import { render , fireEvent , act } from '@testing-library/react' ;
1
2
import * as React from 'react' ;
2
- import { cleanup , fireEvent , render , screen } from '@testing-library/react' ;
3
3
import { TooltipComponent } from '../tooltip-component' ;
4
4
5
- const model = {
6
- id : '123' ,
7
- range : { start : 1 , end : 10 } ,
8
- label : 'model'
9
- } ;
10
- const tooltip = new TooltipComponent ( 10 ) ;
11
- tooltip . setState = jest . fn ( ) ;
12
-
13
- describe ( 'Tooltip component' , ( ) => {
14
- let axisComponent : any ;
15
- const ref = ( el : TooltipComponent | undefined | null ) : void => {
16
- axisComponent = el ;
17
- } ;
18
-
5
+ describe ( 'TooltipComponent' , ( ) => {
19
6
beforeEach ( ( ) => {
20
- axisComponent = null ;
21
- } ) ;
22
-
23
- afterEach ( ( ) => {
24
- cleanup ( ) ;
25
- jest . clearAllMocks ( ) ;
7
+ document . body . innerHTML = '' ;
26
8
} ) ;
27
9
28
- /*
29
- * Skip due to issues with TooltipComponent:
30
- *
31
- * react-tooltip v4.2.14 works in the application but causes an exception
32
- * in tests (https://github.com/ReactTooltip/react-tooltip/issues/681),
33
- * which is fixed in v4.2.17. However, version v4.2.17 breaks the tooltip
34
- * when running in the application.
35
- */
36
- it . skip ( 'renders itself' , ( ) => {
37
- render ( < TooltipComponent /> ) ;
38
- expect ( axisComponent ) . toBeTruthy ( ) ;
39
- expect ( axisComponent instanceof TooltipComponent ) . toBe ( true ) ;
10
+ it ( 'renders content' , async ( ) => {
11
+ render ( < TooltipComponent visible = { true } content = "Test Content" /> ) ;
12
+ const tooltip = document . querySelector ( '.trace-compass-tooltip' ) as HTMLElement ;
13
+ expect ( tooltip . textContent ) . toBe ( 'Test Content' ) ;
14
+ expect ( tooltip . style . opacity ) . toBe ( '1' ) ;
40
15
} ) ;
41
16
42
- // Skip due to issues with TooltipComponent (see above for details)
43
- it . skip ( 'resets timer on mouse enter' , ( ) => {
44
- tooltip . state = {
45
- element : model ,
46
- func : undefined ,
47
- content : 'Test'
48
- } ;
49
- render ( < TooltipComponent /> ) ;
50
- const component = screen . getByRole ( 'tooltip-component-role' ) ;
51
- fireEvent . mouseEnter ( component ) ;
52
- fireEvent . mouseLeave ( component ) ;
53
-
54
- expect ( tooltip . setState ) . toBeCalledWith ( { content : undefined } ) ;
55
- } ) ;
56
-
57
- it ( 'displays a tooltip for a time graph state component' , ( ) => {
58
- tooltip . state = {
59
- element : undefined ,
60
- func : undefined ,
61
- content : undefined
62
- } ;
63
- tooltip . setElement ( model ) ;
64
-
65
- expect ( tooltip . setState ) . toBeCalledWith ( { element : model , func : undefined } ) ;
66
- } ) ;
67
-
68
- it ( 'hides tooltip if mouse is not hovering over element' , async ( ) => {
69
- tooltip . state = {
70
- element : model ,
71
- func : undefined ,
72
- content : 'Test'
73
- } ;
74
- tooltip . setElement ( undefined ) ;
75
- await new Promise ( r => setTimeout ( r , 500 ) ) ;
76
-
77
- expect ( tooltip . setState ) . toBeCalledWith ( { content : undefined } ) ;
17
+ it ( 'de-renders when visibility changes' , async ( ) => {
18
+ const { rerender } = render ( < TooltipComponent visible = { true } content = "Test" /> ) ;
19
+ const tooltip = document . querySelector ( '.trace-compass-tooltip' ) as HTMLElement ;
20
+
21
+ expect ( tooltip . style . opacity ) . toBe ( '1' ) ;
22
+
23
+ rerender ( < TooltipComponent visible = { false } content = "Test" /> ) ;
24
+ await new Promise ( resolve => setTimeout ( resolve , 1001 ) ) ;
25
+ expect ( tooltip . style . opacity ) . toBe ( '0' ) ;
78
26
} ) ;
79
27
80
- it ( 'hide tooltip because there is no content' , ( ) => {
81
- tooltip . state = {
82
- element : model ,
83
- func : undefined ,
84
- content : undefined
85
- } ;
86
- tooltip . setElement ( undefined ) ;
28
+ it ( 'stays rendered when mouse moves on top of it' , async ( ) => {
29
+ render ( < TooltipComponent visible = { true } content = "Test" /> ) ;
30
+ const tooltip = document . querySelector ( '.trace-compass-tooltip' ) as HTMLElement ;
87
31
88
- expect ( tooltip . setState ) . toBeCalledWith ( { content : undefined } ) ;
32
+ fireEvent . mouseEnter ( tooltip ) ;
33
+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
34
+ expect ( tooltip . style . opacity ) . toBe ( '1' ) ;
89
35
} ) ;
90
- } ) ;
36
+ } ) ;
0 commit comments