690ffb21e8c393b271a7d0dcd5cbe11f696c7a82
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.autoEllipsis.test.js
1 module( 'jquery.autoEllipsis' );
2
3 test( '-- Initial check', function() {
4 expect(1);
5 ok( $.fn.autoEllipsis, 'jQuery.fn.autoEllipsis defined' );
6 });
7
8 function createWrappedDiv( text, width ) {
9 var $wrapper = $( '<div />' ).css( 'width', width );
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 expect(4);
25
26 // We need this thing to be visible, so append it to the DOM
27 var origText = 'This is a really long random string and there is no way it fits in 100 pixels.';
28 var $wrapper = createWrappedDiv( origText, '100px' );
29 $( 'body' ).append( $wrapper );
30 $wrapper.autoEllipsis( { position: 'right' } );
31
32 // Verify that, and only one, span element was created
33 var $span = $wrapper.find( '> span' );
34 strictEqual( $span.length, 1, 'autoEllipsis wrapped the contents in a span element' );
35
36 // Check that the text fits by turning on word wrapping
37 $span.css( 'whiteSpace', 'nowrap' );
38 ltOrEq( $span.width(), $span.parent().width(), "Text fits (making the span 'white-space:nowrap' does not make it wider than its parent)" );
39
40 // Add two characters using scary black magic
41 var spanText = $span.text();
42 var d = findDivergenceIndex( origText, spanText );
43 var spanTextNew = spanText.substr( 0, d ) + origText[d] + origText[d] + '...';
44
45 gt( spanTextNew.length, spanText.length, 'Verify that the new span-length is indeed greater' );
46
47 // Put this text in the span and verify it doesn't fit
48 $span.text( spanTextNew );
49 // In IE6 width works like min-width, allow IE6's width to be "equal to"
50 if ( $.browser.msie && Number( $.browser.version ) == 6 ) {
51 gtOrEq( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more) - IE6: Maybe equal to as well due to width behaving like min-width in IE6' );
52 } else {
53 gt( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more)' );
54 }
55
56 // Clean up
57 $wrapper.remove();
58 });