@@ -19,31 +19,14 @@ describe( 'KeystrokeHandler', () => {
1919
2020 describe ( 'listenTo()' , ( ) => {
2121 it ( 'activates the listening on the emitter' , ( ) => {
22- emitter = Object . create ( EmitterMixin ) ;
23- keystrokes = new KeystrokeHandler ( ) ;
24-
25- const spy = sinon . spy ( keystrokes , 'press' ) ;
26- const keyEvtData = { keyCode : 1 } ;
27-
28- emitter . fire ( 'keydown' , keyEvtData ) ;
29-
30- expect ( spy . notCalled ) . to . be . true ;
31-
32- keystrokes . listenTo ( emitter ) ;
33- emitter . fire ( 'keydown' , keyEvtData ) ;
34-
35- sinon . assert . calledOnce ( spy ) ;
36- sinon . assert . calledWithExactly ( spy , keyEvtData ) ;
37- } ) ;
38-
39- it ( 'triggers #press on #keydown' , ( ) => {
40- const spy = sinon . spy ( keystrokes , 'press' ) ;
41- const keyEvtData = { keyCode : 1 } ;
22+ const spy = sinon . spy ( ) ;
23+ const keyEvtData = getCtrlA ( ) ;
4224
25+ keystrokes . set ( 'Ctrl+A' , spy ) ;
4326 emitter . fire ( 'keydown' , keyEvtData ) ;
4427
4528 sinon . assert . calledOnce ( spy ) ;
46- sinon . assert . calledWithExactly ( spy , keyEvtData ) ;
29+ sinon . assert . calledWithExactly ( spy , keyEvtData , sinon . match . func ) ;
4730 } ) ;
4831 } ) ;
4932
@@ -52,7 +35,7 @@ describe( 'KeystrokeHandler', () => {
5235 const spy = sinon . spy ( ) ;
5336 const keyEvtData = getCtrlA ( ) ;
5437
55- keystrokes . set ( 'ctrl + A' , spy ) ;
38+ keystrokes . set ( 'Ctrl+ A' , spy ) ;
5639
5740 const wasHandled = keystrokes . press ( keyEvtData ) ;
5841
@@ -61,31 +44,6 @@ describe( 'KeystrokeHandler', () => {
6144 expect ( wasHandled ) . to . be . true ;
6245 } ) ;
6346
64- it ( 'provides a callback which both preventDefault and stopPropagation' , done => {
65- const keyEvtData = getCtrlA ( ) ;
66-
67- Object . assign ( keyEvtData , {
68- preventDefault : sinon . spy ( ) ,
69- stopPropagation : sinon . spy ( )
70- } ) ;
71-
72- keystrokes . set ( 'ctrl + A' , ( data , cancel ) => {
73- expect ( data ) . to . equal ( keyEvtData ) ;
74-
75- sinon . assert . notCalled ( keyEvtData . preventDefault ) ;
76- sinon . assert . notCalled ( keyEvtData . stopPropagation ) ;
77-
78- cancel ( ) ;
79-
80- sinon . assert . calledOnce ( keyEvtData . preventDefault ) ;
81- sinon . assert . calledOnce ( keyEvtData . stopPropagation ) ;
82-
83- done ( ) ;
84- } ) ;
85-
86- emitter . fire ( 'keydown' , keyEvtData ) ;
87- } ) ;
88-
8947 it ( 'returns false when no handler' , ( ) => {
9048 const keyEvtData = getCtrlA ( ) ;
9149
@@ -99,7 +57,7 @@ describe( 'KeystrokeHandler', () => {
9957 it ( 'handles array format' , ( ) => {
10058 const spy = sinon . spy ( ) ;
10159
102- keystrokes . set ( [ 'ctrl ' , 'A' ] , spy ) ;
60+ keystrokes . set ( [ 'Ctrl ' , 'A' ] , spy ) ;
10361
10462 expect ( keystrokes . press ( getCtrlA ( ) ) ) . to . be . true ;
10563 } ) ;
@@ -108,14 +66,70 @@ describe( 'KeystrokeHandler', () => {
10866 const spy1 = sinon . spy ( ) ;
10967 const spy2 = sinon . spy ( ) ;
11068
111- keystrokes . set ( [ 'ctrl ' , 'A' ] , spy1 ) ;
112- keystrokes . set ( [ 'ctrl ' , 'A' ] , spy2 ) ;
69+ keystrokes . set ( [ 'Ctrl ' , 'A' ] , spy1 ) ;
70+ keystrokes . set ( [ 'Ctrl ' , 'A' ] , spy2 ) ;
11371
11472 keystrokes . press ( getCtrlA ( ) ) ;
11573
11674 sinon . assert . calledOnce ( spy1 ) ;
11775 sinon . assert . calledOnce ( spy2 ) ;
11876 } ) ;
77+
78+ it ( 'supports priorities' , ( ) => {
79+ const spy1 = sinon . spy ( ) ;
80+ const spy2 = sinon . spy ( ) ;
81+ const spy3 = sinon . spy ( ) ;
82+ const spy4 = sinon . spy ( ) ;
83+
84+ keystrokes . set ( [ 'Ctrl' , 'A' ] , spy1 ) ;
85+ keystrokes . set ( [ 'Ctrl' , 'A' ] , spy2 , { priority : 'high' } ) ;
86+ keystrokes . set ( [ 'Ctrl' , 'A' ] , spy3 , { priority : 'low' } ) ;
87+ keystrokes . set ( [ 'Ctrl' , 'A' ] , spy4 ) ;
88+
89+ keystrokes . press ( getCtrlA ( ) ) ;
90+
91+ sinon . assert . callOrder ( spy2 , spy1 , spy4 , spy3 ) ;
92+ } ) ;
93+
94+ it ( 'provides a callback which causes preventDefault and stopPropagation in the DOM' , done => {
95+ const keyEvtData = getCtrlA ( ) ;
96+
97+ keystrokes . set ( 'Ctrl+A' , ( data , cancel ) => {
98+ expect ( data ) . to . equal ( keyEvtData ) ;
99+
100+ sinon . assert . notCalled ( keyEvtData . preventDefault ) ;
101+ sinon . assert . notCalled ( keyEvtData . stopPropagation ) ;
102+
103+ cancel ( ) ;
104+
105+ sinon . assert . calledOnce ( keyEvtData . preventDefault ) ;
106+ sinon . assert . calledOnce ( keyEvtData . stopPropagation ) ;
107+
108+ done ( ) ;
109+ } ) ;
110+
111+ emitter . fire ( 'keydown' , keyEvtData ) ;
112+ } ) ;
113+
114+ it ( 'provides a callback which stops the event and remaining callbacks in the keystroke handler' , ( ) => {
115+ const spy1 = sinon . spy ( ) ;
116+ const spy2 = sinon . spy ( ) ;
117+ const spy3 = sinon . spy ( ) ;
118+ const spy4 = sinon . spy ( ) ;
119+
120+ keystrokes . set ( [ 'Ctrl' , 'A' ] , spy1 ) ;
121+ keystrokes . set ( [ 'Ctrl' , 'A' ] , spy2 , { priority : 'high' } ) ;
122+ keystrokes . set ( [ 'Ctrl' , 'A' ] , spy3 , { priority : 'low' } ) ;
123+ keystrokes . set ( [ 'Ctrl' , 'A' ] , ( keyEvtData , cancel ) => {
124+ spy4 ( ) ;
125+ cancel ( ) ;
126+ } ) ;
127+
128+ keystrokes . press ( getCtrlA ( ) ) ;
129+
130+ sinon . assert . callOrder ( spy2 , spy1 , spy4 ) ;
131+ sinon . assert . notCalled ( spy3 ) ;
132+ } ) ;
119133 } ) ;
120134
121135 describe ( 'destroy()' , ( ) => {
@@ -133,7 +147,7 @@ describe( 'KeystrokeHandler', () => {
133147 const spy = sinon . spy ( ) ;
134148 const keystrokeHandler = keystrokes ;
135149
136- keystrokeHandler . set ( 'ctrl + A' , spy ) ;
150+ keystrokeHandler . set ( 'Ctrl+ A' , spy ) ;
137151
138152 keystrokeHandler . destroy ( ) ;
139153
@@ -146,5 +160,10 @@ describe( 'KeystrokeHandler', () => {
146160} ) ;
147161
148162function getCtrlA ( ) {
149- return { keyCode : keyCodes . a , ctrlKey : true } ;
163+ return {
164+ keyCode : keyCodes . a ,
165+ ctrlKey : true ,
166+ preventDefault : sinon . spy ( ) ,
167+ stopPropagation : sinon . spy ( )
168+ } ;
150169}
0 commit comments