Feature spec

Always aiming to write readable specs

require 'spec_helper'

feature 'Consumer adds additional info' do

  include FullformSetupHelper
  include AutoPoliciesHelper, FullformAdditionalStepHelper, ExitPageHelper

  let!(:policy)  { FactoryGirl.create(:auto_policy, :with_additional_auto_flag, arrival: arrival) }
  let!(:vehicle) { FactoryGirl.create(:polk_vehicle, :with_year_make_and_model) }
  let!(:insurer) { FactoryGirl.create(:insurer, :with_insurer_insurance_type) }
  let(:additional_step_path)  { '/auto_policies/fullform/s2_additional' }
  let(:p) { PageObject.new }

  before :each do
    setup_fullform_foundation
    setup_pages
    stub_lead_buyer_predictor
    FactoryGirl.create_list(:tip, 5)
    FactoryGirl.create_list(:auto_insurance_term, 5)

    visit_ready auto_policies_path(current_step: additional_step_path)
  end

  context 'consumer adds a driver', :options do
    context 'correctly', :happy do
      scenario 'sees the name of the new driver on the form', :js do
        first_name, last_name = Faker::Name.first_name, Faker::Name.last_name

        add_additional_driver(first_name, last_name)

        expect(page).to have_content(first_name)
        expect(page).to have_content(last_name)
        expect(policy.drivers.reload.length).to eq 2
      end
    end

    context 'incorrectly', :sad do
      scenario 'does not persist a driver to the database', :js do
        expect{ add_invalid_driver }.to_not change{ Driver.count }
      end

      scenario 'sees field_with_errors', :js do
        add_invalid_driver

        expect(page).to have_error_fields
      end
    end
  end

  context 'consumer adds a vehicle', :js, :options do
    context 'correctly', :happy do
      scenario 'sees the vehicle year-make-model for the added vehicle' do
        add_additional_vehicle(vehicle.year, vehicle.make, vehicle.model)

        expect(page).to have_content("#{vehicle.year} #{vehicle.make} #{vehicle.model}")
        expect(policy.autos.reload.length).to eq(2)
      end
    end

    context 'incorrectly', :sad do
      scenario 'does not persist an auto to the database' do
        expect{ add_invalid_additional_vehicle }.to_not change{ Auto.count }
      end

      scenario 'sees field_with_errors' do
        add_invalid_additional_vehicle

        expect(page).to have_error_fields
      end
    end
  end

  context 'consumer adds a violation', :options do
    context 'correctly', :happy do
      scenario 'persists the additional violation', :js do
        add_valid_violation
        wait_for_ajax

        expect(Violation.count).to eq(1)
      end

      scenario 'sees the violation added', :js do
        add_valid_violation

        expect(page).to have_content('Violation 1')
      end
    end

    context 'incorrectly', :sad do
      scenario 'sees field_with_errors', :js do
        add_invalid_violation

        expect(page).to have_error_fields
      end

      scenario 'does not persist a violation to the database', :js do
        expect{ add_invalid_violation }.to_not change{ Violation.count }
      end
    end
  end

  context 'allow only one driver association form', :js, :options do
    before :each do
      visit_ready auto_policies_path(current_step: additional_step_path)
    end

    scenario 'hides add buttons' do
      find(p.css_violation_add).click

      expect(page).not_to have_selector p.css_violation_add, visible: true
    end

    scenario 'brings back add buttons on cancel' do
      find(p.css_add_driver).click
      with_ajax_wait{ find(p.css_remove_driver2).click }

      expect(page).to have_selector p.css_add_driver, visible: true
    end

    scenario 'brings back add buttons on successful save' do
      add_valid_violation

      expect(page).to have_selector p.css_add_claim
    end

    scenario 'does not bring back add buttons on failed save' do
      add_invalid_violation

      expect(page).not_to have_selector p.css_add_claim, visible: true
    end
  end
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s