Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capybara-angular does not wait for route changes #17

Open
alexspark opened this issue Feb 10, 2015 · 2 comments
Open

capybara-angular does not wait for route changes #17

alexspark opened this issue Feb 10, 2015 · 2 comments

Comments

@alexspark
Copy link

I am testing a login page, and after a successful login, I should be redirected to the dashboard page.
I am using the ng-routes module, not the angular-ui-router. My test for asserting this route change always fails.
my assertion looks like this:

expect(current_url).to eq("#{@app_host}/#/dashboard")

I see that the way capybara-angular checks to see if angular is ready is just by counting outstanding XHR requests.I think waiting for route changes should be a part of what this library covers.

@joshnuss
Copy link
Contributor

Agree this would be useful. Though it should only work in ngRoute or ui-router is being used.

This is what I do in my app currently:

# spec/support/route_changes.rb
module RouteChange
  def wait_for_route_changes
    page.evaluate_script <<-JS
      angular.element(document).ready(function() {
        var app = angular.element(document.querySelector('[ng-app], [data-ng-app]'));
        var injector = app.injector();

        injector.invoke(function($rootScope) {
          $rootScope.$on("$routeChangeStart", function() {
            window.angularReady = false;
          });

          var ready = function() { window.angularReady = true };

          $rootScope.$on("$routeChangeSuccess", ready);
          $rootScope.$on("$routeChangeError", ready);
        });
      });
    JS
  end
end

Then include it in spec_helper:

config.include Capybara::Angular::DSL
config.include RouteChange, type: :feature

And in my feature spec:

scenario "search for a product" do
  visit root_path
  wait_for_route_changes

  within :css, "form[name=SearchForm]" do
    fill_in "keywords", with: "shirt"
    click_on "Search"
  end

  expect(page.all('.product-listing .product').size).to eq(1)
end

@alexspark
Copy link
Author

I agree with it as an option to opt-in for.
I was thinking that one can check for the specific module in the page

if(angular.module('ngRoute')) then register the listeners

There might be a better way to check if the application is using ngRoute, and
it would seem just as seamless as the Capybara::Angular::DSL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants