test/reform_test.rb in reform-1.1.1 vs test/reform_test.rb in reform-1.2.0.beta1

- old
+ new

@@ -1,58 +1,7 @@ require 'test_helper' -class RepresenterTest < MiniTest::Spec - class SongRepresenter < Reform::Representer - property :title - property :name - end - - let (:rpr) { SongRepresenter.new(Object.new) } - - describe "#fields" do - it "returns all properties as strings" do - rpr.fields.must_equal(["title", "name"]) - end - end -end - -class WithOptionsTest < MiniTest::Spec - subject { Reform::Representer::WithOptions::Options.new } - - it { subject.must_equal({}) } - it { subject.exclude!([:id, :title]).must_equal(:exclude => [:id, :title]) } - it do - subject.exclude!([:id, :title]) - subject.exclude!([:id, :name]) - subject.must_equal(:exclude => [:id, :title, :id, :name]) - end - it { subject.include!([:id, :title]).must_equal(:include => [:id, :title]) } -end - -class FieldsTest < MiniTest::Spec - describe "#new" do - it "accepts list of properties" do - fields = Reform::Contract::Fields.new([:name, :title]) - fields.name.must_equal nil - fields.title.must_equal nil - end - - it "accepts list of properties and values" do - fields = Reform::Contract::Fields.new(["name", "title"], "title" => "The Body") - fields.name.must_equal nil - fields.title.must_equal "The Body" - end - - it "processes value syms" do - skip "we don't need to test this as representer.to_hash always returns strings" - fields = Reform::Fields.new(["name", "title"], :title => "The Body") - fields.name.must_equal nil - fields.title.must_equal "The Body" - end - end -end - class ReformTest < ReformSpec let (:comp) { OpenStruct.new(:name => "Duran Duran", :title => "Rio") } let (:form) { SongForm.new(comp) } @@ -72,12 +21,12 @@ let (:options) { {:type => String} } subject do opts = options Class.new(Reform::Form) do - properties [:name, :title], opts - properties [:created_at] + properties :name, :title, opts + properties :created_at end.new(comp) end it { subject.name.must_equal "Duran Duran" } it { subject.title.must_equal "Rio" } @@ -244,131 +193,9 @@ form.validate({"title" => "The Body"}) form.title.must_equal "The Body" form.position.must_equal nil form.errors.messages.must_equal({:name=>["can't be blank"], :position=>["can't be blank"]}) end - end - - describe "#column_for_attribute" do - let (:model) { Artist.new } - let (:klass) do - require 'reform/active_record' - - Class.new Reform::Form do - include Reform::Form::ActiveRecord - - model :artist - - property :name - end - end - let (:form) { klass.new(model) } - - it 'should delegate to the model' do - Calls = [] - - def model.column_for_attribute(*args) - Calls << [:column_for_attribute, *args] - end - - form.column_for_attribute(:name) - Calls.must_include [:column_for_attribute, :name] - end - end - - describe "#column_for_attribute with composition" do - let (:artist_model) { Artist.new } - let (:song_model) { Song.new } - let (:form_klass) do - require 'reform/active_record' - - Class.new Reform::Form do - include Reform::Form::ActiveRecord - include Reform::Form::Composition - - model :artist - - property :name, on: :artist - property :title, on: :song - end - end - let (:form) { form_klass.new(artist: artist_model, song: song_model) } - - it 'should delegate to the model' do - ArtistCalls, SongCalls = [], [] - - def artist_model.column_for_attribute(*args) - ArtistCalls << [:column_for_attribute, *args] - end - - def song_model.column_for_attribute(*args) - SongCalls << [:column_for_attribute, *args] - end - - form.column_for_attribute(:name) - ArtistCalls.must_include [:column_for_attribute, :name] - - form.column_for_attribute(:title) - SongCalls.must_include [:column_for_attribute, :title] - end - end -end - - -class EmptyAttributesTest < MiniTest::Spec - Credentials = Struct.new(:password) - - class PasswordForm < Reform::Form - property :password - property :password_confirmation, :empty => true - end - - let (:cred) { Credentials.new } - let (:form) { PasswordForm.new(cred) } - - before { form.validate("password" => "123", "password_confirmation" => "321") } - - it { - form.password.must_equal "123" - form.password_confirmation.must_equal "321" - - form.sync - cred.password.must_equal "123" - - hash = {} - form.save do |nested| - hash = nested - end - - hash.must_equal("password"=> "123", "password_confirmation" => "321") - } -end - -class ReadonlyAttributesTest < MiniTest::Spec - Location = Struct.new(:country) - - class LocationForm < Reform::Form - property :country, :virtual => true # read_only: true - end - - let (:loc) { Location.new("Australia") } - let (:form) { LocationForm.new(loc) } - - it { form.country.must_equal "Australia" } - it do - form.validate("country" => "Germany") # this usually won't change when submitting. - form.country.must_equal "Germany" - - - form.sync - loc.country.must_equal "Australia" # the writer wasn't called. - - hash = {} - form.save do |nested| - hash = nested - end - - hash.must_equal("country"=> "Germany") end end class OverridingAccessorsTest < BaseTest