describe LinksController do
  let(:user) { create(:user) }

  describe "GET links#index", type: :request, format: :html do
    it "will succeed and redirect" do
      get links_path(as: user)
      expect(response.status).to eq(200)
    end

    it "will redirect a non logged in user" do
      get links_path
      expect(response.status).to eq(302)
    end

    it "will succeed with oldest params" do
      get links_path(as: user, oldest: true)
      expect(response.status).to eq(200)
    end
  end

  describe "POST links#create", type: :request do
    before(:each) do
      get links_path(as: user)
    end

    it "will succeed with good params" do
      expect {
        post "/links", params: { link: {address: "http://helloworld.com"} }
      }.to change(Link, :count)
    end

    it "will fail with bad params" do
      expect {
        post "/links", params: { link: {url: ""} }
      }.to_not change(Link, :count)
    end

    it "will redirect" do
      post "/links", params: { link: {address: "http://helloworld.com"} }
      expect(response.status).to eq(302)
    end
  end

  describe "GET links#show", type: :request do
    before(:each) do
      link = create(:link, user: user)
      get link_path(link, as: user)
    end

    it "will succeed with good params" do
      expect(response.status).to eq(200)
    end
  end

  describe "DELETE links#destroy", type: :request do
    before(:each) do
      link = create(:link, user: user)
      delete link_path(link, as: user)
    end

    it "will delete the link" do
      expect(Link.count).to eq(0)
    end

    it "will redirect" do
      expect(response.status).to eq(302)
    end
  end

  describe "PATCH links#update", type: :request do
    let(:link) { create(:link, user: user) }
    let(:patch!) { patch link_path(link, params: { public: !link.public }, as: user) }

    it "updates the public attribute" do
      expect { patch! }.to change { link.reload.public }
    end
  end
end
