QUnit: fix swapped expected/actual fields
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.byteLimit.test.js
index c21844e..22d2af1 100644 (file)
        /**
         * Test factory for $.fn.byteLimit
         *
-        * @param $input {jQuery} jQuery object in an input element
-        * @param hasLimit {Boolean} Wether a limit should apply at all
-        * @param limit {Number} Limit (if used) otherwise undefined
-        * The limit should be less than 20 (the sample data's length)
+        * @param {Object} options
+        * @param {string} options.description Test name
+        * @param {jQuery} options.$input jQuery object in an input element
+        * @param {string} options.sample Sequence of characters to simulate being
+        *  added one by one
+        * @param {string} options.expected Expected final value of `$input`
         */
        function byteLimitTest( options ) {
                var opt = $.extend( {
                        description: '',
                        $input: null,
                        sample: '',
-                       hasLimit: false,
-                       expected: '',
-                       limit: null
+                       expected: ''
                }, options );
 
-               QUnit.asyncTest( opt.description, opt.hasLimit ? 3 : 2, function ( assert ) {
+               QUnit.asyncTest( opt.description, 1, function ( assert ) {
                        setTimeout( function () {
-                               var rawVal, fn, effectiveVal;
-
                                opt.$input.appendTo( '#qunit-fixture' );
 
                                // Simulate pressing keys for each of the sample characters
                                addChars( opt.$input, opt.sample );
 
-                               rawVal = opt.$input.val();
-                               fn = opt.$input.data( 'byteLimit.callback' );
-                               effectiveVal = fn ? fn( rawVal ) : rawVal;
-
-                               if ( opt.hasLimit ) {
-                                       assert.ltOrEq(
-                                               $.byteLength( effectiveVal ),
-                                               opt.limit,
-                                               'Prevent keypresses after byteLimit was reached, length never exceeded the limit'
-                                       );
-                                       assert.equal(
-                                               $.byteLength( rawVal ),
-                                               $.byteLength( opt.expected ),
-                                               'Not preventing keypresses too early, length has reached the expected length'
-                                       );
-                                       assert.equal( rawVal, opt.expected, 'New value matches the expected string' );
-
-                               } else {
-                                       assert.equal(
-                                               $.byteLength( effectiveVal ),
-                                               $.byteLength( opt.expected ),
-                                               'Unlimited scenarios are not affected, expected length reached'
-                                       );
-                                       assert.equal( rawVal, opt.expected, 'New value matches the expected string' );
-                               }
+                               assert.equal(
+                                       opt.$input.val(),
+                                       opt.expected,
+                                       'New value matches the expected string'
+                               );
+
                                QUnit.start();
                        }, 10 );
                } );
@@ -89,7 +68,6 @@
                description: 'Plain text input',
                $input: $( '<input type="text"/>' ),
                sample: simpleSample,
-               hasLimit: false,
                expected: simpleSample
        } );
 
@@ -98,7 +76,6 @@
                $input: $( '<input type="text"/>' )
                        .byteLimit(),
                sample: simpleSample,
-               hasLimit: false,
                expected: simpleSample
        } );
 
                        .attr( 'maxlength', '10' )
                        .byteLimit(),
                sample: simpleSample,
-               hasLimit: true,
-               limit: 10,
                expected: '1234567890'
        } );
 
                $input: $( '<input type="text"/>' )
                        .byteLimit( 10 ),
                sample: simpleSample,
-               hasLimit: true,
-               limit: 10,
                expected: '1234567890'
        } );
 
                        .attr( 'maxlength', '10' )
                        .byteLimit( 15 ),
                sample: simpleSample,
-               hasLimit: true,
-               limit: 15,
                expected: '123456789012345'
        } );
 
                $input: $( '<input type="text"/>' )
                        .byteLimit( 14 ),
                sample: mbSample,
-               hasLimit: true,
-               limit: 14,
                expected: '1234567890' + U_20AC + '1'
        } );
 
                $input: $( '<input type="text"/>' )
                        .byteLimit( 12 ),
                sample: mbSample,
-               hasLimit: true,
-               limit: 12,
                expected: '1234567890' + '12'
        } );
 
                description: 'Pass the limit and a callback as input filter',
                $input: $( '<input type="text"/>' )
                        .byteLimit( 6, function ( val ) {
-                               // Invalid title
-                               if ( val === '' ) {
-                                       return '';
-                               }
-
+                               var title = mw.Title.newFromText( String( val ) );
                                // Return without namespace prefix
-                               return new mw.Title( String( val ) ).getMain();
+                               return title ? title.getMain() : '';
                        } ),
                sample: 'User:Sample',
-               hasLimit: true,
-               limit: 6, // 'Sample' length
                expected: 'User:Sample'
        } );
 
                $input: $( '<input type="text"/>' )
                        .attr( 'maxlength', '6' )
                        .byteLimit( function ( val ) {
-                               // Invalid title
-                               if ( val === '' ) {
-                                       return '';
-                               }
-
+                               var title = mw.Title.newFromText( String( val ) );
                                // Return without namespace prefix
-                               return new mw.Title( String( val ) ).getMain();
+                               return title ? title.getMain() : '';
                        } ),
                sample: 'User:Sample',
-               hasLimit: true,
-               limit: 6, // 'Sample' length
                expected: 'User:Sample'
        } );
 
+       byteLimitTest( {
+               description: 'Pass the limit and a callback as input filter',
+               $input: $( '<input type="text"/>' )
+                       .byteLimit( 6, function ( val ) {
+                               var title = mw.Title.newFromText( String( val ) );
+                               // Return without namespace prefix
+                               return title ? title.getMain() : '';
+                       } ),
+               sample: 'User:Example',
+               // The callback alters the value to be used to calculeate
+               // the length. The altered value is "Exampl" which has
+               // a length of 6, the "e" would exceed the limit.
+               expected: 'User:Exampl'
+       } );
+
+       byteLimitTest( {
+               description: 'Input filter that increases the length',
+               $input: $( '<input type="text"/>' )
+               .byteLimit( 10, function ( text ) {
+                       return 'prefix' + text;
+               } ),
+               sample: simpleSample,
+               // Prefix adds 6 characters, limit is reached after 4
+               expected: '1234'
+       } );
+
+       // Regression tests for bug 41450
+       byteLimitTest( {
+               description: 'Input filter of which the base exceeds the limit',
+               $input: $( '<input type="text"/>' )
+               .byteLimit( 3, function ( text ) {
+                       return 'prefix' + text;
+               } ),
+               sample: simpleSample,
+               hasLimit: true,
+               limit: 6, // 'prefix' length
+               expected: ''
+       } );
+
        QUnit.test( 'Confirm properties and attributes set', 4, function ( assert ) {
                var $el, $elA, $elB;