summaryrefslogtreecommitdiff
path: root/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js
blob: e8c51214296cb925e6df16edff215cddcef80a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
( function ( $ ) {

	QUnit.module( 'jquery.autoEllipsis', QUnit.newMwEnvironment() );

	function createWrappedDiv( text, width ) {
		var $wrapper = $( '<div>' ).css( 'width', width ),
			$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;
	}

	QUnit.test( 'Position right', 4, function ( assert ) {
		// We need this thing to be visible, so append it to the DOM
		var $span, spanText, d, spanTextNew,
			origText = 'This is a really long random string and there is no way it fits in 100 pixels.',
			$wrapper = createWrappedDiv( origText, '100px' );

		$( '#qunit-fixture' ).append( $wrapper );
		$wrapper.autoEllipsis( { position: 'right' } );

		// Verify that, and only one, span element was created
		$span = $wrapper.find( '> span' );
		assert.strictEqual( $span.length, 1, 'autoEllipsis wrapped the contents in a span element' );

		// Check that the text fits by turning on word wrapping
		$span.css( 'whiteSpace', 'nowrap' );
		assert.ltOrEq(
			$span.width(),
			$span.parent().width(),
			'Text fits (making the span "white-space: nowrap" does not make it wider than its parent)'
		);

		// Add two characters using scary black magic
		spanText = $span.text();
		d = findDivergenceIndex( origText, spanText );
		spanTextNew = spanText.slice( 0, d ) + origText[d] + origText[d] + '...';

		assert.gt( spanTextNew.length, spanText.length, 'Verify that the new span-length is indeed greater' );

		// Put this text in the span and verify it doesn't fit
		$span.text( spanTextNew );
		assert.gt( $span.width(), $span.parent().width(), 'Fit is maximal (adding two characters makes it not fit any more)' );
	} );

}( jQuery ) );