Permalink
Browse files

+ more tests for the client JS

  • Loading branch information...
1 parent e7ac35e commit 6bc9e9a7bbcc6212d9839425339c5f0179dc10e4 @floere committed Feb 25, 2012
Oops, something went wrong.
Binary file not shown.
@@ -24,6 +24,8 @@ var PickyAddination = function(view, results) {
var total = data.total;
var range = calculateRange(data);
if (range.offset < total) {
+ // TODO Use an actual link for the addination.
+ //
var result = $("<div class='addination'>" + t('results.addination.more') + "</div>");
result.bind('click', { offset: range.offset }, view.addinationClicked);
return result;
@@ -1,7 +1,7 @@
function AllocationRenderer(allocationChosenCallback, config) {
var self = this;
- var locale = PickyI18n.locale;
+ var locale = config.locale;
var qualifiers = Localization.qualifiers && Localization.qualifiers[locale] || {};
var explanations = Localization.explanations && Localization.explanations[locale] || {};
@@ -1,12 +1,6 @@
var Localization = {};
var PickyI18n = { };
-// Set the correct locale for all js code.
-//
-$(function() {
- PickyI18n.locale = $('html').attr('lang').split('-')[0] || 'en';
-});
-
// The client handles parameters and
// offers an insert method.
//
@@ -16,6 +10,13 @@ var PickyClient = function(config) {
// jQuery selectors are executed.
//
+ // The locale is by default extracted from the html attribute.
+ //
+ // TODO Remove.
+ //
+ PickyI18n.locale = $('html').attr('lang').split('-')[0] || 'en';
+ config['locale'] = config['locale'] || PickyI18n.locale;
+
// This is used to generate the correct query strings, localized.
//
// e.g with locale it:
@@ -3,6 +3,8 @@
// Container for an allocation.
//
+// TODO Rename to combinations.
+//
function Allocation(type, weight, count, combination, ids, rendered) {
var self = this;
@@ -11,9 +13,9 @@ function Allocation(type, weight, count, combination, ids, rendered) {
this.count = count; // 14
this.combination = combination; // [['title', 'Old', 'old'], ['title', 'Man', 'man']]
this.ids = ids || [];
- this.rendered = rendered;
+ this.rendered = rendered || [];
this.entries = this.rendered; // TODO Remove?
-
+
this.isType = function(name) {
return name == self.type;
};
@@ -25,7 +27,7 @@ function Allocation(type, weight, count, combination, ids, rendered) {
//
function Allocations(allocations) {
var self = this;
-
+
this.allocations = [];
// Wrap and save the allocations.
@@ -76,7 +78,7 @@ function PickyData(data) {
var renderedAmount = function() {
var sum = 0;
allocations.each(function(i, allocation) {
- sum += allocation.rendered.length;
+ sum += allocation.rendered.length;
});
return sum;
};
@@ -1,15 +1,18 @@
var PickyResultsRenderer = function(addination, config) {
+ var locale = config.locale;
+
var results = config['results'];
var resultsDivider = config['resultsDivider'];
var allocationWrapper = config['wrapResults'];
var noAsterisks = config['noAsterisks'];
// Adds asterisks to the last token.
//
- var asteriskifyLastToken = function(combination) {
- var last_part = combination[combination.length-1];
- var parts = combination.slice(0, combination.length-1);
+ var asteriskifyLastToken = function(combinations) {
+ var last_part = combinations[combinations.length-1];
+ if (last_part === undefined) { return []; }
+ var parts = combinations.slice(0, combinations.length-1);
if (parts == []) { parts = [parts]; }
if (!noAsterisks.include(last_part[0])) {
// Replace with * unless there is already one or a tilde.
@@ -19,11 +22,12 @@ var PickyResultsRenderer = function(addination, config) {
parts.push(last_part);
return parts;
};
+ this.asteriskifyLastToken = asteriskifyLastToken; // Note: For tests.
// Replaces the category with an explanation of the category.
//
var explainCategory = function(combination) {
- var explanations = Localization.explanations && Localization.explanations[PickyI18n.locale] || {}; // TODO
+ var explanations = Localization.explanations && Localization.explanations[locale] || {}; // TODO
var parts = [];
var combo;
@@ -33,24 +37,33 @@ var PickyResultsRenderer = function(addination, config) {
explanation = explanations[explanation] || explanation;
parts.push([explanation, combo[1]]);
}
+
return parts;
};
+ this.explainCategory = explainCategory; // Note: Only exposed for testing.
- // Puts together an explanation.
//
- // Note: Accumulates same categories using a whitespace.
//
var strongify = function(category, joinedTokens) {
- return [category.replace(/([\w\sÄäÖöÜüéèà]+)/, "<strong>$1</strong>"), joinedTokens].join(' ');
+ return [category.replace(/([\w\sÄäÖöÜüéèà\,]+)/, "<strong>$1</strong>"), joinedTokens].join(' ');
};
- var explain = function(type, combination) {
- var explanation_delimiter = Localization.explanation_delimiters[PickyI18n.locale];
+ this.strongify = strongify; // Note: Only exposed for testing.
+
+ // Puts together an explanation.
+ //
+ // Note: Accumulates same categories using a whitespace.
+ //
+ var explain = function(type, combinations) {
+ var explanation_delimiter = Localization.explanation_delimiters[locale];
- var parts = explainCategory(asteriskifyLastToken(combination));
+ var parts = explainCategory(asteriskifyLastToken(combinations));
var lastCategory = '';
var tokenAccumulator = [];
var joinedTokens = '';
- var replaced = $.map(parts, function(part) {
+ var replaced = []
+
+ // Note: Was $.map
+ parts.each(function(i, part) {
var category = part[0];
var token = part[1];
@@ -63,7 +76,7 @@ var PickyResultsRenderer = function(addination, config) {
tokenAccumulator.push(token);
lastCategory = category;
- return undefined;
+ return;
}
var result = strongify(lastCategory, tokenAccumulator.join(' '));
@@ -72,30 +85,38 @@ var PickyResultsRenderer = function(addination, config) {
tokenAccumulator.push(token);
lastCategory = category;
- return result;
+ replaced.push(result);
});
+
// there might be something in the accumulator
//
replaced.push(strongify(lastCategory, tokenAccumulator.join(' ')));
replaced = replaced.join(' ' + explanation_delimiter + ' ');
+ replaced = '<span class="explanation">' + type + ' ' + replaced + '</span>';
- return '<span class="explanation">' + type + ' ' + replaced + '</span>';
+ return replaced;
};
+ this.explain = explain; // Note: Only exposed for testing.
// TODO Make customizable.
//
var renderHeader = function(data, allocation) {
// TODO Make type definable. (Mapping, i18n)
//
var header_html = '<div class="header">';
- header_html += explain(allocation.type, allocation.combination);
+ header_html += explain(allocation.type, allocation.combination); // TODO Rename to combinations?
if (data.offset > 0) {
- header_html += '<div class="tothetop"><a href="#" onclick="javascript:$(\'body\').animate({scrollTop: 0}, 500);">&uarr;</a></div>'; // searchEngine.focus();
+ // TODO Add the class to the link. Remove the div.
+ //
+ header_html += '<div class="tothetop"><a href="#" onclick="javascript:$(\'body\').animate({scrollTop: 0}, 500);">&uarr;</a></div>';
+ // searchEngine.focus();
}
+ header_html += '</div>';
return header_html;
};
+ this.renderHeader = renderHeader;
// Render results with the data.
//
@@ -1,5 +1,7 @@
// Translations
//
+// TODO Still needed?
+//
var dictionary = {
common:{
@@ -39,5 +41,6 @@ var t = function(key) {
break;
}
};
+
return current;
};
@@ -0,0 +1,65 @@
+var allocation;
+describe(
+ "Empty allocation",
+ function() {
+ allocation = new Allocation(
+ 'test',
+ 3.14,
+ 123,
+ [['attr1', 'Original1', 'parsed1'], ['attr2', 'Original2', 'parsed2']],
+ [1,2,3],
+ ['a', 'b', 'c']
+ );
+ },
+ function() {
+ describe(
+ "type", null,
+ function() {
+ it("is correct", function() {
+ return allocation.type == 'test';
+ });
+ }
+ );
+ describe(
+ "weight", null,
+ function() {
+ it("is correct", function() {
+ return allocation.weight == 3.14;
+ });
+ }
+ );
+ describe(
+ "count", null,
+ function() {
+ it("is correct", function() {
+ return allocation.count == 123;
+ });
+ }
+ );
+ describe(
+ "ids", null,
+ function() {
+ it("is correct", function() {
+ return allocation.ids[1] == 2;
+ });
+ }
+ );
+ describe(
+ "rendered", null,
+ function() {
+ it("is correct", function() {
+ return allocation.rendered[1] == 'b';
+ });
+ }
+ );
+ describe(
+ "isType",
+ null,
+ function() {
+ it("is correct", function() {
+ return allocation.isType('test');
+ });
+ }
+ );
+ }
+);
@@ -0,0 +1,80 @@
+var allocations;
+describe(
+ "Empty allocations",
+ function() {
+ allocations = new Allocations([]);
+ },
+ function() {
+ describe(
+ "length",
+ null,
+ function() {
+ it("is correct", function() {
+ return allocations.length == 0;
+ });
+ }
+ );
+ describe(
+ "each",
+ null,
+ function() {
+ it("is correct", function() {
+ var result = true;
+ allocations.each(function() {
+ result = false; // It should not be called.
+ });
+ return result;
+ });
+ }
+ );
+ }
+);
+describe(
+ "Non-Empty allocations",
+ function() {
+ allocations = new Allocations(
+ [
+ [
+ 'test',
+ 3.14,
+ 123,
+ [['attr1', 'Original1', 'parsed1'], ['attr2', 'Original2', 'parsed2']],
+ [1,2,3]
+ ]
+ ]
+ );
+ },
+ function() {
+ describe(
+ "length",
+ null,
+ function() {
+ it("is correct", function() {
+ return allocations.length == 1;
+ });
+ }
+ );
+ // describe(
+ // "remove",
+ // null,
+ // function() {
+ // it("is correct", function() {
+ // return allocations.remove(0) == [];
+ // });
+ // }
+ // );
+ describe(
+ "each",
+ null,
+ function() {
+ it("is correct", function() {
+ var result = false;
+ allocations.each(function() {
+ result = true;
+ });
+ return result; // Needs to be called.
+ });
+ }
+ );
+ }
+);
Oops, something went wrong.

0 comments on commit 6bc9e9a

Please sign in to comment.