Currently, I am working on writing a JavaScript test for a method that involves making two function calls (model.expandChildren() and view.update();).
// within the 'app' / 'RV.graph'
var expandChildren = function(){
return model.expandChildren().then(function(result) {
view.update(result);
});
};
I attempted to utilize Jasmine specs to test the functionality by spying on both the view and model functions. However, it seems that only one spy can be used in a single test. I feel like I might be missing something important here as there should be a way to mock multiple method calls using spies since my function relies on both of them.
My goal is for the spec to successfully execute the following code. Currently, the first test passes (the first spy works correctly), but the second test fails as Jasmine attempts to run the actual function instead of the spied one:
var model = GRAPH.model;
var view = GRAPH.view;
var app = RV.graph;
describe('#expandChildren', function() {
beforeEach(function() {
// first spy, functioning properly
spyOn(model, 'expandChildren').and.callFake(function() {
return new Promise(function(resolve) {
resolve(testResponse);
});
});
// second spy does not work due to Jasmine's limitation
spyOn(view, 'update');
app.expandChildren();
});
// successful test
it('calls model.expandChildren', function() {
expect(model.expandChildren).toHaveBeenCalled();
});
// unsuccessful test executing the REAL view.update function
it('calls view.update', function() {
expect(view.update).toHaveBeenCalled();
});
});
Is there a way to achieve this with Jasmine?