/*
* bezen.template.test.js - Unit Tests of HTML template methods
*
* author: Eric Bréchemier
* license: Creative Commons Attribution 3.0 Unported
* http://creativecommons.org/licenses/by/3.0/
* version: 2012-08-15
*/
/*requires bezen.js */
/*requires bezen.object.js */
/*requires bezen.dom.js */
/*requires bezen.template.js */
/*requires bezen.assert.js */
/*requires bezen.testrunner.js */
/*jslint nomen:false, white:false, onevar:false, plusplus:false */
/*global bezen, document, window */
bezen.template.test = (function() {
// Builder of
// Closure object for Test of HTML templating
// Define aliases
var assert = bezen.assert,
testrunner = bezen.testrunner,
log = bezen.log,
$ = bezen.$,
hasAttribute = bezen.dom.hasAttribute,
exists = bezen.object.exists;
var testSetAliases = function(){
var ut = bezen.template.setAliases;
var aliases = bezen.template._.aliases;
// check initial aliases
assert.objectEquals(aliases,{
template:'template',
list: 'list',
optional: 'optional'
}, "initial values expected in aliases: template, list, optional");
// declare unknown aliases
ut({
missing: 'new',
other: 'new'
});
assert.objectEquals(aliases,{
template:'template',
list: 'list',
optional: 'optional'
}, "no change expected after declaring unknown aliases");
// declare all aliases at once
ut({
template: 'newtemplate',
list: 'newlist',
optional: 'newoptional'
});
assert.objectEquals(aliases,{
template:'newtemplate',
list: 'newlist',
optional: 'newoptional'
}, "new aliases expected after declaring new values all at once");
// declare aliases one by one
ut({template: 'template2'});
ut({list: 'list2'});
ut({optional: 'optional2'});
assert.objectEquals(aliases,{
template:'template2',
list: 'list2',
optional: 'optional2'
}, "new aliases expected after declaring new values one by one");
// restore initial aliases
ut({
template: 'template',
list: 'list',
optional: 'optional'
});
assert.objectEquals(aliases,{
template:'template',
list: 'list',
optional: 'optional'
}, "original values expected after resetting aliases");
};
var testRemoveClones = function() {
var ut = bezen.template.removeClones;
var template1 = $('prototype01');
ut(template1);
assert.equals(template1.nextSibling, $('clone01'),
"No change expected for template #1 until bezen.lastClone has been set");
template1.bezen = {lastClone: $('clone01') };
ut(template1);
assert.equals(template1.nextSibling, null,
"No more sibling expected for template with a single clone as sibling");
ut(template1);
assert.equals(template1.nextSibling, null,
"No change expected on second run for template without siblings");
var template2 = $('prototype02');
ut(template2);
assert.equals(template2.nextSibling, $('clone02'),
"No change expected for template #2 until bezen.lastClone has been set");
template2.bezen = {lastClone: $('clone04') };
ut(template2);
assert.equals(template2.nextSibling, $('notaclone05'),
"First uncloned node expected as sibling");
ut(template2);
assert.equals(template2.nextSibling, $('notaclone05'),
"No change expected for template with no clone as sibling");
};
var testGetBaseUrl = function(){
var ut = bezen.template._.getBaseUrl;
assert.equals(ut('http://url-without-hash.com/'),
'http://url-without-hash.com/',
"no change expected when the url has no hash");
assert.equals(ut('http://url-with-hash.com/#'),
'http://url-with-hash.com/', "empty hash must be removed");
assert.equals(ut('http://url-with-hash.com#simple'),
'http://url-with-hash.com', "simple hash must be removed");
assert.equals(ut('http://url-with-hash.com#more/complex/hash'),
'http://url-with-hash.com',
"more complex hash must be removed");
assert.equals(ut(), ut(window.location.href),
"missing param must default to window.location.href");
};
var testGetNodeValue = function() {
var ut = bezen.template._.getNodeValue;
var href = $('node10').getAttributeNode('href');
assert.equals(ut(href),'#url#',
"url parameter expected for href attribute");
var src = $('node11').getAttributeNode('src');
assert.equals(ut(src), '#img#',
"img parameter expected for src attribute");
var title = $('node12').getAttributeNode('title');
assert.equals(ut(title), '#title#',
"title parameter expected for title attribute");
var text = $('node12').firstChild;
assert.equals(ut(text), '#text#',
"text parameter expected for text node");
};
var testReplaceParams = function(){
var ut = bezen.template._.replaceParams;
var STATUS_NO_CHANGE = bezen.template._.STATUS_NO_CHANGE,
STATUS_SUCCESS = bezen.template._.STATUS_SUCCESS,
STATUS_MISSING = bezen.template._.STATUS_MISSING,
STATUS_FAILED = bezen.template._.STATUS_FAILED;
assert.isTrue( STATUS_NO_CHANGE < STATUS_SUCCESS &&
STATUS_SUCCESS < STATUS_MISSING &&
STATUS_MISSING < STATUS_FAILED,
"growing values expected for No Change < Success < Missing < Failed");
var data = {};
var noparamText = $('node21').firstChild;
assert.equals(ut(noparamText,data), STATUS_NO_CHANGE,
"status no change expected for text without param");
assert.equals(noparamText.nodeValue,'No param',
"no change expected for text without param");
var noparamAttribute = $('node21').getAttributeNode('title');
assert.equals(ut(noparamAttribute,data), STATUS_NO_CHANGE,
"status no change expected for attribute without param");
assert.equals(noparamAttribute.nodeValue,'No param',
"no change expected for attribute without param");
data = {param1: 'single'};
var singleParamText = $('node22').firstChild;
assert.equals(ut(singleParamText,data), STATUS_SUCCESS,
"status success expected for text with single parameter");
assert.equals(singleParamText.nodeValue, 'single',
"replaced text expected for text with single parameter");
var singleParamAttribute = $('node22').getAttributeNode('title');
assert.equals(ut(singleParamAttribute,data), STATUS_SUCCESS,
"status success expected for attribute with single parameter");
assert.equals(singleParamAttribute.nodeValue, 'single',
"replaced text expected for attribute with single parameter");
data = {'0-one-2': 'strange'};
var strangeParamText = $('node23').firstChild;
assert.equals(ut(strangeParamText,data), STATUS_SUCCESS,
"status success expected for text with strange parameter");
assert.equals(strangeParamText.nodeValue, 'strange',
"replaced text expected for text with strange parameter");
var strangeParamAttribute = $('node23').getAttributeNode('title');
assert.equals(ut(strangeParamAttribute,data), STATUS_SUCCESS,
"status success expected for attribute with strange parameter");
assert.equals(strangeParamAttribute.nodeValue, 'strange',
"replaced text expected for attribute with strange parameter");
data = {param1: 'first', param2: 'second'};
var twoParamText = $('node24').firstChild;
assert.equals(twoParamText.nodeValue, '..#param1##param2#..',
"initial text expected for text with two parameters");
assert.equals(ut(twoParamText,data), STATUS_SUCCESS,
"status success expected for text with two parameters");
assert.equals(twoParamText.nodeValue, '..firstsecond..',
"replaced text expected for text with two parameters");
var twoParamAttribute = $('node24').getAttributeNode('title');
assert.equals(ut(twoParamAttribute,data), STATUS_SUCCESS,
"status success expected for attribute with two parameters");
assert.equals(twoParamAttribute.nodeValue, 'One: first Two: second',
"replaced text expected for attribute with two parameters");
data = {param1: 'tic', param2: 'tac', param3: 'toe'};
var threeParamText = $('node25').firstChild;
assert.equals(ut(threeParamText,data), STATUS_SUCCESS,
"status success expected for text with three parameters");
assert.equals(threeParamText.nodeValue, '1: tic 2: tac 3: toe',
"replaced text expected for text with three parameters");
var threeParamAttribute = $('node25').getAttributeNode('title');
assert.equals(ut(threeParamAttribute,data), STATUS_SUCCESS,
"status success expected for attribute with three parameters");
assert.equals(threeParamAttribute.nodeValue, 'ticparam2toe',
"first and third params expected to be replaced for "+
"attribute with three mingled parameters");
data = {'null': null, 'undef': undefined, 'text': "lorem ipsum"};
var nullParamText = $('node30').firstChild;
assert.equals(ut(nullParamText,data), STATUS_FAILED,
"status failed expected for text with null parameter");
assert.equals(nullParamText.nodeValue, 'null',
"'null' value expected for text with null parameter");
var undefParamText = $('node31').firstChild;
assert.equals(ut(undefParamText,data), STATUS_MISSING,
"status missing expected for text with undefined parameter");
assert.equals(undefParamText.nodeValue, '#undef#',
"no replacement expected for text with undefined parameter");
var textUndefParamText = $('node32').firstChild;
assert.equals(ut(textUndefParamText,data), STATUS_MISSING,
"status missing expected for text + undef");
assert.equals(textUndefParamText.nodeValue, 'lorem ipsum #undef#',
"partial replacement expected for text + undef");
var textNullParamText = $('node33').firstChild;
assert.equals(ut(textNullParamText,data), STATUS_FAILED,
"status failed expected for text + null");
assert.equals(textNullParamText.nodeValue, 'lorem ipsum null',
"replacement expected for text + null");
var undefTextParamText = $('node34').firstChild;
assert.equals(ut(undefTextParamText,data), STATUS_MISSING,
"status missing expected for undef + text");
assert.equals(undefTextParamText.nodeValue, '#undef# lorem ipsum',
"partial replacement expected for undef + text");
var undefNullParamText = $('node35').firstChild;
assert.equals(ut(undefNullParamText,data), STATUS_FAILED,
"status failed expected for undef + null");
assert.equals(undefNullParamText.nodeValue, '#undef# null',
"partial replacement expected for undef + null");
var nullTextParamText = $('node36').firstChild;
assert.equals(ut(nullTextParamText,data), STATUS_FAILED,
"status failed expected for null + text");
assert.equals(nullTextParamText.nodeValue, 'null lorem ipsum',
"partial replacement expected for null + text");
var nullUndefParamText = $('node37').firstChild;
assert.equals(ut(nullUndefParamText,data), STATUS_FAILED,
"status failed expected for null + undef");
assert.equals(nullUndefParamText.nodeValue, 'null #undef#',
"partial replacement expected for null + undef");
var textUndefNullParamText = $('node38').firstChild;
assert.equals(ut(textUndefNullParamText,data), STATUS_FAILED,
"status failed expected for text+undef+null");
assert.equals(textUndefNullParamText.nodeValue, 'lorem ipsum #undef# null',
"partial replacement expected for text+undef+null");
var nullUndefTextParamText = $('node39').firstChild;
assert.equals(ut(nullUndefTextParamText,data), STATUS_FAILED,
"status failed expected for null+undef+text");
assert.equals(nullUndefTextParamText.nodeValue, 'null #undef# lorem ipsum',
"partial replacement expected for null+undef+text");
var undefTextNullParamText = $('node40').firstChild;
assert.equals(ut(undefTextNullParamText,data), STATUS_FAILED,
"status failed expected for undef+text+null");
assert.equals(undefTextNullParamText.nodeValue, '#undef# lorem ipsum null',
"partial replacement expected for undef+text+null");
// test with numeric value which appears in IE for colSpan attribute
var numericNodeValue = {nodeValue: 5};
assert.equals(ut(numericNodeValue, data), STATUS_NO_CHANGE,
"no change expected for numeric node value");
assert.equals(numericNodeValue.nodeValue, 5,
"numeric value must have been preserved");
// test with object value which appears in IE for 'bezen' property
var id = {id:42};
var customValue = {nodeValue: id};
assert.equals(ut(customValue, data), STATUS_NO_CHANGE,
"no change expected for custom node value");
assert.equals(customValue.nodeValue, id,
"object value must have been preserved");
};
var testAnonymize = function(){
var ut = bezen.template._.anonymize;
var div50 = $('div50');
ut(div50.firstChild); // no failure expected, nothing expected without id
ut(div50);
assert.isFalse( hasAttribute(div50,'id'),
"div50 expected to be anonymized");
ut(div50);
assert.isFalse( hasAttribute(div50,'id'),
"no change expected on second run");
var div51 = $('div51');
var id = {};
ut(div51,id);
assert.isFalse( hasAttribute(div51,'id'),
"div51 expected to be anonymized");
assert.objectEquals(id,{div51: div51}, "div51 expected in id");
};
var isParentRemoved = function(node){
// check whether the node has been removed from its parent
// This is done by checking whether the parentNode is null (all browsers
// except IE) or the parentNode has no innerHTML property (IE).
//
// param:
// node - (DOM Node) (!nil) the node to check
return (node.parentNode === null ||
node.parentNode.innerHTML === undefined);
};
var testInitNode = function() {
var ut = bezen.template.initNode;
var STATUS_NO_CHANGE = bezen.template._.STATUS_NO_CHANGE,
STATUS_SUCCESS = bezen.template._.STATUS_SUCCESS,
STATUS_OPTIONAL_NIXED = bezen.template._.STATUS_OPTIONAL_NIXED,
STATUS_OPTIONAL_KEPT = bezen.template._.STATUS_OPTIONAL_KEPT,
STATUS_MISSING = bezen.template._.STATUS_MISSING,
STATUS_FAILED = bezen.template._.STATUS_FAILED;
assert.isTrue( STATUS_NO_CHANGE < STATUS_SUCCESS &&
STATUS_SUCCESS < STATUS_OPTIONAL_NIXED &&
STATUS_OPTIONAL_NIXED < STATUS_OPTIONAL_KEPT &&
STATUS_OPTIONAL_KEPT < STATUS_MISSING &&
STATUS_MISSING < STATUS_FAILED,
"growing values expected for No Change < Success < "+
"Optional Nixed < Optional Kept < Missing < Failed");
var title = 'Holidays for the school year 2009-2010';
var data = {title: title};
var block0 = $('block60');
assert.equals(ut(block0,data),STATUS_SUCCESS,
"status success expected after replacement of parameter in block 0");
assert.equals(block0.getAttributeNode('title').nodeValue, title,
"replaced title expected in title attribute of block 0");
assert.isFalse(hasAttribute(block0,'id'),
"id expected to be removed from block 0");
var block1 = $('block61');
var id1 = {};
assert.equals(ut(block1,data,id1),STATUS_SUCCESS,
"status success expected after replacement of parameter in block 1");
assert.equals(block1.firstChild.firstChild.nodeValue, 'Title: '+title,
"replaced title expected in h2 within block 1");
assert.isFalse(hasAttribute(block1,'id'),
"id expected to be removed from block 1");
assert.objectEquals(id1,{block61: block1}, "block 1 expected in id1");
var row2 = $('month62');
data = {month: 'September', year: 2009, holidays: 1, alternate: 'even'};
assert.equals(ut(row2,data),STATUS_SUCCESS,
"status success expected after replacement of parameters in row 2");
assert.equals(row2.className,'even',"'even' class name expected on row 2");
var td = row2.getElementsByTagName('td');
assert.equals(td[0].firstChild.nodeValue, 'September',
"month expected in first cell of row 2");
assert.equals(td[1].firstChild.nodeValue, '2009',
"year expected in second cell of row 2");
assert.equals(td[2].firstChild.nodeValue, '1',
"holidays expected in third cell of row 2");
assert.isFalse(hasAttribute(row2,'id'),
"id expected to be removed from row 2");
var row3 = $('month63');
data = {month: 'October', year: 2009, holidays: 8, alternate: 'odd'};
assert.equals(ut(row3,data),STATUS_SUCCESS,
"status success expected after replacement of parameters in row 3");
assert.equals(row3.className,'odd', "'odd' class name expected on row 3");
td = row3.getElementsByTagName('td');
assert.equals(td[0].firstChild.nodeValue, 'October',
"month expected in first cell of row 3");
assert.equals(td[1].firstChild.nodeValue, '2009',
"year expected in second cell of row 3");
assert.equals(td[2].firstChild.nodeValue, '8',
"holidays expected in third cell of row 3");
assert.isFalse(hasAttribute(row3,'id'),
"id expected to be removed from row 3");
var months = [
{month: 'September', year: 2009, holidays: 1, alternate: 'odd'},
{month: 'October', year: 2009, holidays: 8, alternate: 'even'},
{month: 'November', year: 2009, holidays: 6, alternate: 'odd'},
{month: 'December', year: 2009, holidays: 13, alternate: 'even'},
{month: 'January', year: 2010, holidays: 4, alternate: 'odd'},
{month: 'February', year: 2010, holidays: 12, alternate: 'even'},
{month: 'March', year: 2010, holidays: 9, alternate: 'odd'},
{month: 'April', year: 2010, holidays: 18, alternate: 'even'},
{month: 'May', year: 2010, holidays: 4, alternate: 'odd'},
{month: 'June', year: 2010, holidays: 1, alternate: 'even'},
{month: 'July', year: 2010, holidays: 29, alternate: 'odd'},
{month: 'August', year: 2010, holidays: 31, alternate: 'even'}
];
data = {
title: title,
'month list': months
};
var block4 = $('block64');
assert.equals(ut(block4,data),STATUS_SUCCESS,
"status success expected after parameter replacement in block 4");
assert.isFalse(hasAttribute(block4,'id'),
"block4 expected to be without id");
assert.equals(block4.getElementsByTagName('h2')[0].firstChild.nodeValue,
'Title: '+title, "replaced title expected in h2 within block 4");
var tr, row, month;
tr = block4.getElementsByTagName('tr');
for (row=1; row