require 'rails_helper'

RSpec.describe QuotationsController, type: :controller do

  describe "GET #new" do
    before { subject }

    subject { get :new }

    it { expect(response)       .to render_template(:new)      }
    it { expect(assigns(:user)) .to be_a(User)                 }
    it { expect(response)        .to have_http_status(:success) }
  end

  describe "POST #products_step" do
    let(:start_date)    { 1.month.ago }
    let(:end_date)      { 2.months.ago }
    let(:price_per_day) { 10.5 }

    let(:params) {
      {
        user: FactoryBot.attributes_for(:user),
        product: {
          product_type: Product::Subscription.name,
          start_date: start_date,
          end_date: end_date,
          price_per_day: price_per_day
        }
      }
    }

    context 'for product type subscription' do
      subject { post :products_step, params: params }

      context 'when it saves successfuly' do
        it { expect{subject}.to change{User.count}.by(1) }

        context 'check response' do
          before { subject }

          it { expect(response)         .to render_template(:products_step) }
          it { expect(response)         .to have_http_status(:success) }
          it { expect(assigns(:user))   .to be_a(User) }
          it { expect(assigns(:product)).to be_a(Product) }
        end
      end

      context 'when it has errors' do
        before  { params[:user][:name] = nil }

        it { expect{subject}.to_not change{User.count} }

        context 'check response' do
          before { subject }

          it { expect(response)                     .to render_template(:new) }
          it { expect(response)                     .to have_http_status(:success) }
          it { expect(assigns(:user).errors[:name]) .to_not be_blank }
        end
      end
    end
  end

end
