require_relative "../shared"

RSpec.describe "reflected resource delete action" do
  include_context "resource action"

  context "resource is already defined" do
    let :frontend_test_case do
      "actions/delete"
    end

    let :path do
      "/posts/#{deletable.one.id}"
    end

    let :method do
      :delete
    end

    let :form do
      {
        view_path: "/",
        origin: "/"
      }
    end

    let :deletable do
      data.posts.create
    end

    before do
      deletable
    end

    context "reflected action is not defined in the existing resource" do
      let :reflected_app_def do
        Proc.new do
          resource :post, "/posts" do
          end
        end
      end

      it "defines the reflected action" do
        expect {
          response
        }.to change {
          data.posts.count
        }.by(-1)
      end
    end

    context "action is defined in the existing resource that matches the reflected action" do
      let :reflected_app_def do
        local = self
        Proc.new do
          resource :posts, "/posts" do
            delete do
              local.instance_variable_set(:@reflected_action, reflected_action)
              send "app"
            end
          end
        end
      end

      it "does perform the reflective delete" do
        expect {
          response
        }.not_to change {
          data.posts.count
        }
      end

      it "does not try to redirect" do
        expect(response[0]).to eq(200)
        expect(response[2]).to eq("app")
      end

      it "exposes reflected state" do
        response
        expect(@reflected_action).to be_instance_of(Pakyow::Reflection::Action)
      end
    end
  end
end
