5

I have a nested form that has 4 checkboxes. Currently, everything is working in browser, but I can't get the capybara tests to uncheck the checkbox and save.

Using Rails 4.2.2 and latest versions of capaybara-webkit and rspec

settings.html.erb

  <%= f.fields_for :preferences do |f| %>
    <div class="email-notifications-holder">
      <div class="email-holder">
        <%= f.label :new_match, "Getting a new match each week" %>
        <%= f.check_box :new_match, class: "checkbox new_match_email" %>
      </div>
      <div class="email-holder">
        <%= f.label :match_reminder, "New matches Thursday reminder", class: "match_reminder_email" %>
        <%= f.check_box :match_reminder, default: true, class: "checkbox"   %>
      </div>
      <div class="email-holder">
        <%= f.label :accepted_match, "A Glassbreakers accepted a match", class: "accepted_match_email" %>
        <%= f.check_box :accepted_match, default: true, class: "checkbox"   %>
      </div>
      <div class="email-holder">
        <%= f.label :new_message, "Received a new message", class: "new_message_email" %>
        <%= f.check_box :new_message, default: true, class: "checkbox"  %>
      </div>
    </div>
  <% end %>

edit_account_spec.rb

  it "allows the user to opt out of new match email", :js do
    user = create(:user)
    preferences = create(:preference, user: user)
    sign_in(user)

    visit edit_user_path(user)
    click_tab(t("edit_user.tabs.settings"))

    find(:css, "#user_preferences_attributes_0_new_match").set(false)

    within "#button-container" do
      page.find('.save.main-save-button-edit').trigger('click')
    end



    visit edit_user_path(user)
    click_tab(t("edit_user.tabs.settings"))

    user.preferences.reload
    new_match_email_checkbox = find(".new_match_email")
    expect(new_match_email_checkbox.checked?).to be_falsey
  end

I've tried clicking it, unchecking it, checking it, trigger clicking it, wrapping it around a within block, reloading the db, etc.

    new_match_email_checkbox = find(".new_match_email")
      within(".email-notifications-holder") do
      page.uncheck('Getting a new match each week')
    end

    new_match_email_checkbox.set(false)
LMo
  • 1,429
  • 2
  • 15
  • 32
  • And how do you know it is not unchecking this field? Is there some exception, unexpected behaviour? Have you tried to `save_and_open_page`? – BroiSatse Jul 02 '15 at 23:24
  • @BroiSatse yes, i've tried save_and_open_page - when it opens the page the checkbox is still checked! – LMo Jul 06 '15 at 02:59
  • your example has some mangled code... that includes a fine and a within as well as the uncheck... I can't tell if you've tried just the basic uncheck without that extra guff. eg just plain: `page.uncheck('Getting a new match each week')` – Taryn East Jul 06 '15 at 04:33

2 Answers2

2

Right now when you save a user's profile, you must have onboard skills saved or else it will throw an error message when you're trying to click the save button.

part of the user controller

  def update
    if update_current_user?(user_params)
      redirect_to user_path(current_user)
    else
      flash["notice"] =
        "Please choose 3 industries, fields and years of experience."
      redirect_to edit_user_path(current_user)
    end
  end

  private

  def update_current_user?(update_params)
    skills_chosen?(update_params[:user_onboard_skills_attributes]) &&
      current_user.update(update_params)
  end

Using save_and_open_page, the error alert wasn't appearing so it was unclear what was happening. I was able to debug this by trailing the logs while running the tests using:

tail -f log/test.log
LMo
  • 1,429
  • 2
  • 15
  • 32
1

Just using this will uncheck the checkbox

   within(".email-notifications-holder") do
      page.uncheck('Getting a new match each week')
   end

But you then have to grab the element to test it.

new_match_email_checkbox = find(".new_match_email")
expect(new_match_email_checkbox.checked?).to be_falsey

Note: One thing I am unclear about. Are you trying to make this line work?:

find(:css, "#user_preferences_attributes_0_new_match").set(false) 

or are you trying to uncheck the checkbox after you call user.preferences.reload ?

Uri Mikhli
  • 690
  • 6
  • 19
  • I was showing a few options how I was trying to go about it. The find(:css, "#user_preferences_attributes_0_new_match").set(false) line was a test. I also was reloading the database to ensure it saved. If you look at the answer below, I figured out why it wasn't saving. – LMo Jul 08 '15 at 19:01