# frozen_string_literal: true RSpec.describe "catalog/_show_tools.html.erb" do let(:document) { SolrDocument.new id: 'xyz', format: 'a' } let(:blacklight_config) { Blacklight::Configuration.new } before do assign :response, instance_double(Blacklight::Solr::Response, params: {}) allow(view).to receive(:blacklight_config).and_return blacklight_config allow(view).to receive(:has_user_authentication_provider?).and_return false end context 'without passing in a document local explicitly' do before do assign :document, document end let(:document_actions) { blacklight_config.show.document_actions } it 'defaults to the @document and renders the action' do allow(view).to receive(:some_action_solr_document_path).with(document, any_args).and_return 'x' document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action' render 'catalog/show_tools' expect(rendered).to have_link "Some action", href: "x" end end describe "document actions" do let(:document_actions) { blacklight_config.show.document_actions } it "renders a document action" do allow(view).to receive(:some_action_solr_document_path).with(document, any_args).and_return 'x' document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action' render 'catalog/show_tools', document: document expect(rendered).to have_link "Some action", href: "x" end it "uses the provided label" do allow(view).to receive(:some_action_solr_document_path).and_return "x" document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, label: "Some label", partial: 'document_action' render 'catalog/show_tools', document: document expect(rendered).to have_selector '.some_action', text: "Some label" end it "evaluates a document action's if configurations" do allow(view).to receive(:some_action_solr_document_path).and_return "x" document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, if: false, partial: 'document_action' render 'catalog/show_tools', document: document expect(rendered).not_to have_selector '.some_action', text: "Some action" end it "evaluates a document action's if configuration with a proc" do allow(view).to receive(:some_action_solr_document_path).and_return "x" document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', if: proc { |_config, doc| doc.id == "xyz" } render 'catalog/show_tools', document: document expect(rendered).not_to have_selector '.some_action', text: "Some action" end it "evaluates a document action's unless configurations" do allow(view).to receive(:some_action_solr_document_path).and_return "x" document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', unless: true render 'catalog/show_tools', document: document expect(rendered).not_to have_selector '.some_action', text: "Some action" end it "allows the tool to have a custom id" do allow(view).to receive(:some_action_solr_document_path).and_return "x" document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', id: "some_action" render 'catalog/show_tools', document: document expect(rendered).to have_selector '#some_action', text: "Some action" end it "defaults to modal behavior" do allow(view).to receive(:some_action_solr_document_path).and_return "x" document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action' render 'catalog/show_tools', document: document expect(rendered).to have_selector '.some_action > a[data-blacklight-modal="trigger"]', text: "Some action" end it "allows configuration to opt out of modal behavior" do allow(view).to receive(:some_action_solr_document_path).and_return "x" document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', modal: false render 'catalog/show_tools', document: document expect(rendered).not_to have_selector '.some_action > a[data-blacklight-modal="trigger"]', text: "Some action" end context 'without any document actions defined' do before do document_actions.clear end it 'does not display the tools' do render 'catalog/show_tools', document: document expect(rendered).to be_blank end end end end