require 'rails_helper' RSpec.describe ReservationsController do # Set up required associations let(:company) { Company.create!(name: "Test Company", entity: "Corp", id_number: "123456") } let(:team) { Team.create!(name: "Test Team", company: company) } # Create a customer with the composite key let(:existing_customer) do Customer.create!( first_name: "John", surname: "Doe", original_phone: "123456789", phone: "123456789", company: company ) end let(:valid_attributes_with_existing_customer) do { company_id: company.id, team_id: team.id, customer_first_name: existing_customer.first_name, customer_surname: existing_customer.surname, customer_original_phone: existing_customer.original_phone, start_time: 1.day.from_now, end_time: 1.day.from_now + 1.hour } end let(:valid_attributes_with_new_customer) do { company_id: company.id, team_id: team.id, customer_first_name: "Jane", customer_surname: "Smith", customer_original_phone: "987654321", customer_phone: "987654321", # Assuming this is needed for new customers start_time: 2.days.from_now, end_time: 2.days.from_now + 1.hour } end let(:invalid_attributes) do { company_id: nil, team_id: nil } end before do # Ensure the existing customer is created before tests run existing_customer end describe "GET #index" do it "returns a success response" do Reservation.create! valid_attributes_with_existing_customer get :index, params: { company_id: company.id } expect(response).to be_successful end end describe "GET #new" do it "returns a success response" do get :new, params: { company_id: company.id } expect(response).to be_successful end end describe "POST #create" do context "with existing customer" do it "creates a new Reservation with an existing customer" do expect do post :create, params: { reservation: valid_attributes_with_existing_customer, company_id: company.id } end.to change(Reservation, :count).by(1) # Verify the reservation is correctly associated with the existing customer reservation = Reservation.last expect(reservation.customer).to eq(existing_customer) expect(reservation.company_id).to eq(company.id) expect(reservation.team_id).to eq(team.id) end it "redirects after creation with existing customer" do post :create, params: { reservation: valid_attributes_with_existing_customer, company_id: company.id } expect(response).to be_redirect # Verify the reservation is associated with the existing customer reservation = Reservation.last expect(reservation.customer).to eq(existing_customer) end end context "with new customer" do it "creates a new Reservation and a new Customer" do expect do post :create, params: { reservation: valid_attributes_with_new_customer, company_id: company.id } end.to change(Reservation, :count).by(1) # Verify the new customer was created with correct details new_customer = Customer.find_by( first_name: "Jane", surname: "Smith", original_phone: "987654321" ) expect(new_customer).not_to be_nil expect(new_customer.phone).to eq("987654321") expect(new_customer.company_id).to eq(company.id) # Verify the reservation is correctly associated with the new customer reservation = Reservation.last expect(reservation.customer).to eq(new_customer) expect(reservation.company_id).to eq(company.id) expect(reservation.team_id).to eq(team.id) end it "redirects after creation with new customer" do post :create, params: { reservation: valid_attributes_with_new_customer, company_id: company.id } expect(response).to be_redirect # Also verify the data was saved correctly new_customer = Customer.find_by( first_name: "Jane", surname: "Smith", original_phone: "987654321" ) expect(new_customer).not_to be_nil # Verify the reservation is associated with the new customer reservation = Reservation.last expect(reservation.customer).to eq(new_customer) end end context "with invalid params" do it "handles invalid attributes appropriately" do post :create, params: { reservation: invalid_attributes, company_id: company.id } expect(response.status).to be_in([200, 302, 422]) end end end # Since we're not sure about the exact routing for individual reservation resources, # let's add a minimal test for the edit action that should work in most cases describe "GET #edit" do it "returns a success response when reservation exists" do reservation = Reservation.create! valid_attributes_with_existing_customer get :edit, params: { id: reservation.id } expect(response).to be_successful end end end