Add simple test for jquery.autoEllipsis
authorRoan Kattouw <catrope@users.mediawiki.org>
Tue, 17 May 2011 22:19:27 +0000 (22:19 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Tue, 17 May 2011 22:19:27 +0000 (22:19 +0000)
resources/test/index.html
resources/test/unit/jquery/jquery.autoEllipsis.js [new file with mode: 0644]

index 8ce82fb..3138182 100644 (file)
@@ -13,6 +13,8 @@
        <script src="../mediawiki/mediawiki.js"></script>
        <script src="../mediawiki/mediawiki.user.js"></script>
 
+       <script src="../jquery/jquery.autoEllipsis.js"></script>
+       
        <script>
        mw.user.options.set({"skin": "vector"});
        </script>
@@ -43,6 +45,7 @@
        <script src="unit/jquery/jquery.mwPrototypes.js"></script>
        <script src="unit/mediawiki.util/mediawiki.util.js"></script>
        <script src="unit/jquery/jquery.colorUtil.js"></script>
+       <script src="unit/jquery/jquery.autoEllipsis.js"></script>
 
        <!-- TestSwarm -->
        <script src="testswarm.inject.js"></script>
diff --git a/resources/test/unit/jquery/jquery.autoEllipsis.js b/resources/test/unit/jquery/jquery.autoEllipsis.js
new file mode 100644 (file)
index 0000000..a613387
--- /dev/null
@@ -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 = $( '<div />' ).css( 'width', '100px' );
+       var $div = $( '<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();
+});