require 'rails_helper'

feature 'Search for product' do
  let!(:product) { FactoryGirl.create(:product, name: 'Organic Cotton Romper') }

  background do
    FactoryGirl.create(:website)
    sign_in_as_admin
  end

  scenario 'Search for and navigate to product', js: true do
    given_i_am_on_the_product_index
    and_i_search_for 'Organic'
    when_i_click_on 'Organic Cotton Romper'
    then_i_should_be_on_the_edit_product_page
  end

  def given_i_am_on_the_product_index
    visit admin_products_path
  end

  def and_i_search_for query
    save_and_open_page
    fill_in 'query', with: query
    click_button 'Search'
  end

  def when_i_click_on link_text
    within(:css, '#search-results') do
      click_link link_text
    end
  end

  def then_i_should_be_on_the_edit_product_page
    expect(page).to have_css(:h1, text: 'Edit Product')
  end
end

require 'rails_helper'

feature 'Search for product' do
  let!(:product) { FactoryGirl.create(:product, name: 'Organic Cotton Romper') }

  background do
    FactoryGirl.create(:website)
    sign_in_as_admin
  end

  scenario 'Search for and navigate to product', js: true do
    given_i_am_on_the_product_index
    and_i_search_for 'Organic'
    when_i_click_on 'Organic Cotton Romper'
    then_i_should_be_on_the_edit_product_page
  end

  def given_i_am_on_the_product_index
    visit admin_products_path
  end

  def and_i_search_for query
    fill_in 'query', with: query
    click_button 'Search'
  end

  def when_i_click_on link_text
    within(:css, '#search-results') do
      click_link link_text
    end
  end

  def then_i_should_be_on_the_edit_product_page
    expect(page).to have_css(:h1, text: 'Edit Product')
  end
end

Remove debugging code in failing spec
Remove debugging code in failing spec
