Skip to content

Instantly share code, notes, and snippets.

@jaredmcateer
Created July 6, 2011 13:49
Show Gist options
  • Save jaredmcateer/1067262 to your computer and use it in GitHub Desktop.
Save jaredmcateer/1067262 to your computer and use it in GitHub Desktop.
var TitleTest = TestCase('TitleTest');
TitleTest.prototype.defaultTitle = 'title';
TitleTest.prototype.defaultCount = '0';
TitleTest.prototype.setUp = function() {
var titleObj;
this.div = new Element('div');
$$('body').first().insert(this.div);
this.div.insert('<div id="title"><h1><span id="titleCaption">' + this.defaultTitle + '</span><span id="titleCount">' + this.defaultCount + '</span></h1></div>');
// My title object sets up a few custom event listeners and handles
// changing the title of the page based on data passed with the event.
titleObj = new Title();
});
TitleTest.prototype.testNewItemsEvent = function() {
var data = {count: 10};
assertEquals('Count should be zero before events are fired', this.defaultCount, $('titleCount').innerHTML);
document.fire('custom:NewItems', data);
assertEquals('New count should be 10', data.count + '', $('titleCount').innerHTML);
});
// ... a few other simple tests like the one above
TitleTest.prototype.testUpdateSpecial = function() {
var data = {caption: 'Special Title', count: 10},
specialObj = {special: {type: 2, value: 5}};
// Emulate a special type of category, that can only be
// added at page load
document.fire('custom:UpdateTitle', data);
assertEquals(data.caption, $('titleCaption').innerHTML);
assertEquals(data.caption, $('titleCount').innerHTML);
//Removing the special category should revert the title to its default
document.fire('custom:RemoveSpecial', specialObj);
assertEquals(this.defaultTitle, $('titleCaption').innerHTML);
assertEquals(this.defaultCount, $('titleCount').innerHTML);
// only way to get this gets added back in during non page load is using
// javascript based history so it needs to revert to previous state
document.fire('custom:AddSpecial', specialObj);
assertEquals(data.caption, $('titleCaption').innerHTML);
assertEquals(data.count + '', $('titleCount').innerHTML);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment