From 4caa8528cabeac71bb0181d19e1c611afa71b7f5 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Tue, 21 May 2013 22:33:38 +0200 Subject: [PATCH] jquery.byteLimit: Improve unit tests * Update outdated documentation * Remove hasLimit and limit properties of the test helper function. These just repeated data already provided (hasLimit if input != output, limit == expected.length) * Add tests for a filter that increases the length (currently only a decreasing filter was tested). Change-Id: Ib4c19f4d49ed9d19117bbbaccedb0fccaeb34719 --- .../resources/jquery/jquery.byteLimit.test.js | 95 +++++++++---------- 1 file changed, 44 insertions(+), 51 deletions(-) diff --git a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js index c21844eb84..7f4494eefd 100644 --- a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js @@ -31,55 +31,34 @@ /** * 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: $( '' ), sample: simpleSample, - hasLimit: false, expected: simpleSample } ); @@ -98,7 +76,6 @@ $input: $( '' ) .byteLimit(), sample: simpleSample, - hasLimit: false, expected: simpleSample } ); @@ -108,8 +85,6 @@ .attr( 'maxlength', '10' ) .byteLimit(), sample: simpleSample, - hasLimit: true, - limit: 10, expected: '1234567890' } ); @@ -118,8 +93,6 @@ $input: $( '' ) .byteLimit( 10 ), sample: simpleSample, - hasLimit: true, - limit: 10, expected: '1234567890' } ); @@ -129,8 +102,6 @@ .attr( 'maxlength', '10' ) .byteLimit( 15 ), sample: simpleSample, - hasLimit: true, - limit: 15, expected: '123456789012345' } ); @@ -139,8 +110,6 @@ $input: $( '' ) .byteLimit( 14 ), sample: mbSample, - hasLimit: true, - limit: 14, expected: '1234567890' + U_20AC + '1' } ); @@ -149,8 +118,6 @@ $input: $( '' ) .byteLimit( 12 ), sample: mbSample, - hasLimit: true, - limit: 12, expected: '1234567890' + '12' } ); @@ -167,8 +134,6 @@ return new mw.Title( String( val ) ).getMain(); } ), sample: 'User:Sample', - hasLimit: true, - limit: 6, // 'Sample' length expected: 'User:Sample' } ); @@ -186,11 +151,39 @@ return new mw.Title( String( val ) ).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: $( '' ) + .byteLimit( 6, function ( val ) { + // Invalid title + if ( val === '' ) { + return ''; + } + + // Return without namespace prefix + return new mw.Title( String( val ) ).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: $( '' ) + .byteLimit( 10, function ( text ) { + return 'prefix' + text; + } ), + sample: simpleSample, + // Prefix adds 6 characters, limit is reached after 4 + expected: '1234' + } ); + QUnit.test( 'Confirm properties and attributes set', 4, function ( assert ) { var $el, $elA, $elB; -- 2.20.1