<commit_before>require 'rails_helper'

describe AdpFormBuilder do

  let(:resource)  { FactoryGirl.create :claim }
  let(:builder)   { AdpFormBuilder.new(:claim, resource, self, {} ) }


  describe 'collection_select2_with_data' do

    before(:each) do
      CaseType.delete_all
      @ct1 = FactoryGirl.create :case_type, name: "Case Type A", is_fixed_fee: true
      @ct2 = FactoryGirl.create :case_type, name: "Case Type B", is_fixed_fee: false
      @ct3 = FactoryGirl.create :case_type, name: "Case Type C", is_fixed_fee: true
      @case_types = [@ct1, @ct2, @ct3]
    end


    it 'should output select with data attributes on each option' do
      html = builder.collection_select2_with_data(:case_type_id, @case_types, :id, :name, {'is-fixed-fee' => :is_fixed_fee?}, { prompt: false } )
      expect(html).to eq(squash(expected_output_with_one_data_attribute))
    end
  end


  describe 'anchored_label' do
    context 'no anchor name supplied' do
      it 'should take the label as the anchor name' do
        expected_html = %Q[<a name="advocate_category"></a><label for="claim_advocate_category">Advocate category</label>]
        expect(builder.anchored_label('Advocate category')).to eq expected_html
      end
    end

    context 'anchor name supplied' do
      it 'should use anchor name supplied' do
        expected_html = %Q[<a name="ad_cat"></a><label for="claim_ad_cat">Advocate category</label>]
        expect(builder.anchored_label('Advocate category', 'ad_cat')).to eq expected_html
      end
    end

    context 'extra html attributes supplied' do
      it 'should use the attributes when anchor name supplied' do
        expected_html = %Q[<a name="ad_cat" class="red"></a><label class="blue" for="claim_ad_cat">Advocate category</label>]
        expect(builder.anchored_label('Advocate category', 'ad_cat', { anchor_attributes: {class: 'red'}, label_attributes: {class: 'blue'} })).to eq expected_html
      end

      it 'should use the attributes when no anchor name provided' do
        expected_html = %Q[<a name="advocate_category" class="red"></a><label class="blue" for="claim_advocate_category">Advocate category</label>]
        expect(builder.anchored_label('Advocate category', nil, { anchor_attributes: {class: 'red'}, label_attributes: {class: 'blue'} })).to eq expected_html
      end
    end
  end

  describe 'anchored_without_label' do
    context 'no anchor name supplied' do
      it 'should take the label as the anchor name' do
        expected_html = %Q[<a name="advocate_category"></a>]
        expect(builder.anchored_without_label('Advocate category')).to eq expected_html
      end
    end

    context 'anchor name supplied' do
      it 'should use anchor name supplied' do
        expected_html = %Q[<a name="ad_cat"></a>]
        expect(builder.anchored_without_label('Advocate category', 'ad_cat')).to eq expected_html
      end
    end

    context 'extra html attributes supplied' do
      it 'should use the attributes' do
        expected_html = %Q[<a name="ad_cat" class="red"></a>]
        expect(builder.anchored_without_label('Advocate category', 'ad_cat', anchor_attributes: {class: 'red'})).to eq expected_html
      end
    end
  end

  describe 'anchored_attribute' do
    it 'should build the label from the object and label' do
      expected_html = %Q[<a name="advocate_claim.test"></a>]
      expect(builder.anchored_attribute('test')).to eq expected_html
    end

    it 'should use any provided attributes' do
      expected_html = %Q[<a name="advocate_claim.test" class="red"></a>]
      expect(builder.anchored_attribute('test', anchor_attributes: {class: 'red'})).to eq expected_html
    end
  end
end

def expected_output_with_one_data_attribute
  html = <<EOS
    <select id="claim_case_type_id" name="claim[case_type_id]" class="select2">
      <option data-is-fixed-fee="true" value="#{@ct1.id}">Case Type A</option>
      <option data-is-fixed-fee="false" value="#{@ct2.id}">Case Type B</option>
      <option data-is-fixed-fee="true" value="#{@ct3.id}">Case Type C</option>
    </select>
EOS
end


def squash(html)
  html.gsub(/\s+\</, '<').chomp
end<commit_msg>Amend spec to use alternate select<commit_after>require 'rails_helper'

describe AdpFormBuilder do

  let(:resource)  { FactoryGirl.create :claim }
  let(:builder)   { AdpFormBuilder.new(:claim, resource, self, {} ) }


  describe 'collection_select2_with_data' do

    before(:each) do
      CaseType.delete_all
      @ct1 = FactoryGirl.create :case_type, name: "Case Type A", is_fixed_fee: true
      @ct2 = FactoryGirl.create :case_type, name: "Case Type B", is_fixed_fee: false
      @ct3 = FactoryGirl.create :case_type, name: "Case Type C", is_fixed_fee: true
      @case_types = [@ct1, @ct2, @ct3]
    end


    it 'should output select with data attributes on each option' do
      html = builder.select :case_type_id, @case_types.map{ |ct| [ct.name, ct.id, {data: {'is-fixed-fee' => ct.is_fixed_fee?}}] }, { include_blank: true }, { class: 'autocomplete' }
      expect(html).to eq(squash(expected_output_with_one_data_attribute))
    end
  end


  describe 'anchored_label' do
    context 'no anchor name supplied' do
      it 'should take the label as the anchor name' do
        expected_html = %Q[<a name="advocate_category"></a><label for="claim_advocate_category">Advocate category</label>]
        expect(builder.anchored_label('Advocate category')).to eq expected_html
      end
    end

    context 'anchor name supplied' do
      it 'should use anchor name supplied' do
        expected_html = %Q[<a name="ad_cat"></a><label for="claim_ad_cat">Advocate category</label>]
        expect(builder.anchored_label('Advocate category', 'ad_cat')).to eq expected_html
      end
    end

    context 'extra html attributes supplied' do
      it 'should use the attributes when anchor name supplied' do
        expected_html = %Q[<a name="ad_cat" class="red"></a><label class="blue" for="claim_ad_cat">Advocate category</label>]
        expect(builder.anchored_label('Advocate category', 'ad_cat', { anchor_attributes: {class: 'red'}, label_attributes: {class: 'blue'} })).to eq expected_html
      end

      it 'should use the attributes when no anchor name provided' do
        expected_html = %Q[<a name="advocate_category" class="red"></a><label class="blue" for="claim_advocate_category">Advocate category</label>]
        expect(builder.anchored_label('Advocate category', nil, { anchor_attributes: {class: 'red'}, label_attributes: {class: 'blue'} })).to eq expected_html
      end
    end
  end

  describe 'anchored_without_label' do
    context 'no anchor name supplied' do
      it 'should take the label as the anchor name' do
        expected_html = %Q[<a name="advocate_category"></a>]
        expect(builder.anchored_without_label('Advocate category')).to eq expected_html
      end
    end

    context 'anchor name supplied' do
      it 'should use anchor name supplied' do
        expected_html = %Q[<a name="ad_cat"></a>]
        expect(builder.anchored_without_label('Advocate category', 'ad_cat')).to eq expected_html
      end
    end

    context 'extra html attributes supplied' do
      it 'should use the attributes' do
        expected_html = %Q[<a name="ad_cat" class="red"></a>]
        expect(builder.anchored_without_label('Advocate category', 'ad_cat', anchor_attributes: {class: 'red'})).to eq expected_html
      end
    end
  end

  describe 'anchored_attribute' do
    it 'should build the label from the object and label' do
      expected_html = %Q[<a name="advocate_claim.test"></a>]
      expect(builder.anchored_attribute('test')).to eq expected_html
    end

    it 'should use any provided attributes' do
      expected_html = %Q[<a name="advocate_claim.test" class="red"></a>]
      expect(builder.anchored_attribute('test', anchor_attributes: {class: 'red'})).to eq expected_html
    end
  end
end

def expected_output_with_one_data_attribute
  html = <<EOS
    <select class="autocomplete" name="claim[case_type_id]" id="claim_case_type_id">
      <option value></option>
      <option data-is-fixed-fee="true" value="#{@ct1.id}">Case Type A</option>
      <option data-is-fixed-fee="false" value="#{@ct2.id}">Case Type B</option>
      <option data-is-fixed-fee="true" value="#{@ct3.id}">Case Type C</option>
    </select>
EOS
end


def squash(html)
  html.gsub(/\s+\</, '<').chomp
end
{'fraction_non_alphanumeric': 0.1272337383845604, 'fraction_numerical': 0.0026209197045508697, 'mean_word_length': 3.852601156069364, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2262907', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2588, 'n_words': 703}