From 466029f417c0a9762d45ddfe85a1f221ac588423 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 17 May 2011 22:19:27 +0000 Subject: [PATCH] Add simple test for jquery.autoEllipsis --- resources/test/index.html | 3 ++ .../test/unit/jquery/jquery.autoEllipsis.js | 47 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 resources/test/unit/jquery/jquery.autoEllipsis.js diff --git a/resources/test/index.html b/resources/test/index.html index 8ce82fb850..3138182223 100644 --- a/resources/test/index.html +++ b/resources/test/index.html @@ -13,6 +13,8 @@ + + @@ -43,6 +45,7 @@ + diff --git a/resources/test/unit/jquery/jquery.autoEllipsis.js b/resources/test/unit/jquery/jquery.autoEllipsis.js new file mode 100644 index 0000000000..a613387e05 --- /dev/null +++ b/resources/test/unit/jquery/jquery.autoEllipsis.js @@ -0,0 +1,47 @@ +module( 'jquery.autoEllipsis.js' ); + +test( '-- Initial check', function(){ + + ok( jQuery.fn.autoEllipsis, 'jQuery.fn.autoEllipsis defined' ); +}); + +function createWrappedDiv( text ) { + var $wrapper = $( '
' ).css( 'width', '100px' ); + var $div = $( '
' ).text( text ); + $wrapper.append( $div ); + return $wrapper; +} + +function findDivergenceIndex( a, b ) { + var i = 0; + while ( i < a.length && i < b.length && a[i] == b[i] ) { + i++; + } + return i; +} + +test( 'Position right', function() { + // We need this thing to be visible, so append it to the DOM + var origText = 'This is a really long random string and there is no way it fits in 100 pixels.'; + var $wrapper = createWrappedDiv( origText ); + $( 'body' ).append( $wrapper ); + // Autoellipse it + $wrapper.autoEllipsis( { position: 'right' } ); + // Turn on word wrapping + var $span = $wrapper.find( 'span' ); + $span.css( 'whiteSpace', 'nowrap' ); + + // Check that the text fits + ok( $span.width() <= $span.parent().width(), "text fits (span's width is no larger than its parent's width)" ); + + // Add one character using scary black magic + var spanText = $span.text(); + var d = findDivergenceIndex( origText, spanText ); + spanText = spanText.substr( 0, d ) + origText[d] + '...'; + + // Put this text in the span and verify it doesn't fit + $span.text( spanText ); + ok( $span.width() > $span.parent().width(), "fit is maximal (adding one character makes it not fit any more)" ); + + $wrapper.remove(); +}); -- 2.20.1