require 'rails_helper'

RSpec.describe CommonHelper, type: :helper do
  describe 'validation error' do
    let(:resource) { build(:dam_resource, title: nil, status: :under_review) }

    before { resource.valid? }

    it 'returns true' do
      expect(helper.field_error?(resource, :title)).to be true
    end

    it 'returns error' do
      expect(helper.field_error_class(resource, :title)).to eq 'error'
    end

    it 'returns content_tag with error' do
      expect(helper.field_error(resource, :title)).to match(/<span class=\"float-right error\"><i>can&#39;t be blank<\/i><\/span>/)
    end

    it 'returns content_tag with error' do
      resource.update(title: 'this is a very long string that is more than 40 characters so it raises validatin error')
      resource.valid?

      expect(helper.field_error(resource, :title)).to match(/<span class=\"float-right error\"><i>max characters limit is 40<\/i><\/span>/)
    end
  end

  describe 'validation success' do
    # factory has all fields filled
    let(:resource) { build(:dam_resource) }

    before { resource.valid? }

    it 'returns false' do
      expect(helper.field_error?(resource, :title)).to be false
    end

    it 'returns nil' do
      expect(helper.field_error(resource, :title)).to be nil
    end
  end
end
