module ShoppingCart
  feature 'checkout step :address', js: true do
    let!(:order) { create(:order) }
    let(:order_items) { create_list(:order_item, order: order) }
    let(:billing_address) { create(:billing_address) }
    let(:shipping_address) { create(:billing_address) }

    before do
      visit shopping_cart.order_checkout_path(order, :address)
    end

    scenario 'it will be address steep' do
      expect(current_path).to include('address')
      expect(page).to have_content(I18n.t('address.billing_address'))
      expect(page).to have_content(I18n.t('address.shipping_address'))

      within('.billing-address-box') do
        expect(find('.address-inputs')).to have_content(I18n.t('address.first_name'))
        expect(find('.address-inputs')).to have_content(I18n.t('address.last_name'))
        expect(find('.address-inputs')).to have_content(I18n.t('address.street_address'))
        expect(find('.address-inputs')).to have_content(I18n.t('address.city'))
        expect(find('.address-inputs')).to have_content(I18n.t('address.country'))
        expect(find('.address-inputs')).to have_content(I18n.t('address.zip'))
        expect(find('.address-inputs')).to have_content(I18n.t('address.phone'))
        expect(find('.address-inputs')).to_not have_content(I18n.t('address.same_address'))
      end

      within('.shipping-address-box') do
        expect(page).to have_content(I18n.t('address.same_address'))
      end
    end

    scenario 'it will fill only delivery address' do
      within('.billing-address-box') do
        fill_in 'first-name-input', with: billing_address.first_name
        fill_in 'flast-name-input', with: billing_address.last_name
        fill_in 'street-address-input', with: billing_address.street
        fill_in 'city-input', with: billing_address.city
        select 'Ukraine', from: 'order[billing_address_attributes][country]'
        fill_in 'zip-input', with: billing_address.zip
        fill_in 'phone-input', with: billing_address.phone
      end
      uncheck I18n.t('address.same_address')

      click_button(I18n.t('checkout.next_step'))
      expect(current_path).to include('address')
      expect(page).to have_text("can't be blank")
    end

    scenario 'it will add shipping address without fill inputs' do
      within('.billing-address-box') do
        fill_in 'first-name-input', with: billing_address.first_name
        fill_in 'flast-name-input', with: billing_address.last_name
        fill_in 'street-address-input', with: billing_address.street
        fill_in 'city-input', with: billing_address.city
        select 'Brazil', from: 'order[billing_address_attributes][country]'
        fill_in 'zip-input', with: billing_address.zip
        fill_in 'phone-input', with: billing_address.phone
      end
      check I18n.t('address.same_address')
      click_button(I18n.t('checkout.next_step'))
      expect(current_path).to include('delivery')
      expect(page).to_not have_text("can't be blank")
    end

    scenario 'user can not jump to next steps' do
      click_link(I18n.t('delivery.delivery'))
      click_link(I18n.t('credit_card.payment'))
      click_link(I18n.t('checkout.confirm'))
      click_link(I18n.t('checkout.complete'))
      expect(current_path).to include('address')
    end
  end
end