' ).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-reduce-toolong' ), 'Reduce class set on too-long text.' );
assert.ok( ttf.$element.hasClass( 'mw-mmv-truncate-toolong' ), 'Truncate class set on too-long text.' );
ttf.$element.text( 'a' );
ttf.changeStyle();
assert.ok( !ttf.$element.hasClass( 'mw-mmv-reduce-toolong' ), 'Reduce class unset on too-long text.' );
assert.ok( !ttf.$element.hasClass( 'mw-mmv-truncate-toolong' ), 'Truncate class unset on too-long text.' );
ttf.$element.text( ( new Array( 90 ) ).join( 'a' ) );
ttf.changeStyle();
assert.ok( ttf.$element.hasClass( 'mw-mmv-reduce-toolong' ), 'Reduce class set on slightly long text.' );
assert.ok( !ttf.$element.hasClass( 'mw-mmv-truncate-toolong' ), 'Truncate class unset on slightly long text.' );
} );
QUnit.test( 'Shrink/grow', 10, 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.' );
assert.ok( ttf.truncated, 'State flag is correct.' );
ttf.grow();
assert.strictEqual( $element.text(), textOne + textTwo, 'The full text is readable after calling grow().' );
assert.ok( !ttf.truncated, 'State flag is correct.' );
ttf.grow();
assert.strictEqual( $element.text(), textOne + textTwo, 'grow() is idempotent.' );
assert.ok( !ttf.truncated, 'State flag is correct.' );
ttf.shrink();
assert.strictEqual( $element.text(), textOne + '…', 'The text is shortened again after calling shrink().' );
assert.ok( ttf.truncated, 'State flag is correct.' );
ttf.shrink();
assert.strictEqual( $element.text(), textOne + '…', 'shrink() is idempotent.' );
assert.ok( ttf.truncated, 'State flag is correct.' );
} );
QUnit.test( 'Shrink/grow noop', 2, function ( assert ) {
var $container = $( '#qunit-fixture' ).empty(),
$element = $( '
' ).appendTo( $container ),
textOne = ( new Array( 5 ) ).join( 'a' ),
textTwo = ( new Array( 10 ) ).join( 'b' ),
ttf = new mw.mmv.ui.TruncatableTextField( $container, $element );
ttf.max = 50;
ttf.set( '
' + textOne + '' + textTwo + '' ); // calls shrink
assert.strictEqual( $element.text(), textOne + textTwo, 'Text is intact.' );
assert.ok( !ttf.truncated, 'State flag is correct.' );
} );
}( mediaWiki, jQuery ) );