Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
Initial unit tests for a few helpers.
Browse files Browse the repository at this point in the history
References #11 still a long way to go to actually test the framework.
  • Loading branch information
KevinGrandon committed Nov 2, 2011
1 parent 0ad0824 commit f2c2b8b
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 6 deletions.
5 changes: 4 additions & 1 deletion _core/helpers/FormHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// Before and after are handled by NWTHelperInstance
// So far, "betweenContent" is only used for forms
this.betweenContent = '';

console.log('Form attributes', attributes);
this.key = attributes[0];
this.attributes = attributes[1] || {};

Expand Down Expand Up @@ -173,6 +173,8 @@
optionMarkup += '<option value="' + i + '"' + selectedMarkup + '>' + options[i] + '</option>';
}

delete this.attributes.options;

return '<select ' + this._parseAttributes() + '>' + optionMarkup + '</select>';
}

Expand All @@ -181,6 +183,7 @@
* Renders a text input field
*/
FormField.prototype.render_text = function() {
delete this.attributes.type;
return '<input type="' + this.get('type') + '" value="' + this.get('value') + '" ' + this._parseAttributes() + '>';
}

Expand Down
17 changes: 13 additions & 4 deletions _core/helpers/HtmlHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@


NWTStdHtmlElement.prototype.render = function() {
return '<' + this.tag + ' ' + this._parseAttributes() + '>' + this.content + '</' + this.tag + '>';
var attributes = this._parseAttributes();
return '<' + this.tag + ( attributes ? ' ' + attributes : '' ) + '>' + this.content + '</' + this.tag + '>';
};


Expand All @@ -44,8 +45,11 @@
* Renders the HtmlLink object
*/
HtmlLink.prototype.render = function() {

var attributes = this._parseAttributes();

return this.beforeContent +
'<a href="' + this.get('href') + '" ' + this._parseAttributes() + '>' +
'<a href="' + this.get('href') + '"' + ( attributes ? ' ' + attributes : '' ) + '>' +
this.get('content') +
'</a>' +
this.afterContent;
Expand Down Expand Up @@ -82,7 +86,9 @@
content.push('</li>');
}

return '<ul ' + this._parseAttributes() + '>' + content.join('') + '</ul>';
var attributes = this._parseAttributes();

return '<ul' + ( attributes ? ' ' + attributes : '' ) + '>' + content.join('') + '</ul>';
};


Expand All @@ -102,7 +108,10 @@
* Renders the HtmlLink object
*/
HtmlImage.prototype.render = function() {
return '<img src="' + this.get('src') + '" ' + this._parseAttributes() + '>';

var attributes = this._parseAttributes();

return '<img src="' + this.get('src') + '"' + ( attributes ? ' ' + attributes : '' ) + '>';
};


Expand Down
19 changes: 19 additions & 0 deletions _core/libraries/SharedUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@
};


/**
* Runs a validation rule on a single method
*/
NWTValidation.prototype.validate = function(rule, value, definition) {
var method = '_validate_' + rule;

if( !(definition instanceof Object) ) {
definition = {value: definition};
}

try {
this[method](value, definition);
} catch(e) {
return false;
}
return true;
};


/**
* Validates a provided value with a ruleset
*/
Expand Down
113 changes: 113 additions & 0 deletions _core/tests/TestFormHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
var vows = require("vows"),
assert = require("assert"),
nwt = require('./../libraries/nwt.js');

var formHelper = global.nwt.load().helper('Form');

// Make a context stub
// TODO: Rip this out into a test utility
global.context = function() {
return {clientScripts : []};
};

vows.describe('FormHelper').addBatch({
'FormHelper::generate': {
'with no elements': {
topic: formHelper,

'Proper markup': function (helper) {
assert.equal(helper.generate().render(), '<form method="POST" action="#"></form>');
}
},
'with elements': {
topic: formHelper,

'Proper markup': function (helper) {
var markup = helper.generate(
formHelper.field('Test.field', {type: 'text'})
);
assert.equal(markup.render(), '<form method="POST" action="#"><div class="row text"><label for="TestField">Test.field</label><input type="text" value="" id="TestField" name="Test[field]"></div></form>');
}
}
}
}).addBatch({
'FormHelper::field': {
'Test generic form fields': {
topic: formHelper,

'Default text input': function (helper) {
var markup = helper.field('Key.value');
assert.equal(markup.render(), '<div class="row text"><label for="KeyValue">Key.value</label><input type="text" value="" id="KeyValue" name="Key[value]"></div>');
},

'Text input': function (helper) {
var markup = helper.field('Key.value', {type: 'text'});
assert.equal(markup.render(), '<div class="row text"><label for="KeyValue">Key.value</label><input type="text" value="" id="KeyValue" name="Key[value]"></div>');
},

'Textarea': function (helper) {
var markup = helper.field('Key.value', {type: 'textarea'});
assert.equal(markup.render(), '<div class="row textarea"><label for="KeyValue">Key.value</label><textarea type="textarea" id="KeyValue" name="Key[value]"></textarea></div>');
},

'Select (no options)': function (helper) {
var markup = helper.field('Key.value', {type: 'select'});
assert.equal(markup.render(), '<div class="row select"><label for="KeyValue">Key.value</label><select type="select" id="KeyValue" name="Key[value]"></select></div>');
},

'Select (with options)': function (helper) {
var markup = helper.field('Key.value', {type: 'select', options: {0: '1', 1: '2'}});
assert.equal(markup.render(), '<div class="row select"><label for="KeyValue">Key.value</label><select type="select" id="KeyValue" name="Key[value]"><option value="0" selected>1</option><option value="1">2</option></select></div>');
},

'Multiselect': function (helper) {
var markup = helper.field('Key.value', {type: 'select', multiple: true});
assert.equal(markup.render(), '<div class="row select"><label for="KeyValue">Key.value</label><select type="select" multiple="true" id="KeyValue" name="Key[value]"></select></div>');
},

'Checkbox (unchecked)': function (helper) {
var markup = helper.field('Key.value', {type: 'checkbox'});
assert.equal(markup.render(), '<div class="row checkbox"><label for="KeyValue">Key.value</label><input type="checkbox" value="" id="KeyValue" name="Key[value]"></div>');
},

'Checkbox (checked)': function (helper) {
var markup = helper.field('Key.value', {type: 'checkbox', checked: true});
assert.equal(markup.render(), '<div class="row checkbox"><label for="KeyValue">Key.value</label><input type="checkbox" value="" checked="true" id="KeyValue" name="Key[value]"></div>');
},

'Radio': function (helper) {
var markup = helper.field('Key.value', {type: 'radio'});
assert.equal(markup.render(), '<div class="row radio"><label for="KeyValue">Key.value</label><input type="radio" value="" id="KeyValue" name="Key[value]"></div>');
},

'checkboxgroup': function (helper) {
var markup = helper.field('Key.value', {type: 'checkboxgroup', options: {0: '1', 1: '2'}});
assert.equal(markup.render(), '<div class="row checkboxgroup"><label for="KeyValue">Key.value</label><div class="grouped"><div class="row checkbox"><label for="KeyValue[0]">1</label><input type="checkbox" value="0" value="0" label="1" name="Key[value][0]" id="KeyValue[0]"></div><div class="row checkbox"><label for="KeyValue[1]">2</label><input type="checkbox" value="1" value="1" label="2" name="Key[value][1]" id="KeyValue[1]"></div></div></div>');
},

'radiogroup': function (helper) {
var markup = helper.field('Key.value', {type: 'radiogroup', options: {0: '1', 1: '2'}});
assert.equal(markup.render(), '<div class="row radiogroup"><label for="KeyValue">Key.value</label><div class="grouped"><div class="row radio"><label for="KeyValue[0]">1</label><input type="radio" value="0" value="0" label="1" name="Key[value]" id="KeyValue[0]"></div><div class="row radio"><label for="KeyValue[1]">2</label><input type="radio" value="1" value="1" label="2" name="Key[value]" id="KeyValue[1]"></div></div></div>');
},

'File': function (helper) {
var markup = helper.field('Key.value', {type: 'radio'});
assert.equal(markup.render(), '<div class="row radio"><label for="KeyValue">Key.value</label><input type="radio" value="" id="KeyValue" name="Key[value]"></div>');
},

'Submit': function (helper) {
var markup = helper.submit('Submit');
assert.equal(markup + '', '<input type="submit" value="Submit">');
},

'Before | Between | After': function (helper) {
var markup = helper.field('Key.value', {type: 'text'})
.before('Before')
.between('Between')
.after('After');

assert.equal(markup.render(), '<div class="row text">Before<label for="KeyValue">Key.value</label>Between<input type="text" value="" id="KeyValue" name="Key[value]">After</div>');
},
}
}
}).export(module);
145 changes: 145 additions & 0 deletions _core/tests/TestHtmlHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
var vows = require("vows"),
assert = require("assert"),
nwt = require('./../libraries/nwt.js');

var htmlHelper = global.nwt.load().helper('Html');

vows.describe('HtmlHelper').addBatch({
'HtmlHelper::link': {
'With some content': {
topic: htmlHelper.link('Content', '#'),

'Proper markup': function (topic) {
assert.equal(topic + '', '<a href="#">Content</a>');
}
},
'with a class': {
topic: htmlHelper.link('Content', '#', {class: 'awesome'}),

'has a class of "awesome"': function (topic) {
assert.equal(topic + '', '<a href="#" class="awesome">Content</a>');
}
}
}
}).addBatch({
'HtmlHelper::image': {
'With src': {
topic: htmlHelper.image('/'),

'proper markup': function (topic) {
assert.equal(topic + '', '<img src="/">');
}
},
'with width/height settings': {
topic: htmlHelper.image('/', {height:20, width:20}),

'proper markup': function (topic) {
assert.equal(topic + '', '<img src="/" height="20" width="20">');
}
}
}
}).addBatch({
'HtmlHelper::list': {
'with no items': {
topic: htmlHelper.list(),

'proper markup': function (topic) {
assert.equal(topic + '', '<ul></ul>');
}
},
'with several items': {
topic: htmlHelper.list('Item 1', 'Item 2'),

'proper markup': function (topic) {
assert.equal(topic + '', '<ul><li>Item 1</li><li>Item 2</li></ul>');
}
}
}
}).addBatch({
'HtmlHelper Standard Elements': {
'h1': {
topic: htmlHelper.h1('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<h1>Content</h1>');
}
},
'h2': {
topic: htmlHelper.h2('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<h2>Content</h2>');
}
},
'h3': {
topic: htmlHelper.h3('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<h3>Content</h3>');
}
},
'h4': {
topic: htmlHelper.h4('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<h4>Content</h4>');
}
},
'h5': {
topic: htmlHelper.h5('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<h5>Content</h5>');
}
},
'h6': {
topic: htmlHelper.h6('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<h6>Content</h6>');
}
},
'p': {
topic: htmlHelper.p('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<p>Content</p>');
}
},
'blockquote': {
topic: htmlHelper.blockquote('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<blockquote>Content</blockquote>');
}
},
'code': {
topic: htmlHelper.code('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<code>Content</code>');
}
},
'em': {
topic: htmlHelper.em('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<em>Content</em>');
}
},
'div': {
topic: htmlHelper.div('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<div>Content</div>');
}
},
'pre': {
topic: htmlHelper.pre('Content'),

'proper markup': function (topic) {
assert.equal(topic + '', '<pre>Content</pre>');
}
}
}
}).export(module);
33 changes: 33 additions & 0 deletions _core/tests/TestNumberHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var vows = require("vows"),
assert = require("assert"),
nwt = require('./../libraries/nwt.js');

var numberHelper = global.nwt.load().helper('Number');

vows.describe('numberHelper').addBatch({
'NumberHelper::format': {
'with no elements': {
topic: numberHelper,

'Format whole': function (helper) {
assert.equal(helper.format(100), 100);
},

'Format thousands': function (helper) {
assert.equal(helper.format(1000), '1,000');
},

'Format decimals': function (helper) {
assert.equal(helper.format(100.123), '100.12');
},

'Format thousands and decimals': function (helper) {
assert.equal(helper.format(1000.999), '1,000.99');
},

'Custom format': function (helper) {
assert.equal(helper.format(123456.987, 1, "|", ">"), '123>456|9');
}
}
}
}).export(module);
Loading

0 comments on commit f2c2b8b

Please sign in to comment.