' ).appendTo( $container ),
text = ( new Array( 500 ) ).join( 'a' ),
ttf = new mw.mmv.ui.TruncatableTextField( $container, $element ),
newText = ttf.truncateText( text, ttf.max );
assert.strictEqual( newText.length, 100, 'Text is the right length.' );
assert.strictEqual( newText, ( new Array( 101 ) ).join( 'a' ), 'Text has the right content.' );
} );
QUnit.test( 'Different max length - text truncation', 2, function ( assert ) {
var $container = $( '#qunit-fixture' ).empty(),
$element = $( '
' ).appendTo( $container ),
text = ( new Array( 500 ) ).join( 'a' ),
ttf = new mw.mmv.ui.TruncatableTextField( $container, $element, { max: 200 } ),
newText = ttf.truncateText( text, ttf.max );
assert.strictEqual( newText.length, 200, 'Text is the right length.' );
assert.strictEqual( newText, ( new Array( 201 ) ).join( 'a' ), 'Text has the right content.' );
} );
QUnit.test( 'Different max length - DOM truncation', 1, function ( assert ) {
var $truncatedElement,
$container = $( '#qunit-fixture' ).empty(),
$element = $( '
' ).appendTo( $container ),
textOne = ( new Array( 150 ) ).join( 'a' ),
textTwo = ( new Array( 100 ) ).join( 'b' ),
ttf = new mw.mmv.ui.TruncatableTextField( $container, $element, { max: 200 } );
$element.append(
$( '
' ).text( textOne ),
$( '' ).text( textTwo )
);
// We only want to test the element exclusion here
ttf.truncateText = function () { return ''; };
$truncatedElement = ttf.truncate( $element.get( 0 ), ttf.max, false );
assert.strictEqual( $truncatedElement.text(), textOne, 'The too-long element is removed.' );
} );
QUnit.test( 'Changing style for slightly too-long elements', 3, function ( assert ) {
var $container = $( '#qunit-fixture' ).empty(),
$element = $( '' ).appendTo( $container ).text( ( new Array( 500 ) ).join( 'a' ) ),
ttf = new mw.mmv.ui.TruncatableTextField( $container, $element );
ttf.changeStyle();
assert.ok( ttf.$element.hasClass( 'mw-mmv-truncate-toolong' ), 'Class set on too-long text.' );
ttf.$element.text( 'a' );
ttf.changeStyle();
assert.ok( !ttf.$element.hasClass( 'mw-mmv-truncate-toolong' ), 'Class unset on short text.' );
ttf.$element.text( ( new Array( 300 ) ).join( 'a' ) );
ttf.changeStyle();
assert.ok( ttf.$element.hasClass( 'mw-mmv-truncate-toolong' ), 'Class re-set on too-long text.' );
} );
QUnit.test( 'Shrink/grow', 5, function ( assert ) {
var $container = $( '#qunit-fixture' ).empty(),
$element = $( '
' ).appendTo( $container ),
textOne = ( new Array( 50 ) ).join( 'a' ),
textTwo = ( new Array( 100 ) ).join( 'b' ),
ttf = new mw.mmv.ui.TruncatableTextField( $container, $element );
ttf.max = 50;
ttf.set( '
' + textOne + '' + textTwo + '' ); // calls shrink
assert.strictEqual( $element.text(), textOne + '…', 'The too-long element is excluded.' );
ttf.grow();
assert.strictEqual( $element.text(), textOne + textTwo, 'The full text is readable after calling grow().' );
ttf.grow();
assert.strictEqual( $element.text(), textOne + textTwo, 'grow() is idempotent.' );
ttf.shrink();
assert.strictEqual( $element.text(), textOne + '…', 'The text is shortened again after calling shrink().' );
ttf.shrink();
assert.strictEqual( $element.text(), textOne + '…', 'shrink() is idempotent.' );
} );
}( mediaWiki, jQuery ) );