capybara-angular icon indicating copy to clipboard operation
capybara-angular copied to clipboard

Not waiting for $http

Open nicooga opened this issue 10 years ago • 1 comments

I'm using ui.router and angularjs-rails-resource.

The page I'm visiting has a directive that performs a query to the local API to get some models that populate a select for a form:

CarBrand.query().then(function(carBrands) {
  vm.carBrands = carBrands; // this never gets executed, the model is empty
});

I've opened the page with save_and_open_page to check the rendering state of the app, and the route is resolved (even without using the waiter mentioned in this other issue) and the form is rendered, but the select options are not populated.

I've run out of clues. I checked if there are pending requests as suggested here but the answer is no, there are no pending requests.

However, when I quit, in the last second the requests appear en in the network tab of firefox, as if something was holding them.

Test:

require "rails_helper"

describe "CarListing creation", type: :feature do
  let!(:car_version) { FactoryGirl.create :car_version }

  it "creates a CarListing", js: true do
    visit "/#/avisos/nuevo"

    wait_until_angular_ready
    wait_for_route_changes

    save_and_open_page

    select_by_ng_model "cL_EC.carListing.car.brand", car_version.brand.name
    select_by_ng_model "cL_EC.carListing.car.model", car_version.model.name
    select_by_ng_model "cL_EC.carListing.car.versionId", car_version.id
    fill_in_by_ng_model "cL_EC.carListing.car.year", 1996
  end

  def fill_in_by_ng_model(ng_model, value)
    find_by_ng_model(ng_model).set value
  end

  def select_by_ng_model(ng_model, value)
    find_by_ng_model(ng_model).
    find(:css, "option[value='#{value}']").
    select_option
  end

  def find_by_ng_model(ng_model)
    find(:css, "[ng-model='#{ng_model}']")
  end
end

I tried https://github.com/kikonen/capybara-ng with the same results. AFAIK, this library angularjs-rails-resource is just a wrapper around $http calls.

Any help is aprettiated!

nicooga avatar Feb 22 '16 20:02 nicooga

Have you tried putting the query in the controller of the page instead of your directive? is your directive even going through the linking or not at all? sounds to me like your directive is compiled later than when you request it. I personally have a routeProvider and some directives doing some loading and it works fine without waiting for angular at all, even before I used this library, so prob the problem lies somewhere else

mebibou avatar Apr 19 '16 02:04 mebibou