r114114 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114113‎ | r114114 | r114115 >
Date:21:40, 18 March 2012
Author:krinkle
Status:ok
Tags:
Comment:
[jquery.byteLimit.test] clean up
* js conventions
* whitespace indention
Modified paths:
  • /trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js
@@ -1,187 +1,205 @@
2 -( function () {
 2+( function ( $ ) {
 3+ var simpleSample, U_20AC, mbSample;
34
4 -module( 'jquery.byteLimit', QUnit.newMwEnvironment() );
 5+ module( 'jquery.byteLimit', QUnit.newMwEnvironment() );
56
6 -test( '-- Initial check', function() {
7 - expect(1);
8 - ok( $.fn.byteLimit, 'jQuery.fn.byteLimit defined' );
9 -} );
 7+ // Simple sample (20 chars, 20 bytes)
 8+ simpleSample = '12345678901234567890';
109
11 -// Basic sendkey-implementation
12 -$.addChars = function( $input, charstr ) {
13 - var len = charstr.length;
14 - for ( var i = 0; i < len; i++ ) {
15 - // Keep track of the previous value
16 - var prevVal = $input.val();
 10+ // 3 bytes (euro-symbol)
 11+ U_20AC = '\u20AC';
1712
18 - // Get the key code
19 - var code = charstr.charCodeAt(i);
 13+ // Multi-byte sample (22 chars, 26 bytes)
 14+ mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
2015
21 - // Trigger event and undo if prevented
22 - var event = new jQuery.Event( 'keypress', { keyCode: code, which: code, charCode: code } );
23 - $input.trigger( event );
24 - if ( !event.isDefaultPrevented() ) {
25 - $input.val( prevVal + charstr.charAt(i) );
 16+ // Basic sendkey-implementation
 17+ function addChars( $input, charstr ) {
 18+ var len, i, prevVal, code, event;
 19+ len = charstr.length;
 20+ for ( i = 0; i < len; i += 1 ) {
 21+ // Keep track of the previous value
 22+ prevVal = $input.val();
 23+
 24+ // Get the key code
 25+ code = charstr.charCodeAt( i );
 26+
 27+ // Trigger event and undo if prevented
 28+ event = new jQuery.Event( 'keypress', {
 29+ which: code,
 30+ keyCode: code,
 31+ charCode: code
 32+ } );
 33+ $input.trigger( event );
 34+ if ( !event.isDefaultPrevented() ) {
 35+ $input.val( prevVal + charstr.charAt( i ) );
 36+ }
2637 }
2738 }
28 -};
2939
30 -/**
31 - * Test factory for $.fn.byteLimit
32 - *
33 - * @param $input {jQuery} jQuery object in an input element
34 - * @param hasLimit {Boolean} Wether a limit should apply at all
35 - * @param limit {Number} Limit (if used) otherwise undefined
36 - * The limit should be less than 20 (the sample data's length)
37 - */
38 -var byteLimitTest = function( options ) {
39 - var opt = $.extend({
40 - description: '',
41 - $input: null,
42 - sample: '',
43 - hasLimit: false,
44 - expected: '',
45 - limit: null
46 - }, options);
 40+ /**
 41+ * Test factory for $.fn.byteLimit
 42+ *
 43+ * @param $input {jQuery} jQuery object in an input element
 44+ * @param hasLimit {Boolean} Wether a limit should apply at all
 45+ * @param limit {Number} Limit (if used) otherwise undefined
 46+ * The limit should be less than 20 (the sample data's length)
 47+ */
 48+ function byteLimitTest( options ) {
 49+ var opt = $.extend({
 50+ description: '',
 51+ $input: null,
 52+ sample: '',
 53+ hasLimit: false,
 54+ expected: '',
 55+ limit: null
 56+ }, options);
4757
48 - test( opt.description, function() {
 58+ test( opt.description, function () {
 59+ var rawVal, fn, newVal;
4960
50 - opt.$input.appendTo( '#qunit-fixture' );
 61+ opt.$input.appendTo( '#qunit-fixture' );
5162
52 - // Simulate pressing keys for each of the sample characters
53 - $.addChars( opt.$input, opt.sample );
54 - var rawVal = opt.$input.val(),
55 - fn = opt.$input.data( 'byteLimit-callback' ),
 63+ // Simulate pressing keys for each of the sample characters
 64+ addChars( opt.$input, opt.sample );
 65+ rawVal = opt.$input.val();
 66+ fn = opt.$input.data( 'byteLimit-callback' );
5667 newVal = $.isFunction( fn ) ? fn( rawVal ) : rawVal;
5768
58 - if ( opt.hasLimit ) {
59 - expect(3);
 69+ if ( opt.hasLimit ) {
 70+ expect(3);
6071
61 - ltOrEq( $.byteLength( newVal ), opt.limit, 'Prevent keypresses after byteLimit was reached, length never exceeded the limit' );
62 - equal( $.byteLength( rawVal ), $.byteLength( opt.expected ), 'Not preventing keypresses too early, length has reached the expected length' );
63 - equal( rawVal, opt.expected, 'New value matches the expected string' );
 72+ QUnit.ltOrEq(
 73+ $.byteLength( newVal ),
 74+ opt.limit,
 75+ 'Prevent keypresses after byteLimit was reached, length never exceeded the limit'
 76+ );
 77+ equal(
 78+ $.byteLength( rawVal ),
 79+ $.byteLength( opt.expected ),
 80+ 'Not preventing keypresses too early, length has reached the expected length'
 81+ );
 82+ equal( rawVal, opt.expected, 'New value matches the expected string' );
6483
65 - } else {
66 - expect(2);
67 - equal( newVal, opt.expected, 'New value matches the expected string' );
68 - equal( $.byteLength( newVal ), $.byteLength( opt.expected ), 'Unlimited scenarios are not affected, expected length reached' );
69 - }
 84+ } else {
 85+ expect(2);
 86+ equal( newVal, opt.expected, 'New value matches the expected string' );
 87+ equal(
 88+ $.byteLength( newVal ),
 89+ $.byteLength( opt.expected ),
 90+ 'Unlimited scenarios are not affected, expected length reached'
 91+ );
 92+ }
 93+ } );
 94+ }
 95+
 96+ test( '-- Initial check', function () {
 97+ expect(1);
 98+ ok( $.fn.byteLimit, 'jQuery.fn.byteLimit defined' );
7099 } );
71 -};
72100
73 -var
74 - // Simple sample (20 chars, 20 bytes)
75 - simpleSample = '12345678901234567890',
 101+ byteLimitTest({
 102+ description: 'Plain text input',
 103+ $input: $( '<input>' )
 104+ .attr( 'type', 'text' ),
 105+ sample: simpleSample,
 106+ hasLimit: false,
 107+ expected: simpleSample
 108+ });
76109
77 - // 3 bytes (euro-symbol)
78 - U_20AC = '\u20AC',
 110+ byteLimitTest({
 111+ description: 'Limit using the maxlength attribute',
 112+ $input: $( '<input>' )
 113+ .attr( 'type', 'text' )
 114+ .prop( 'maxLength', '10' )
 115+ .byteLimit(),
 116+ sample: simpleSample,
 117+ hasLimit: true,
 118+ limit: 10,
 119+ expected: '1234567890'
 120+ });
79121
80 - // Multi-byte sample (22 chars, 26 bytes)
81 - mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
 122+ byteLimitTest({
 123+ description: 'Limit using a custom value',
 124+ $input: $( '<input>' )
 125+ .attr( 'type', 'text' )
 126+ .byteLimit( 10 ),
 127+ sample: simpleSample,
 128+ hasLimit: true,
 129+ limit: 10,
 130+ expected: '1234567890'
 131+ });
82132
83 -byteLimitTest({
84 - description: 'Plain text input',
85 - $input: $( '<input>' )
86 - .attr( 'type', 'text' ),
87 - sample: simpleSample,
88 - hasLimit: false,
89 - expected: simpleSample
90 -});
 133+ byteLimitTest({
 134+ description: 'Limit using a custom value, overriding maxlength attribute',
 135+ $input: $( '<input>' )
 136+ .attr( 'type', 'text' )
 137+ .prop( 'maxLength', '10' )
 138+ .byteLimit( 15 ),
 139+ sample: simpleSample,
 140+ hasLimit: true,
 141+ limit: 15,
 142+ expected: '123456789012345'
 143+ });
91144
92 -byteLimitTest({
93 - description: 'Limit using the maxlength attribute',
94 - $input: $( '<input>' )
95 - .attr( 'type', 'text' )
96 - .prop( 'maxLength', '10' )
97 - .byteLimit(),
98 - sample: simpleSample,
99 - hasLimit: true,
100 - limit: 10,
101 - expected: '1234567890'
102 -});
 145+ byteLimitTest({
 146+ description: 'Limit using a custom value (multibyte)',
 147+ $input: $( '<input>' )
 148+ .attr( 'type', 'text' )
 149+ .byteLimit( 14 ),
 150+ sample: mbSample,
 151+ hasLimit: true,
 152+ limit: 14,
 153+ expected: '1234567890' + U_20AC + '1'
 154+ });
103155
104 -byteLimitTest({
105 - description: 'Limit using a custom value',
106 - $input: $( '<input>' )
107 - .attr( 'type', 'text' )
108 - .byteLimit( 10 ),
109 - sample: simpleSample,
110 - hasLimit: true,
111 - limit: 10,
112 - expected: '1234567890'
113 -});
 156+ byteLimitTest({
 157+ description: 'Limit using a custom value (multibyte) overlapping a byte',
 158+ $input: $( '<input>' )
 159+ .attr( 'type', 'text' )
 160+ .byteLimit( 12 ),
 161+ sample: mbSample,
 162+ hasLimit: true,
 163+ limit: 12,
 164+ expected: '1234567890' + '12'
 165+ });
114166
115 -byteLimitTest({
116 - description: 'Limit using a custom value, overriding maxlength attribute',
117 - $input: $( '<input>' )
118 - .attr( 'type', 'text' )
119 - .prop( 'maxLength', '10' )
120 - .byteLimit( 15 ),
121 - sample: simpleSample,
122 - hasLimit: true,
123 - limit: 15,
124 - expected: '123456789012345'
125 -});
 167+ byteLimitTest({
 168+ description: 'Pass the limit and a callback as input filter',
 169+ $input: $( '<input>' )
 170+ .attr( 'type', 'text' )
 171+ .byteLimit( 6, function ( val ) {
 172+ // Invalid title
 173+ if ( val === '' ) {
 174+ return '';
 175+ }
126176
127 -byteLimitTest({
128 - description: 'Limit using a custom value (multibyte)',
129 - $input: $( '<input>' )
130 - .attr( 'type', 'text' )
131 - .byteLimit( 14 ),
132 - sample: mbSample,
133 - hasLimit: true,
134 - limit: 14,
135 - expected: '1234567890' + U_20AC + '1'
136 -});
 177+ // Return without namespace prefix
 178+ return new mw.Title( String( val ) ).getMain();
 179+ } ),
 180+ sample: 'User:Sample',
 181+ hasLimit: true,
 182+ limit: 6, // 'Sample' length
 183+ expected: 'User:Sample'
 184+ });
137185
138 -byteLimitTest({
139 - description: 'Limit using a custom value (multibyte) overlapping a byte',
140 - $input: $( '<input>' )
141 - .attr( 'type', 'text' )
142 - .byteLimit( 12 ),
143 - sample: mbSample,
144 - hasLimit: true,
145 - limit: 12,
146 - expected: '1234567890' + '12'
147 -});
 186+ byteLimitTest({
 187+ description: 'Limit using the maxlength attribute and pass a callback as input filter',
 188+ $input: $( '<input>' )
 189+ .attr( 'type', 'text' )
 190+ .prop( 'maxLength', '6' )
 191+ .byteLimit( function ( val ) {
 192+ // Invalid title
 193+ if ( val === '' ) {
 194+ return '';
 195+ }
148196
149 -byteLimitTest({
150 - description: 'Pass the limit and a callback as input filter',
151 - $input: $( '<input>' )
152 - .attr( 'type', 'text' )
153 - .byteLimit( 6, function( val ) {
154 - // Invalid title
155 - if ( val == '' ) {
156 - return '';
157 - }
 197+ // Return without namespace prefix
 198+ return new mw.Title( String( val ) ).getMain();
 199+ } ),
 200+ sample: 'User:Sample',
 201+ hasLimit: true,
 202+ limit: 6, // 'Sample' length
 203+ expected: 'User:Sample'
 204+ });
158205
159 - // Return without namespace prefix
160 - return new mw.Title( '' + val ).getMain();
161 - } ),
162 - sample: 'User:Sample',
163 - hasLimit: true,
164 - limit: 6, // 'Sample' length
165 - expected: 'User:Sample'
166 -});
167 -
168 -byteLimitTest({
169 - description: 'Limit using the maxlength attribute and pass a callback as input filter',
170 - $input: $( '<input>' )
171 - .attr( 'type', 'text' )
172 - .prop( 'maxLength', '6' )
173 - .byteLimit( function( val ) {
174 - // Invalid title
175 - if ( val === '' ) {
176 - return '';
177 - }
178 -
179 - // Return without namespace prefix
180 - return new mw.Title( '' + val ).getMain();
181 - } ),
182 - sample: 'User:Sample',
183 - hasLimit: true,
184 - limit: 6, // 'Sample' length
185 - expected: 'User:Sample'
186 -});
187 -
188 -}() );
\ No newline at end of file
 206+}( jQuery ) );
\ No newline at end of file

Status & tagging log