require "spec_helper" describe Form::Component::Base do subject { described_class.new(nil, nil) } describe "#composed_name" do let(:form) { mock } let(:name) { "name" } subject { described_class.new(form, name) } context "without base name" do before { form.stub base_name: nil } specify { subject.composed_name.should eql("name") } end context "for single base name" do before { form.stub base_name: "user" } specify { subject.composed_name.should eql("user[name]") } end context "for multiple base name" do before { form.stub base_name: ["user", "profile"] } specify { subject.composed_name.should eql("user[profile][name]") } end end describe "#data" do let(:form) { mock.as_null_object } subject { described_class.new(form, "name") } it "delegates to form's data" do form.should_receive(:data).once subject.data end end describe "#value" do let(:form) { mock } let(:name) { "name" } subject { described_class.new(form, name) } context "with objects that respond to []" do it "returns value for string key" do form.stub data: {"name" => "John Doe"} subject.value.should eql("John Doe") end it "returns value for symbol key" do form.stub data: {:name => "John Doe"} subject.value.should eql("John Doe") end end context "with objects that respond to the attribute" do it "returns value" do form.stub data: mock(name: "John Doe") subject.value.should eql("John Doe") end end context "with no data objects" do it "returns nil" do form.stub :data subject.value.should be_nil end end end describe "#t" do it "proxies arguments to I18n.t" do I18n.should_receive(:t).with(:a, :b) subject.t(:a, :b) end end describe "#humanize" do specify { subject.humanize("name").should eql("Name") } specify { subject.humanize("full_name").should eql("Full name") } end end