1
- import React , { ReactNode } from "react" ;
1
+ import React , { MouseEvent , ReactNode } from "react" ;
2
2
import { render , fireEvent , act } from "@testing-library/react" ;
3
3
import { SimplePosition , UserInteractionModeListener } from "@react-md/utils" ;
4
4
@@ -7,6 +7,7 @@ import { Tooltipped } from "../Tooltipped";
7
7
jest . useFakeTimers ( ) ;
8
8
9
9
interface TestProps {
10
+ onClick ?( event : MouseEvent ) : void ;
10
11
position ?: SimplePosition ;
11
12
defaultPosition ?: SimplePosition ;
12
13
}
@@ -118,6 +119,7 @@ describe("Tooltipped", () => {
118
119
const onBlur = jest . fn ( ) ;
119
120
const onFocus = jest . fn ( ) ;
120
121
const onKeyDown = jest . fn ( ) ;
122
+ const onClick = jest . fn ( ) ;
121
123
const onTouchStart = jest . fn ( ) ;
122
124
const onContextMenu = jest . fn ( ) ;
123
125
const onMouseEnter = jest . fn ( ) ;
@@ -129,6 +131,7 @@ describe("Tooltipped", () => {
129
131
tooltip = "Look at me!"
130
132
onBlur = { onBlur }
131
133
onFocus = { onFocus }
134
+ onClick = { onClick }
132
135
onKeyDown = { onKeyDown }
133
136
onMouseEnter = { onMouseEnter }
134
137
onMouseLeave = { onMouseLeave }
@@ -158,6 +161,9 @@ describe("Tooltipped", () => {
158
161
fireEvent . touchStart ( button ) ;
159
162
expect ( onTouchStart ) . toBeCalledTimes ( 1 ) ;
160
163
164
+ fireEvent . click ( button ) ;
165
+ expect ( onClick ) . toBeCalledTimes ( 1 ) ;
166
+
161
167
fireEvent . contextMenu ( button ) ;
162
168
expect ( onContextMenu ) . toBeCalledTimes ( 1 ) ;
163
169
} ) ;
@@ -456,4 +462,56 @@ describe("Tooltipped", () => {
456
462
} ) ;
457
463
expect ( ( ) => getByRole ( "tooltip" ) ) . toThrow ( ) ;
458
464
} ) ;
465
+
466
+ it ( "should throw an error if a javascript user provides an invalid position" , ( ) => {
467
+ const error = jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => {
468
+ // do nothing
469
+ } ) ;
470
+
471
+ // @ts -expect-error
472
+ expect ( ( ) => render ( < Test position = "abvoe" /> ) ) . toThrow (
473
+ "Invalid position: abvoe"
474
+ ) ;
475
+
476
+ error . mockRestore ( ) ;
477
+ } ) ;
478
+
479
+ it ( "should cancel the tooltip timer if the element is clicked" , ( ) => {
480
+ const onClick = jest . fn ( ) . mockImplementationOnce ( ( event : MouseEvent ) => {
481
+ event . stopPropagation ( ) ;
482
+ } ) ;
483
+ const { getByRole } = render ( < Test onClick = { onClick } /> ) ;
484
+
485
+ const button = getByRole ( "button" ) ;
486
+ fireEvent . mouseEnter ( button ) ;
487
+ fireEvent . click ( button ) ;
488
+
489
+ act ( ( ) => {
490
+ jest . advanceTimersByTime ( 1000 ) ;
491
+ } ) ;
492
+ // event.stopPropagation was called
493
+ expect ( ( ) => getByRole ( "tooltip" ) ) . not . toThrow ( ) ;
494
+
495
+ fireEvent . mouseLeave ( button ) ;
496
+ act ( ( ) => {
497
+ jest . runAllTimers ( ) ;
498
+ } ) ;
499
+ expect ( ( ) => getByRole ( "tooltip" ) ) . toThrow ( ) ;
500
+
501
+ fireEvent . mouseEnter ( button ) ;
502
+ act ( ( ) => {
503
+ jest . advanceTimersByTime ( 500 ) ;
504
+ } ) ;
505
+ expect ( ( ) => getByRole ( "tooltip" ) ) . toThrow ( ) ;
506
+
507
+ fireEvent . click ( button ) ;
508
+ act ( ( ) => {
509
+ jest . advanceTimersByTime ( 500 ) ;
510
+ } ) ;
511
+ expect ( ( ) => getByRole ( "tooltip" ) ) . toThrow ( ) ;
512
+ act ( ( ) => {
513
+ jest . runAllTimers ( ) ;
514
+ } ) ;
515
+ expect ( ( ) => getByRole ( "tooltip" ) ) . toThrow ( ) ;
516
+ } ) ;
459
517
} ) ;
0 commit comments