1
1
( function ( ) {
2
+ var key = jQuery . simulate . keyCode ,
3
+ clickOptions = [ "ctrlKey" , "altKey" , "shiftKey" , "metaKey" ] ,
4
+ keyEvents = [ "keydown" , "keyup" , "keypress" ] ,
5
+ keyOptions = [ "ctrlKey" , "altKey" , "shiftKey" , "metaKey" ] ;
2
6
3
7
module ( "mouse events" ) ;
4
8
@@ -25,28 +29,100 @@ test( "click on radio triggers change", function() {
25
29
notEqual ( checked , firstRadio . prop ( "checked" ) , "radio state changed" ) ;
26
30
} ) ;
27
31
28
- var key = jQuery . simulate . keyCode ,
29
- keyEvents = [ "keydown" , "keyup" , "keypress" ] ,
30
- i = 0 ;
32
+ test ( "click" , function ( ) {
33
+ expect ( 6 ) ;
34
+ jQuery ( "<div></div>" ) . bind ( "click" , function ( event ) {
35
+ ok ( true , "click event fired" ) ;
36
+ equal ( event . button , 0 , "click event was fired with left mouse button" ) ;
37
+ equal ( event . ctrlKey , false , "click event was fired without control key" ) ;
38
+ equal ( event . metaKey , false , "click event was fired without meta key" ) ;
39
+ equal ( event . shiftKey , false , "click event was fired without shift key" ) ;
40
+ equal ( event . altKey , false , "click event was fired without alt key" ) ;
41
+ } ) . appendTo ( "#qunit-fixture" ) . simulate ( "click" ) ;
42
+ } ) ;
43
+
44
+ test ( "click with middle mouse button" , function ( ) {
45
+ expect ( 2 ) ;
46
+ jQuery ( "<div></div>" ) . bind ( "click" , function ( event ) {
47
+ ok ( true , "click event fired" ) ;
48
+ equal ( event . button , 1 , "click event was fired with middle mouse button" ) ;
49
+ } ) . appendTo ( "#qunit-fixture" ) . simulate ( "click" , {
50
+ button : 1
51
+ } ) ;
52
+ } ) ;
53
+
54
+ test ( "click with right mouse button" , function ( ) {
55
+ expect ( 2 ) ;
56
+ jQuery ( "<div></div>" ) . bind ( "click" , function ( event ) {
57
+ ok ( true , "click event fired" ) ;
58
+ equal ( event . button , 2 , "click event was fired with right mouse button" ) ;
59
+ } ) . appendTo ( "#qunit-fixture" ) . simulate ( "click" , {
60
+ button : 2
61
+ } ) ;
62
+ } ) ;
63
+
64
+ function testClickEvent ( clickOption ) {
65
+ var options = { } ;
66
+ options [ clickOption ] = true ;
67
+
68
+ test ( "click with " + clickOption , function ( ) {
69
+ expect ( 2 ) ;
70
+ jQuery ( "<div></div>" ) . bind ( "click" , function ( event ) {
71
+ ok ( true , "click event fired" ) ;
72
+ equal ( event [ clickOption ] , true , "click event was fired with " + clickOption ) ;
73
+ } ) . appendTo ( "#qunit-fixture" ) . simulate ( "click" , options ) ;
74
+ } ) ;
75
+ }
76
+
77
+ clickOptions . forEach ( function ( clickOption ) {
78
+ testClickEvent ( clickOption ) ;
79
+ } ) ;
31
80
32
81
module ( "key events" ) ;
33
82
34
83
function testKeyEvent ( keyEvent ) {
35
84
test ( keyEvent , function ( ) {
36
- expect ( 2 ) ;
85
+ expect ( 2 + keyOptions . length ) ;
37
86
jQuery ( "<div></div>" ) . bind ( keyEvent , function ( event ) {
87
+ var i = 0 ;
88
+
38
89
ok ( true , keyEvent + " event fired" ) ;
39
90
equal ( event . keyCode , key . PAGE_UP , keyEvent + " event has correct keyCode" ) ;
91
+
92
+ for ( i ; i < keyOptions . length ; i ++ ) {
93
+ equal ( event [ keyOptions [ i ] ] , false , keyEvent + " event fired without " + keyOptions [ i ] ) ;
94
+ }
40
95
} ) . appendTo ( "#qunit-fixture" ) . simulate ( keyEvent , {
41
96
keyCode : key . PAGE_UP
42
97
} ) ;
43
98
} ) ;
44
99
}
45
100
46
- for ( ; i < keyEvents . length ; i ++ ) {
47
- testKeyEvent ( keyEvents [ i ] ) ;
101
+ function testKeyEventOption ( keyEvent , keyOption ) {
102
+ test ( keyEvent + " with " + keyOption , function ( ) {
103
+ var options = {
104
+ keyCode : key . PAGE_UP
105
+ } ;
106
+ options [ keyOption ] = true ;
107
+
108
+ expect ( 3 ) ;
109
+ jQuery ( "<div></div>" ) . bind ( keyEvent , function ( event ) {
110
+ ok ( true , keyEvent + " event fired" ) ;
111
+ equal ( event . keyCode , key . PAGE_UP , keyEvent + " event has correct keyCode" ) ;
112
+ equal ( event [ keyOption ] , true , keyEvent + " event fired with " + keyOption ) ;
113
+ } ) . appendTo ( "#qunit-fixture" ) . simulate ( keyEvent , options ) ;
114
+ } ) ;
48
115
}
49
116
117
+ keyEvents . forEach ( function ( keyEvent ) {
118
+ testKeyEvent ( keyEvent ) ;
119
+
120
+ keyOptions . forEach ( function ( keyOption ) {
121
+ testKeyEventOption ( keyEvent , keyOption ) ;
122
+ } ) ;
123
+ } ) ;
124
+
125
+
50
126
module ( "complex events" ) ;
51
127
52
128
asyncTest ( "drag moves option" , function ( ) {
@@ -98,4 +174,4 @@ asyncTest( "drag moves option", function() {
98
174
} ) ;
99
175
} ) ;
100
176
101
- } ) ( ) ;
177
+ } ) ( ) ;
0 commit comments