Sha256: ba49adff5f1a948d88f0b5b262ae9c9ee7cb8636494b076d9e4ac0ac17f2eb62

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

RSpec.shared_examples "an active element" do
  let(:resolver_options) { {} }
  let(:resolver) { HungryForm::Resolver.new(resolver_options) }
  
  let(:group_options) { {} }
  let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, group_options) {} }

  let(:element) { described_class.new(:element_name, group, resolver, active_element_options) {} }

  it_behaves_like "an element" do
    let(:element_options) { active_element_options }
  end

  describe ".new" do
    it "should have empty error" do
      expect(element.error).to eq ""
    end

    it "should not be required if its parent is not visible" do
      group_options[:visible] = false
      active_element_options[:required] = true
      expect(element.required?).to eq false
    end

    it "should have a nil value" do
      expect(element.value).to be nil
    end

    it "should have a value from form params" do
      resolver_options[:params]= {"group_element_name" => "element_value" }
      expect(element.value).to eq "element_value"
    end

    it "should have a value from element structure" do
      active_element_options[:value] = "element_value"
      expect(element.value).to eq "element_value"
    end
  end

  describe "#valid?" do
    describe "when required" do
      before(:each) do
        active_element_options[:required] = true
      end

      it "is valid" do
        element.value = "value"
        expect(element.valid?).to eq true
      end

      it "is invalid" do
        element.value = ""
        expect(element.valid?).to eq false
      end
    end
  end

  describe "#to_hash" do
    it "should include required, value and error" do
      active_element_options[:value] = ''
      active_element_options[:required] = true
      expect(element.to_hash).to include(:required, :value, :error)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hungryform-0.0.4 spec/support/shared_active_element.rb