Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Mask: Adding more unit tests copied from the specs used for maskedInput

  • Loading branch information...
commit 719aae5bac73819a85b7f574e790ce543b3ce5f4 1 parent 96d7949
Corey Frang authored August 01, 2011
187  tests/unit/mask/mask_events.js
@@ -2,46 +2,179 @@
2 2
 
3 3
 module( "mask: events" );
4 4
 
5  
-test( "keydown: delete and backspace behaviors", function() {
  5
+/* TODO: Descide behavior of bluring non-valid inputs */
  6
+
  7
+test( "focus: Initial Caret Positioning", function() {
  8
+	var input = $( "#mask1" ).val("").mask({ mask: "9" }),
  9
+		mask = input.data( "mask" );
  10
+
  11
+	equal( input.val(), "_", "Initial Value Expected" );
  12
+	input.focus();
  13
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  14
+
  15
+	input.mask( "option", "mask", "(9)" );
  16
+	equal( input.val(), "(_)", "Initial Value Expected" );
  17
+	input.focus();
  18
+	deepEqual( mask._caret(), { begin: 1, end: 1 }, "Caret position correct");
  19
+
  20
+});
  21
+
  22
+test( "keydown: Backspace pulls values from right", function() {
6 23
 	expect( 12 );
7  
-	var input = $( "#mask1" ).val( "1234" ).mask({ mask: "99/99/99" }),
  24
+	var input = $( "#mask1" ).val("123").mask({ mask: "999" }),
8 25
 		mask = input.data( "mask" );
9 26
 
10  
-	equal( input.val(), "12/34/__", "Initial Value" );
11  
-	mask._caret( 0 );
12  
-	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
13  
-	equal( input.val(), "23/4_/__", "Deleted first value" );
  27
+	equal( input.val(), "123", "Initial Value Expected" );
  28
+
14 29
 	mask._caret( 2 );
15  
-	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
16  
-	equal( input.val(), "23/__/__", "Deleted value after literal" );
  30
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
  31
+	equal( input.val(), "13_", "Backspaced the second character");
  32
+	deepEqual( mask._caret(), { begin: 1, end: 1 }, "Caret position correct");
17 33
 
18  
-	input.val( "123456" ).mask( "refresh" );
19  
-	equal( input.val(), "12/34/56", "New Initial Value" );
20  
-	mask._caret( 2, 4 );
21  
-	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
22  
-	equal( input.val(), "12/56/__", "Deleted 34 out of the middle" );
23  
-	deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position" );
24  
-	
25  
-	input.val( "123456" ).mask( "refresh" );
26  
-	equal( input.val(), "12/34/56", "New Initial Value" );
27  
-	mask._caret( 4 );
  34
+	input.val( "1z" ).mask( "option", "mask", "9a" );
  35
+	equal( input.val(), "1z", "Initial Value Expected" );
  36
+
  37
+	mask._caret( 1 );
28 38
 	input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
29  
-	equal( input.val(), "12/45/6_", "Backspaced 3 out of the middle" );
30  
-	
  39
+	equal( input.val(), "_z", "Backspace did not pull value because it wasn't valid" );
  40
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  41
+
  42
+	input.val( "12" ).mask( "option", "mask", "9-9" );
  43
+	equal( input.val(), "1-2", "Initial Value Expected" );
31 44
 
32  
-	mask._caret( 2, 4 );
  45
+	mask._caret( 1 );
33 46
 	input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
34  
-	equal( input.val(), "12/6_/__", "Backspaced 45 out of the middle" );
35  
-	deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position" );
36  
-	
37  
-	mask._caret( 6 );
  47
+	equal( input.val(), "2-_", "Backspace pulled value because it was valid" );
  48
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  49
+
  50
+
  51
+	input.val( "1z" ).mask( "option", "mask", "9-a" );
  52
+	equal( input.val(), "1-z", "Initial Value Expected" );
  53
+
  54
+	mask._caret( 1 );
  55
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
  56
+	equal( input.val(), "_-z", "Backspace did not pull value because it wasn't valid" );
  57
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  58
+});
  59
+
  60
+test( "keydown: Backspace with the cursor to the right of a mask literal", function() {
  61
+	expect( 6 );
  62
+	var input = $( "#mask1" ).val("123").mask({ mask: "9-99" }),
  63
+		mask = input.data( "mask" );
  64
+
  65
+	equal( input.val(), "1-23", "Initial Value Expected" );
  66
+
  67
+	mask._caret( 2 );
38 68
 	input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
39  
-	equal( input.val(), "12/6_/__", "Backspaced last value" );
  69
+	equal( input.val(), "2-3_", "Backspaced across the literal -, brought values with");
  70
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  71
+
  72
+	input.val("z12").mask( "option", "mask", "a-99" );
  73
+	equal( input.val(), "z-12", "New Initial Value Expected");
40 74
 
41 75
 	mask._caret( 2 );
42 76
 	input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
43  
-	equal( input.val(), "16/__/__", "Backspaced value from middle" );
  77
+	equal( input.val(), "_-12", "Backspaced across the literal -, values held position");
  78
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  79
+});
  80
+
  81
+test( "keydown: Delete pulling values", function() {
  82
+	expect( 18 );
  83
+	var input = $( "#mask1" ).val("123").mask({ mask: "9-99" }),
  84
+		mask = input.data( "mask" );
  85
+	equal( input.val(), "1-23", "Initial value expected" );
  86
+
  87
+	mask._caret( 1 );
  88
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
  89
+	equal( input.val(), "1-3_", "Delete across the literal -, brought values with");
  90
+	deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position correct");
  91
+
  92
+	input.val("12z").mask( "option", "mask", "9-9a" );
  93
+	equal( input.val(), "1-2z", "Initial value expected" );
  94
+
  95
+	mask._caret( 1 );
  96
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
  97
+	equal( input.val(), "1-_z", "Deleted across the literal -, z was not pulled");
  98
+	deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position correct");
44 99
 
  100
+	input.val("12").mask( "option", "mask", "99" );
  101
+	equal( input.val(), "12", "Initial value expected" );
  102
+
  103
+	mask._caret( 0 );
  104
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
  105
+	equal( input.val(), "2_", "Deleted value, pulled values from the right");
  106
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  107
+
  108
+	input.val("1z").mask( "option", "mask", "9a" );
  109
+	equal( input.val(), "1z", "Initial value expected" );
  110
+
  111
+	mask._caret( 0 );
  112
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
  113
+	equal( input.val(), "_z", "Deleted value, couldn't pull values from the right");
  114
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  115
+
  116
+	input.val("12").mask( "option", "mask", "9-9" );
  117
+	equal( input.val(), "1-2", "Initial value expected" );
  118
+
  119
+	mask._caret( 0 );
  120
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
  121
+	equal( input.val(), "2-_", "Deleted value, pulled values from the right across the literal");
  122
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  123
+
  124
+
  125
+	input.val("1z").mask( "option", "mask", "9-a" );
  126
+	equal( input.val(), "1-z", "Initial value expected" );
  127
+
  128
+	mask._caret( 0 );
  129
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
  130
+	equal( input.val(), "_-z", "Deleted value, couldn't pull values from the right");
  131
+	deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
  132
+});
  133
+
  134
+test( "keydown: escape returns to original value", function() {
  135
+	expect( 3 );
  136
+	var input = $( "#mask1" ).val("6").mask({ mask: "9" }),
  137
+		mask = input.data( "mask" );
  138
+
  139
+	equal( input.val(), "6", "Initial value expected" );
  140
+	input.focus();
  141
+
  142
+	mask._caret( 0 );
  143
+	input.simulate( "keypress", { keyCode: "1".charCodeAt(0) });
  144
+	equal( input.val(), "1", "Typed over" );
  145
+
  146
+	input.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE });
  147
+	equal( input.val(), "6", "Reverted value after pressing escape" );
  148
+
  149
+});
  150
+
  151
+test( "keypress: typing behaviors", function() {
  152
+	expect( 8 );
  153
+	var input = $( "#mask1" ).mask({ mask: "9-9" }),
  154
+		mask = input.data( "mask" );
  155
+
  156
+	equal( input.val(), "_-_", "Initial value expected" );
  157
+
  158
+	mask._caret( 0 );
  159
+	input.simulate( "keypress", { keyCode: "1".charCodeAt( 0 ) });
  160
+	equal( input.val(), "1-_", "Typed a 1" );
  161
+
  162
+	mask._caret( 0 );
  163
+	input.simulate( "keypress", { keyCode: "2".charCodeAt( 0 ) });
  164
+	equal( input.val(), "2-1", "Typed a 2 before the 1" );
  165
+	deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position correct");
  166
+	
  167
+	input.val("").mask( "option", "mask", "9-a" );
  168
+	equal( input.val(), "_-_", "Initial value expected" );
  169
+
  170
+	mask._caret( 0 );
  171
+	input.simulate( "keypress", { keyCode: "1".charCodeAt( 0 ) });
  172
+	equal( input.val(), "1-_", "Typed a 1" );
  173
+
  174
+	mask._caret( 0 );
  175
+	input.simulate( "keypress", { keyCode: "2".charCodeAt( 0 ) });
  176
+	equal( input.val(), "2-_", "Typed a 2 before the 1 - 1 is lost because not valid" );
  177
+	deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position correct");
45 178
 });
46 179
 
47 180
 }( jQuery ) );
5  ui/jquery.ui.mask.js
@@ -137,6 +137,7 @@ $.widget( "ui.mask", {
137 137
 		this._bind({
138 138
 			focus: function( event ) {
139 139
 				lastUnsavedValue = elem.val();
  140
+				that._caret( that._seekRight( that._parseValue() - 1 ) );
140 141
 			},
141 142
 			blur: function( event ) {
142 143
 
@@ -176,7 +177,7 @@ $.widget( "ui.mask", {
176 177
 				}
177 178
 			},
178 179
 			keypress: function( event ) {
179  
-				var key = event.which,
  180
+				var key = event.keyCode,
180 181
 					position = that._caret(),
181 182
 					bufferPosition = that._seekRight( position.begin - 1 ),
182 183
 					bufferObject = that.buffer[ bufferPosition ];
@@ -312,8 +313,6 @@ $.widget( "ui.mask", {
312 313
 					if ( this._validValue( bufferObject, character ) ) {
313 314
 						valuePosition++;
314 315
 					}
315  
-					lastFilledPosition = bufferPosition;
316  
-
317 316
 					// when parsing a literal from a raw .val() if it doesn't match,
318 317
 					// assume that the literal is missing from the val()
319 318
 					break;

0 notes on commit 719aae5

Please sign in to comment.
Something went wrong with that request. Please try again.