e0d5b98f63d7965caf55c8f5c987096d1ff55439
[lhc/web/wiklou.git] / resources / test / unit / jquery / jquery.autoEllipsis.js
1 module( 'jquery.autoEllipsis.js' );
2
3 test( '-- Initial check', function(){
4
5 ok( jQuery.fn.autoEllipsis, 'jQuery.fn.autoEllipsis defined' );
6 });
7
8 function createWrappedDiv( text ) {
9 var $wrapper = $( '<div />' ).css( 'width', '100px' );
10 var $div = $( '<div />' ).text( text );
11 $wrapper.append( $div );
12 return $wrapper;
13 }
14
15 function findDivergenceIndex( a, b ) {
16 var i = 0;
17 while ( i < a.length && i < b.length && a[i] == b[i] ) {
18 i++;
19 }
20 return i;
21 }
22
23 test( 'Position right', function() {
24 // We need this thing to be visible, so append it to the DOM
25 var origText = 'This is a really long random string and there is no way it fits in 100 pixels.';
26 var $wrapper = createWrappedDiv( origText );
27 $( 'body' ).append( $wrapper );
28 $wrapper.autoEllipsis( { position: 'right' } );
29
30 // Verify that, and only one, span element was created
31 var $span = $wrapper.find( '> span' );
32 deepEqual( $span.length, 1, 'autoEllipsis wrapped the contents in a span element' );
33
34 // Check that the text fits by turning on word wrapping
35 $span.css( 'whiteSpace', 'nowrap' );
36 ok( $span.width() <= $span.parent().width(), "Text fits (span's width is no larger than its parent's width)" );
37
38 // Add one character using scary black magic
39 var spanText = $span.text();
40 var d = findDivergenceIndex( origText, spanText );
41 spanText = spanText.substr( 0, d ) + origText[d] + '...';
42
43 // Put this text in the span and verify it doesn't fit
44 $span.text( spanText );
45 ok( $span.width() > $span.parent().width(), 'Fit is maximal (adding one character makes it not fit any more)' );
46
47 // Clean up
48 $wrapper.remove();
49 });