Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added test suite.

  • Loading branch information...
commit b6ec5e6a365cadeddaf4600606af30097686ac79 1 parent 9ed2f4f
@polarblau authored
View
1  .gitignore
@@ -1,2 +1,3 @@
.DS_Store
_dev.html
+lib/jquery-1.6.2.js
View
128 spec/smarttruncation_spec.js
@@ -2,6 +2,19 @@ describe('smarttruncation', function () {
// Helpers
+ function makeFixture() {
+ var list = [];
+ var text = "Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum";
+ var words = text.split(" ");
+ var length = Math.floor(Math.random() * (words.length - 10)) + 10;
+ list.push('<ul class="list" id="list" style="width:100px">');
+ list.push('<li>');
+ list.push(words.slice(0, length).join(' '));
+ list.push('</li>');
+ list.push('</ul>');
+ return $(list.join(''));
+ };
+
function hasNamesSpacedEvent(el, event, namespace) {
var events = $(el).data("events");
return events[event] !== undefined && events[event][0].namespace === namespace;
@@ -10,8 +23,8 @@ describe('smarttruncation', function () {
// Custom matchers
- var isElementTruncated = function(input) {
- var isTruncated = true; // how do we test for this?
+ var isElementTruncated = function(listItem) {
+ var isTruncated = $.contains(listItem, $('span.smarttruncation-wrapper'));
return isTruncated ? isTruncated : null;
};
@@ -26,32 +39,113 @@ describe('smarttruncation', function () {
});
});
- // Build and remove fixtures in setup and teardown
-
- beforeEach(function () {
-
- });
- afterEach(function () {
- $(window).data('smarttruncation.sizecache', null);
- });
-
- //
-
- // TEST: Settings
+ // Test main features for different font sizes
+ var testFontSizes = [8, 10, 12, 24, 32];
+ var fontFamilies = ['serif', 'sans-serif'];
- // TEST: CSS & Attrbutes
+ $.each(fontFamilies, function(i, family){
+ describe('for family ' + family, function(){
+
+ $.each(testFontSizes, function(i, size){
+ describe('for size ' + size, function(){
+
+ beforeEach(function () {
+ makeFixture().appendTo('body');
+ $('#list li').css({
+ 'fontSize' : size + 'px',
+ 'fontFamily': family
+ });
+ });
+
+ afterEach(function () {
+ $('#list').remove();
+ $(window).data('smarttruncation.sizecache', null);
+ });
+
+ // TEST: Truncation
+
+ it('should trucate the text', function() {
+ var $listItem = $('#list li');
+ var textBefore = $listItem.text();
+ $listItem.smartTruncation();
+ var textAfter = $listItem.text();
+ expect(textBefore.length > textAfter.length).toBeTruthy();
+ });
+
+ it('should append an ellipsis to the text', function() {
+ var $listItem = $('#list li');
+ var text = $listItem.smartTruncation().text();
+ expect(text.slice(-3)).toEqual("...");
+ });
+
+ it('should ensure that the text is shorter than the container', function() {
+ var $listItem = $('#list li');
+ var wrapper = $listItem.smartTruncation().find('span');
+ expect($listItem.width() >= wrapper.width()).toBeTruthy();
+ });
+
+ // TEST: Settings
+
+ it('should keep an file extension if "protectExtensions" is set to "true"', function() {
+ var $listItem = $('#list li');
+ $listItem.text("this_is_a_very_very_very_very_very_very_very_very_long_file_name.png")
+ var text = $listItem.smartTruncation({ protectExtensions: true }).text();
+ expect(text.slice(-3)).toEqual("png");
+ expect(text.slice(-6)).toEqual("...png");
+ });
+
+ it('should shorten the text still if "truncateCenter" is set to "true"', function() {
+ var $listItem = $('#list li');
+ var textBefore = $listItem.text();
+ $listItem.smartTruncation({ truncateCenter: true });
+ var textAfter = $listItem.text();
+ expect(textBefore.length > textAfter.length).toBeTruthy();
+ });
+
+ it('should not append an ellipsis to the text if "truncateCenter" is set to "true"', function() {
+ var $listItem = $('#list li');
+ var text = $listItem.smartTruncation({ truncateCenter: true });
+ expect(text.slice(-3)).toNotEqual("...");
+ });
+
+ it('should insert the ellipsis within the text if "truncateCenter" is set to "true"', function() {
+ var $listItem = $('#list li');
+ $listItem.smartTruncation({ truncateCenter: true });
+ expect($listItem.text().split("...").length).toEqual(2);
+ });
+
+ it('should ensure that the text is shorter than the container still if "truncateCenter" is set to "true"', function() {
+ var $listItem = $('#list li');
+ var wrapper = $listItem.smartTruncation({ truncateCenter: true }).find('span');
+ expect($listItem.width() >= wrapper.width()).toBeTruthy();
+ });
+
+ });
+ });
+ });
+ });
// TEST: General jQuery plugin functionality
it('should be applicable to multiple elements', function() {
-
+ var $list_1 = makeFixture().appendTo('body');
+ var $list_2 = makeFixture().appendTo('body');
+ $('.list li').smartTruncation();
+ expect($list_1).toBeTruncated();
+ expect($list_1.find('li')).toBeTruncated();
+ $list_1.remove();
+ $list_2.remove();
});
it('should be chainable', function() {
-
+ var $list = makeFixture().appendTo('body');
+ var $listItem = $list.find('li');
+ $listItem.smartTruncation().addClass('foo');
+ expect($listItem.hasClass('foo')).toBeTruthy();
+ $list.remove();
});
});
View
15 src/jquery.smarttruncation.js
@@ -78,9 +78,13 @@
}
// wrap content in a inline element to get exact width
- var $wrapper = $this.wrapInner('<span/>').find('span').css({
- 'whiteSpace': 'nowrap'
- })
+ var $wrapper = $this
+ .wrapInner('<span/>', {
+ 'class' : 'smarttruncation-wrapper'
+ })
+ .find('span').css({
+ 'whiteSpace': 'nowrap'
+ });
var origText = $.trim($wrapper.text());
var outerWidth = $wrapper.width();
@@ -100,6 +104,7 @@
var update = function() {
// how much do we need to shave off including the ellipsis we want to append?
var diff = $this.width() - outerWidth - 3 * sizes['.'];
+ var safety;
// do we need to truncate
if (diff <= 0) {
@@ -153,7 +158,7 @@
// allow for a little bit room, if we check for absolutes,
// the browser will get caught in a loop and the sky will come down
// let's use 40% of the fontsize
- var safety = parseInt(fontAttributes.fontSize, 10) * 0.4;
+ safety = parseInt(fontAttributes.fontSize, 10) * 0.4;
if ($wrapper.width() + safety < $this.width()) {
while ($wrapper.width() + safety < $this.width()) {
lengthLeft < lengthRight? lengthLeft++ : lengthRight++;
@@ -194,7 +199,7 @@
// allow for a little bit room, if we check for absolutes,
// the browser will get caught in a loop and the sky will come down
// let's use 40% of the fontsize
- var safety = parseInt(fontAttributes.fontSize, 10) * 0.4;
+ safety = parseInt(fontAttributes.fontSize, 10) * 0.4;
if ($wrapper.width() + safety < $this.width()) {
while ($wrapper.width() + safety < $this.width()) {
$wrapper.text($.trim(chunks.slice(0, ++length).join("")) + "..." + extension);
Please sign in to comment.
Something went wrong with that request. Please try again.