require 'rails_helper'

RSpec.describe Dunlop::StateBadgeHelper, type: :helper do
  subject { helper }

  describe '#state_badge' do
    it 'returns an empty html_safe string when no target is present' do
      target = double(present?: false)
      expect( subject.state_badge target).to be_blank
      expect( subject.state_badge target ).to be_html_safe
    end

    it 'returns an empty html_safe string when no state is present' do
      target = create :owner_initialization_scenario1
      target.state = nil
      expect( subject.state_badge target ).to be_blank
      expect( subject.state_badge target ).to be_html_safe
    end

    it 'returns proper badge htm' do
      target = create :owner_initialization_scenario1
      result = subject.state_badge target
      result.should eq <<-HTML.strip_heredoc.split("\n").join
        <span class="display-badge owner_initialization scenario1 pending" data-id="#{target.id}" data-resource="owner_initialization/scenario1" data-state="pending">
        <span class="pre-display"></span>
        <span class="badge-name">Owner Init</span>
        <span class="human-state">Pending</span>
        <span class="post-display"></span>
        </span>
      HTML
      result.should be_html_safe
    end

    it 'can append text to the state' do
      target = FactoryBot.create :owner_initialization_scenario1
      expect( subject.state_badge target, state_append_text: ' (7)' ).to include %|<span class="human-state">Pending</span><span class="post-display"> (7)</span>|
    end

    it 'allows the state to be overridden by another valid state for the target and displays the translated version' do
      target = FactoryBot.create :owner_initialization_scenario1
      expect( subject.state_badge target, state: 'completed' ).to include 'Completed'
    end

    it 'works with decorated objects' do
      target = create(:workflow_instance_scenario1, :active).decorate
      expect( subject.state_badge target ).to eq <<-HTML.strip_heredoc.split("\n").join
        <span class="display-badge workflow_instance scenario1 active" data-id="#{target.id}" data-resource="workflow_instance/scenario1" data-state="active">
        <span class="pre-display"></span>
        <span class="badge-name">WI</span>
        <span class="human-state">Actief</span>
        <span class="post-display"></span>
        </span>
      HTML
    end

  end

  describe "#workflow_instance_state_badges_for" do
    it "returns in the proper state order (uncategorize, unplanned, planned, overdue, active, any_rejected, all_completed)" do
      create(:workflow_instance_scenario1).owner_initialization.reject!
      create(:workflow_instance_scenario1, :unplanned)
      create(:workflow_instance_scenario1).owner_initialization.process!
      expect( subject ).to receive(:current_user).and_return User.new

      result = subject.workflow_instance_state_badges_for(WorkflowInstance::Scenario1)
      document = Nokogiri::HTML(result)
      document.css('.human-state').map(&:text).should eq ["Unplanned", "Actief", "Any rejected"]
    end
  end

  describe "#workflow_step_state_badges_for" do
    it "returns in the proper state order (uncategorize, unplanned, planned, overdue, active, any_rejected, all_completed)" do
      create(:workflow_instance_scenario1).owner_initialization.process!
      create(:workflow_instance_scenario1, :unplanned)
      create(:workflow_instance_scenario1).owner_initialization.reject!
      expect( subject ).to receive(:current_user).and_return User.new

      result = subject.workflow_step_state_badges_for(OwnerInitialization::Scenario1)
      document = Nokogiri::HTML(result)
      document.css('.human-state').map(&:text).should eq ["Pending", "Actief", "Rejected"]
    end
  end
end
