@@ -5,7 +5,9 @@ import ClickAwayListener from '../src';
5
5
describe ( 'ClickAway Listener' , ( ) => {
6
6
it ( 'should render properly' , ( ) => {
7
7
const { getByText } = render (
8
- < ClickAwayListener onClickAway = { ( ) => null } > Hello World</ ClickAwayListener >
8
+ < ClickAwayListener onClickAway = { ( ) => null } >
9
+ Hello World
10
+ </ ClickAwayListener >
9
11
) ;
10
12
expect ( getByText ( / H e l l o W o r l d / i) ) . toBeTruthy ( ) ;
11
13
} ) ;
@@ -22,48 +24,82 @@ describe('ClickAway Listener', () => {
22
24
</ React . Fragment >
23
25
) ;
24
26
27
+ fireEvent . click ( getByText ( / A b u t t o n / i) ) ;
28
+ fireEvent . click ( getByText ( / A t e x t e l e m e n t / i) ) ;
29
+ fireEvent . click ( getByText ( / H e l l o W o r l d / i) ) ;
30
+ expect ( fakeHandleClick ) . toBeCalledTimes ( 2 ) ;
31
+ } ) ;
32
+
33
+ it ( 'works with different mouse events' , ( ) => {
34
+ const fakeHandleClick = jest . fn ( ) ;
35
+ const { getByText } = render (
36
+ < React . Fragment >
37
+ < ClickAwayListener onClickAway = { fakeHandleClick } mouseEvent = "mousedown" >
38
+ Hello World
39
+ </ ClickAwayListener >
40
+ < button > A button</ button >
41
+ < p > A text element</ p >
42
+ </ React . Fragment >
43
+ ) ;
44
+
25
45
fireEvent . mouseDown ( getByText ( / A b u t t o n / i) ) ;
26
46
fireEvent . mouseDown ( getByText ( / A t e x t e l e m e n t / i) ) ;
27
47
fireEvent . mouseDown ( getByText ( / H e l l o W o r l d / i) ) ;
28
48
expect ( fakeHandleClick ) . toBeCalledTimes ( 2 ) ;
29
49
} ) ;
30
50
51
+ it ( 'works with different touch events' , ( ) => {
52
+ const fakeHandleClick = jest . fn ( ) ;
53
+ const { getByText } = render (
54
+ < React . Fragment >
55
+ < ClickAwayListener onClickAway = { fakeHandleClick } touchEvent = "touchend" >
56
+ Hello World
57
+ </ ClickAwayListener >
58
+ < button > A button</ button >
59
+ < p > A text element</ p >
60
+ </ React . Fragment >
61
+ ) ;
62
+
63
+ fireEvent . touchEnd ( getByText ( / A b u t t o n / i) ) ;
64
+ fireEvent . touchEnd ( getByText ( / A t e x t e l e m e n t / i) ) ;
65
+ fireEvent . touchEnd ( getByText ( / H e l l o W o r l d / i) ) ;
66
+ expect ( fakeHandleClick ) . toBeCalledTimes ( 2 ) ;
67
+ } ) ;
68
+
31
69
it ( 'should handle multiple cases' , ( ) => {
32
70
const fakeHandleClick = jest . fn ( ) ;
33
71
const fakeHandleClick2 = jest . fn ( ) ;
34
72
const { getByTestId } = render (
35
73
< React . Fragment >
36
74
< ClickAwayListener onClickAway = { fakeHandleClick } >
37
- < div data-testid = "hello-world" >
38
- Hello World
39
- </ div >
75
+ < div data-testid = "hello-world" > Hello World</ div >
40
76
</ ClickAwayListener >
41
77
< button data-testid = "button-one" > A button</ button >
42
78
< button data-testid = "some-other-button-one" > Some other button</ button >
43
79
< p data-testid = "text-one" > A text element</ p >
44
80
45
81
< ClickAwayListener onClickAway = { fakeHandleClick2 } >
46
- < div data-testid = "foo-bar" >
47
- Foo bar
48
- </ div >
82
+ < div data-testid = "foo-bar" > Foo bar</ div >
49
83
</ ClickAwayListener >
50
84
< button data-testid = "button-two" > Foo bar button</ button >
51
- < button data-testid = "some-other-button-two" > Foo bar other button</ button >
85
+ < button data-testid = "some-other-button-two" >
86
+ Foo bar other button
87
+ </ button >
52
88
< p data-testid = "text-two" > Foo bar text element</ p >
53
89
</ React . Fragment >
54
90
) ;
55
91
56
- fireEvent . mouseDown ( getByTestId ( 'button-one' ) ) ;
57
- fireEvent . mouseDown ( getByTestId ( 'text-one' ) ) ;
58
- fireEvent . mouseDown ( getByTestId ( 'hello-world' ) ) ;
59
- fireEvent . mouseDown ( getByTestId ( 'some-other-button-one' ) ) ;
92
+ fireEvent . click ( getByTestId ( 'button-one' ) ) ;
93
+ fireEvent . click ( getByTestId ( 'text-one' ) ) ;
94
+ fireEvent . click ( getByTestId ( 'hello-world' ) ) ;
95
+ fireEvent . click ( getByTestId ( 'some-other-button-one' ) ) ;
60
96
expect ( fakeHandleClick ) . toBeCalledTimes ( 3 ) ;
61
97
62
98
// 4 from the previous ones, and the 3 new ones
63
- fireEvent . mouseDown ( getByTestId ( 'button-two' ) ) ;
64
- fireEvent . mouseDown ( getByTestId ( 'text-two' ) ) ;
65
- fireEvent . mouseDown ( getByTestId ( 'foo-bar' ) ) ;
66
- fireEvent . mouseDown ( getByTestId ( 'some-other-button-two' ) ) ;
99
+ fireEvent . click ( getByTestId ( 'button-two' ) ) ;
100
+ fireEvent . click ( getByTestId ( 'text-two' ) ) ;
101
+ fireEvent . click ( getByTestId ( 'foo-bar' ) ) ;
102
+ fireEvent . click ( getByTestId ( 'some-other-button-two' ) ) ;
67
103
expect ( fakeHandleClick2 ) . toBeCalledTimes ( 7 ) ;
68
104
} ) ;
69
105
} ) ;
0 commit comments