Reputation: 2205
I was wondering what is best practice or at least a practice for using Jasmine to test javascript that requires remote libraries called on page load, but not in the app.
More specifically, I'm creating a backbone view for processing payments using stripe. Stripe recommends that you load their javascript in your layout from their servers.
But my tests don't have my layout, so when I try to do this
it("calls stripe token creation", function() {
stripeSpy = spyOn(Stripe, "createToken");
form.submit();
expect(stripeSpy).toHaveBeenCalled();
});
It gives the error.
Stripe is not defined
I'd rather not depend on remote libraries for my test, nor do I really want to go against stripe preferred method of relying on their source code. What would be the best way to approach this?
Upvotes: 5
Views: 957
Reputation: 110922
To mock out the Stripe
dependency you have to create a new Stripe
object with the function you want to call:
var Stripe = {createToken: sinon.spy()}
form.submit();
expect(Stripe.createToken).toHaveBeenCalled();
Upvotes: 3
Reputation: 3925
You can use Sinon.JS to mock\stub this out.
Check out http://sinonjs.org/
Example usage for jQuery's AJAX stub
it("makes a GET request for todo items", function () {
sinon.stub(jQuery, "ajax");
getTodos(42, sinon.spy());
assert(jQuery.ajax.calledWithMatch({ url: "/todo/42/items" }));
});
For yours I'd imagine you'd do something like sinon.stub(Stripe, "createToken")
Hope this helps.
Upvotes: 2