require 'rails_helper'

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

  describe "#table_show_link" do
    it 'returns a valid result' do
      record = create 'workflow_instance_batch/scenario1', name: 'B1'
      allow(subject).to receive(:can?).and_return true
      subject.table_show_link(record).should eq %|
        <a class="table-link show ui mini basic primary icon button" href="/workflow_instance_batch/scenario1s/#{record.id}"><i class="folder open icon"></i></a>
      |.strip
    end

    it "does not return when there are no permissions" do
      record = create 'workflow_instance_batch/scenario1', name: 'B1'
      allow(subject).to receive(:can?).with(:show, record).and_return false
      subject.table_show_link(record).should be_blank
      subject.table_show_link(record, nil, authorized: true).should be_present
    end
  end

  describe "#table_edit_link" do
    it 'returns a valid result' do
      record = create 'workflow_instance_batch/scenario1', name: 'B1'
      allow(subject).to receive(:can?).and_return true
      subject.table_edit_link(record).should eq %|
        <a class="table-link edit ui mini basic yellow icon button" href="/workflow_instance_batch/scenario1s/#{record.id}/edit"><i class="write icon"></i></a>
      |.strip
    end

    it "does not return when there are no permissions" do
      record = create 'workflow_instance_batch/scenario1', name: 'B1'
      allow(subject).to receive(:can?).with(:update, record).and_return false
      subject.table_edit_link(record).should be_blank
      subject.table_edit_link(record, nil, authorized: true).should be_present
    end
  end

  describe '#table_destroy_link' do
    it 'returns a valid result' do
      record = create 'workflow_instance_batch/scenario1', name: 'B1'
      allow(subject).to receive(:can?).and_return true
      subject.table_destroy_link(record).should eq %|
        <a data-confirm="Are you sure you want to delete SC1 Batch #{record.presentation_name}?" class="table-link destroy ui mini negative icon button" rel="nofollow" data-method="delete" href="/workflow_instance_batch/scenario1s/#{record.id}"><i class="trash icon"></i></a>
      |.strip
    end

    it "does not return when there are no permissions" do
      record = create 'workflow_instance_batch/scenario1', name: 'B1'
      allow(subject).to receive(:can?).with(:destroy, record).and_return false
      subject.table_destroy_link(record).should be_blank
      subject.table_destroy_link(record, nil, authorized: true).should be_present
    end
  end
end
