How can I test a Javascript callback function of an AJAX transaction with RSpec/Capybara/Ruby?

Viewed 73

I am using RSpec/Capybara and chrome/firefox for testing.

I am using PayPal's iframe/Javascript purchasing flow.

When the user completes the purchase transaction in the pop-up browser window, that window closes and the callback function inside the button/registered function is called (sorry for the bad description - I'm not a Javascript expert).

Here's what the PayPal function looks like:

function paypal_button(plan_id, container_id) {
  paypal.Buttons({
    createSubscription: function(data, actions) {
      return actions.subscription.create({
        'plan_id': plan_id
      });
    },
    onCancel: function (data) {
      console.log("canceled");
    },
    onError: function (err) {
      console.log("error")
    },
    onApprove: function(data, actions) {
        stuff
    }
  }).render(container_id);
}

In the above code, stuff is doing a bunch of modifications to the DOM including submitting a form. I don't think I can intercept the Javascript before the form submission, so, really, I just need to understand what I can do in the RSpec test to cause this onApprove function to fire.

onApprove is documented here: https://developer.paypal.com/docs/checkout/integrate/#5-capture-the-transaction

Am I going about this all wrong? Should I be doing something completely different for testing this level of in-page Javascript?

0 Answers
Related