@@ -3,7 +3,6 @@ import ClickOutside from '../'
33import { wait } from '../../../../test'
44
55function bootstrap ( args ?: object ) {
6- let registeredHandler
76 const el = document . createElement ( 'div' )
87
98 const binding = {
@@ -13,8 +12,11 @@ function bootstrap (args?: object) {
1312 } ,
1413 }
1514
15+ let clickHandler
16+ let mousedownHandler
1617 jest . spyOn ( window . document . body , 'addEventListener' ) . mockImplementation ( ( eventName , eventHandler , options ) => {
17- registeredHandler = eventHandler
18+ if ( eventName === 'click' ) clickHandler = eventHandler
19+ if ( eventName === 'mousedown' ) mousedownHandler = eventHandler
1820 } )
1921 jest . spyOn ( window . document . body , 'removeEventListener' )
2022
@@ -23,72 +25,84 @@ function bootstrap (args?: object) {
2325 return {
2426 callback : binding . value . handler ,
2527 el : el as HTMLElement ,
26- registeredHandler,
28+ clickHandler,
29+ mousedownHandler,
2730 }
2831}
2932
30- describe ( 'click-outside.js ' , ( ) => {
33+ describe ( 'click-outside' , ( ) => {
3134 it ( 'should register and unregister handler' , ( ) => {
32- const { registeredHandler , el } = bootstrap ( )
33- expect ( window . document . body . addEventListener ) . toHaveBeenCalledWith ( 'click' , registeredHandler , true )
35+ const { clickHandler , el } = bootstrap ( )
36+ expect ( window . document . body . addEventListener ) . toHaveBeenCalledWith ( 'click' , clickHandler , true )
3437
3538 ClickOutside . unbind ( el )
36- expect ( window . document . body . removeEventListener ) . toHaveBeenCalledWith ( 'click' , registeredHandler , true )
39+ expect ( window . document . body . removeEventListener ) . toHaveBeenCalledWith ( 'click' , clickHandler , true )
3740 } )
3841
3942 it ( 'should call the callback when closeConditional returns true' , async ( ) => {
40- const { registeredHandler , callback } = bootstrap ( { closeConditional : ( ) => true } )
43+ const { clickHandler , callback } = bootstrap ( { closeConditional : ( ) => true } )
4144 const event = { target : document . createElement ( 'div' ) }
4245
43- registeredHandler ( event )
46+ clickHandler ( event )
4447 await wait ( )
4548 expect ( callback ) . toHaveBeenCalledWith ( event )
4649 } )
4750
4851 it ( 'should not call the callback when closeConditional returns false' , async ( ) => {
49- const { registeredHandler , callback, el } = bootstrap ( { closeConditional : ( ) => false } )
52+ const { clickHandler , callback, el } = bootstrap ( { closeConditional : ( ) => false } )
5053
51- registeredHandler ( { target : el } )
54+ clickHandler ( { target : el } )
5255 await wait ( )
5356 expect ( callback ) . not . toHaveBeenCalled ( )
5457 } )
5558
5659 it ( 'should not call the callback when closeConditional is not provided' , async ( ) => {
57- const { registeredHandler , callback, el } = bootstrap ( )
60+ const { clickHandler , callback, el } = bootstrap ( )
5861
59- registeredHandler ( { target : el } )
62+ clickHandler ( { target : el } )
6063 await wait ( )
6164 expect ( callback ) . not . toHaveBeenCalled ( )
6265 } )
6366
6467 it ( 'should not call the callback when clicked in element' , async ( ) => {
65- const { registeredHandler , callback, el } = bootstrap ( { closeConditional : ( ) => true } )
68+ const { clickHandler , callback, el } = bootstrap ( { closeConditional : ( ) => true } )
6669
67- registeredHandler ( { target : el } )
70+ clickHandler ( { target : el } )
6871 await wait ( )
6972 expect ( callback ) . not . toHaveBeenCalledWith ( )
7073 } )
7174
7275 it ( 'should not call the callback when clicked in elements' , async ( ) => {
73- const { registeredHandler , callback, el } = bootstrap ( {
76+ const { clickHandler , callback, el } = bootstrap ( {
7477 closeConditional : ( ) => true ,
7578 include : ( ) => [ el ] ,
7679 } )
7780
78- registeredHandler ( { target : document . createElement ( 'div' ) } )
81+ clickHandler ( { target : document . createElement ( 'div' ) } )
7982 await wait ( )
8083 expect ( callback ) . not . toHaveBeenCalledWith ( )
8184 } )
8285
8386 it ( 'should not call the callback when event is not fired by user action' , async ( ) => {
84- const { registeredHandler , callback } = bootstrap ( { closeConditional : ( ) => true } )
87+ const { clickHandler , callback } = bootstrap ( { closeConditional : ( ) => true } )
8588
86- registeredHandler ( { isTrusted : false } )
89+ clickHandler ( { isTrusted : false } )
8790 await wait ( )
8891 expect ( callback ) . not . toHaveBeenCalledWith ( )
8992
90- registeredHandler ( { pointerType : false } )
93+ clickHandler ( { pointerType : false } )
9194 await wait ( )
9295 expect ( callback ) . not . toHaveBeenCalledWith ( )
9396 } )
97+
98+ it ( 'should not call the callback when mousedown was on the element' , async ( ) => {
99+ const { clickHandler, mousedownHandler, callback, el } = bootstrap ( { closeConditional : ( ) => true } )
100+ const mousedownEvent = { target : el }
101+ const clickEvent = { target : document . createElement ( 'div' ) }
102+
103+ mousedownHandler ( mousedownEvent )
104+ clickHandler ( clickEvent )
105+ await wait ( )
106+ expect ( callback ) . not . toHaveBeenCalledWith ( clickEvent )
107+ } )
94108} )
0 commit comments