' ).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( 'Shrink method', 2, function ( assert ) {
var $container = $( '#qunit-fixture' ).empty(),
$element = $( '
' ).appendTo( $container ),
ttf = new mw.mmv.ui.TruncatableTextField( $container, $element );
ttf.truncate = function ( ele, max, ell ) {
assert.strictEqual( max, ttf.max, 'Max length is passed in right' );
assert.strictEqual( ell, true, 'Ellipses are enabled on the first call' );
};
ttf.shrink();
} );
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 $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 ''; };
ttf.truncate( $element.get( 0 ), ttf.max, false );
assert.strictEqual( $container.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.' );
} );
}( mediaWiki, jQuery ) );