require "rails_helper"

RSpec.describe TagsHelper, type: :helper do
  describe "#button_to_bulk_tagging" do
    let(:project) { build_stubbed(:project) }

    subject { helper.button_to_bulk_tagging(project) }

    it "renders content" do
      expect(subject).to match("tag_all")
      expect(subject).to match(t("shared.tag_multiple_releases"))
    end

    it "routes to the bulk tagging path" do
      expect(subject).to match(new_bulk_tagging_path)
      expect(subject).to match("method=\"get\"")
    end

    it "includes project_id as a parameter" do
      expect(subject).to match("name=\"project_id\"")
      expect(subject).to match("value=\"#{project.id}\"")
    end

    it "includes a data attribute for the selected releasable ids" do
      expect(subject).to match("data-releasable-ids")
    end

    it "is disabled by default" do
      expect(subject).to match("data-remote=\"true\"")
    end

    it "disables when clicked" do
      expect(subject).to match("data-disable-with")
    end
  end

  describe "#tags_preview" do
    let(:appearance_release) { create(:appearance_release) }

    it "shows the names of tags if there are two or fewer tags" do
      appearance_release.tags.build(name: "Actor")
      appearance_release.tags.build(name: "Fresh")

      preview = helper.tags_preview(appearance_release, appearance_release.tags)

      expect(preview).to have_link("Actor", href: link_to_manage_tags(appearance_release))
      expect(preview).to have_link("Fresh", href: link_to_manage_tags(appearance_release))
      expect(preview).not_to have_link("1 more tag")
    end

    context "when there are no tags" do
      it "returns nothing" do
        preview = helper.tags_preview(appearance_release, [])

        expect(preview).to be_blank
      end
    end

    context "when there is more than two tag" do
      it "includes a link to an action that shows all tags" do
        appearance_release.tags.build(name: "Actor")
        appearance_release.tags.build(name: "Fresh")
        appearance_release.tags.build(name: "Debut")

        preview = helper.tags_preview(appearance_release, appearance_release.tags)
        
        expect(preview).to have_link("1 more tag", href: link_to_manage_tags(appearance_release))
      end
    end
  end

  private

  def link_to_manage_tags(taggable)
    url_for([:new, taggable, :acts_as_taggable_on_tag, locale: I18n.locale])
  end
end
