require "rails_helper"

RSpec.describe NotesController, type: :controller do
  let(:current_user) { create(:user) }
  let(:releasable) { create(:appearance_release) }

  before do
    sign_in current_user
  end

  describe "#index" do
    it "responds successfully" do
      get :index, xhr: true, params: { "#{releasable.model_name.singular}_id" => releasable }

      expect(response).to be_successful
    end
  end

  describe "#new" do
    it "responds successfully" do
      get :new, xhr: true, params: { "#{releasable.model_name.singular}_id" => releasable }
    end
  end

  describe "#create" do
    it "responds successfully" do
      post :create, xhr: true, params: { "#{releasable.model_name.singular}_id" => releasable, note: note_params }

      expect(response).to be_successful
    end

    it "creates a new Note record" do
      expect {
        post :create, xhr: true, params: { "#{releasable.model_name.singular}_id" => releasable, note: note_params }
      }.to change(Note, :count).by(1)
    end

    context "when the new note is not valid" do
      before do
        allow_any_instance_of(Note).to receive(:save).and_return(false)
      end

      it "does not create a new Note record" do
        expect {
          post :create, xhr: true, params: { "#{releasable.model_name.singular}_id" => releasable, note: note_params }
        }.not_to change(Note, :count)
      end
    end
  end

  private

  def note_params
    attributes_for(:note)
  end
end
