I'm currently going through the book called 'Mastering Web Application Development with AngularJS' and in one of the examples there is a test called 'Aggregating callbacks'.
The example I've got the problem with contains the Person object:
var Person = function(name, $log) {
this.eat = function(food) {
$log.info(name + ' is eating delicious ' + food);
};
this.beHungry = function(reason) {
$log.warn(name + ' is hungry because: ' + reason);
};
};
The object called Restaurant:
var Restaurant = function($q, $rootScope) {
var currentOrder;
return {
takeOrder : function(orderedItems) {
currentOrder = {
deferred : $q.defer(),
items : orderedItems
};
return currentOrder.deferred.promise;
},
deliverOrder : function() {
currentOrder.deferred.resolve(currentOrder.items);
$rootScope.$digest();
},
problemWithOrder : function(reason) {
currentOrder.deferred.reject(reason);
$rootScope.$digest();
}
};
};
and lastly the test for the aggregating callback:
it('should allow callbacks aggregation', function() {
var pizzaPid = new Restaurant($q, $rootScope);
var pizzaDelivered = pizzaPid.takeOrder('Margherita');
pizzaDelivered.then(pawel.eat, pawel.beHungry);
pizzaDelivered.then(pete.eat, pete.beHungry);
pizzaPid.deliveryOrder();
expect($log.info.logs).toContain(['Pawel is eating delicious Margherita']);
expect($log.info.logs).toContain(['Pete is eating delicious Margherita']);
});
As you can see the test doesn't show how items are added / injected to the test and I'm new to the concept of TDD in general.
What I've ended up doing was to convert those global objects to service and factory:
angular.module('myApp', [])
.service('Person', function(personName, $log) {
this.eat = function(food) {
$log.info(personName + ' is eating delicious ' + food);
};
this.beHungry = function(reason) {
$log.warn(personName + ' is hungry because: ' + reason);
};
})
.factory('Restaurant', function($q, $rootScope) {
var currentOrder;
return {
takeOrder : function(orderedItems) {
currentOrder = {
deferred : $q.defer(),
items : orderedItems
};
return currentOrder.deferred.promise;
},
deliverOrder : function() {
currentOrder.deferred.resolve(currentOrder.items);
$rootScope.$digest();
},
problemWithOrder : function(reason) {
currentOrder.deferred.reject(reason);
$rootScope.$digest();
}
};
});
But now I'm struggling with the multiple instances of the service to represent 'pawel' and 'pete' in my test:
describe('Person and Restaurant tests', function() {
var Person;
var Restaurant;
var $q;
var $rootScope;
var $log;
beforeEach(function() {
module('myApp');
module(function($provide) {
$provide.value('personName', 'Pawel');
});
inject(function(_Person_, _Restaurant_, _$q_, _$rootScope_, _$log_) {
Person = _Person_;
Restaurant = _Restaurant_;
$q = _$q_;
$rootScope = _$rootScope_;
$log = _$log_;
});
});
it('should allow callbacks aggregation', function() {
var pizzaDelivered = Restaurant.takeOrder('Margherita');
// here's where the problem is
// with current set up I can only call it as
// pizzaDelivered.then(Person.eat, Person.beHungry);
pizzaDelivered.then(pawel.eat, pawel.beHungry);
pizzaDelivered.then(pete.eat, pete.beHungry);
Restaurant.deliveryOrder();
expect($log.info.logs).toContain(['Pawel is eating delicious Margherita']);
expect($log.info.logs).toContain(['Pete is eating delicious Margherita']);
});
});
As I said - I'm new to it and would appreciate some help.