Closed tylerzika closed 4 years ago
I think your use case will need to use spyOn(f).and.returnValues()
https://jasmine.github.io/2.4/introduction.html#section-Spies:_%3Ccode%3Eand.returnValues%3C/code%3E
I haven't tried this myself, but the idea is to define n number of return values equivalent to the number of callouts your method will do. If you have 3 server calls when your component initializes, you would define three separate return values (in the order you would expect the callouts to be in).
i was in a similar situation testing 3 server calls but I had to make three server calls made in the same component and accomplished it by using action.getName() in the spyOn() callback and branch the response based on the action name.
Note: it does add branching logic to your test which can make your code more complex. Still kind of a newbie at Jasmine so I'm not sure if writing your test like this a good practice or not.
spyOn($A, "enqueueAction").and.callFake(function (action) {
var res = {
getState: function () {
return "SUCCESS";
}
};
var cb = action.getCallback("SUCCESS");
if (action.getName() == 'loadAccount') {
res.getReturnValue = function () {
return [{
"Name": "Acct 1",
}];
}
} else if (action.getName() == 'loadOrder') {
res.getReturnValue = function () {
return [{
"Name": "Order 1",
}];
}
};
cb.fn.apply(cb.s, [res]);
});
When my component loads, I make multiple calls to the server. I'm not sure how to test this. The server calls are as follows:
c:pageContainer -> getOrderInvite()
getOrderInvite() then loads the component
c:shippingAddressUpdateForm -> getShippingDestinationList()
getShippingDestinationList() -> getCountryStatePicklistValues()
So three server calls are enqueued in a chain when my app loads: getOrderInvite() -> getShippingDestinationList() -> getCountryStatePicklistValues()
This is my attempt on chaining spyOn, but it doesn't appear to work.
All these server calls need to happen in order to load
c:shippingAddressUpdateForm
into thev.body
ofc:pageContainer
, which is what I'm testing for.I'm currently getting