I'm having some serious difficulties trying to tests functions with arguments in javascript. How do I tests arguments?
This is my function:
NotificationHelper.prototype.addNotificationItems = function(validationErrors) {
this.get$NotificationItems().html(render('tmpl-NotificationView-notificationItems', {items: validationErrors}))
};
validationErrors is supposed to be an object and the function render renders the handlebar template in html code. It renders a bunch of li in a loop depending on how many properties there are in the object.
My question is what do I need to test for this argument? Do I need to test for all types?
//Filled object
notificationHelper.addNotificationItems({Street: 'Veuillez respecter le format requis.'});
expect($notificationItems.find('li').length).toEqual(1);
//Empty object
notificationHelper.addNotificationItems({});
expect($notificationItems.find('li').length).toEqual(0);
//Filled array
notificationHelper.addNotificationItems(['Street', 'Veuillez respecter le format requis.']);
expect($notificationItems.find('li').length).toEqual(0);
//Empty array
notificationHelper.addNotificationItems([]);
expect($notificationItems.find('li').length).toEqual(0);
//Undefined
notificationHelper.addNotificationItems(undefined);
expect($notificationItems.find('li').length).toEqual(0);
//Null
notificationHelper.addNotificationItems(null);
expect($notificationItems.find('li').length).toEqual(0);
//Boolean
notificationHelper.addNotificationItems(true);
expect($notificationItems.find('li').length).toEqual(0);
//Integer
notificationHelper.addNotificationItems(0);
expect($notificationItems.find('li').length).toEqual(0);
//String
notificationHelper.addNotificationItems('String');
expect($notificationItems.find('li').length).toEqual(0);
Or do I only make my function accept an object argument:
NotificationHelper.prototype.addNotificationItems = function(validationErrors) {
if(typeof validationErrors == Object)
this.get$NotificationItems().html(render('tmpl-NotificationView-notificationItems', {items: validationErrors}))
};
As you can see, I am lost... Also is this the good way to do it? Should I throw an error in the function if it's not the good type, instead of just checking it does not have the good behaviour?
notificationHelperhave its own$notificationItemsproperty?