test/virtual_test.rb in reform-2.3.0.rc1 vs test/virtual_test.rb in reform-2.3.0.rc2

- old
+ new

@@ -1,24 +1,64 @@ -require 'test_helper' +require "test_helper" class VirtualTest < MiniTest::Spec class CreditCardForm < TestForm property :credit_card_number, virtual: true # no read, no write, it's virtual. + collection :transactions, virtual: true, populate_if_empty: OpenStruct do + property :id + end end - let (:form) { CreditCardForm.new(Object.new) } + let(:form) { CreditCardForm.new(Object.new) } it { - form.validate("credit_card_number" => "123") + form.validate(credit_card_number: "123", transactions: [id: 1]) - form.credit_card_number.must_equal "123" # this is still readable in the UI. + form.credit_card_number.must_equal "123" # this is still readable in the UI. + form.transactions.first.id.must_equal 1 # this is still readable in the UI. form.sync hash = {} form.save do |nested| hash = nested end - hash.must_equal("credit_card_number"=> "123") + hash.must_equal("credit_card_number" => "123", "transactions" => ["id" => 1]) } -end \ No newline at end of file +end + +class VirtualAndDefaultTest < MiniTest::Spec + class CreditCardForm < TestForm + property :credit_card_number, virtual: true, default: "123" # no read, no write, it's virtual. + collection :transactions, virtual: true, populate_if_empty: OpenStruct, default: [OpenStruct.new(id: 2)] do + property :id + end + end + + def hash(form) + hash = {} + form.save do |nested| + hash = nested + end + hash + end + + let(:form) { CreditCardForm.new(Object.new) } + + it { + form = CreditCardForm.new(Object.new) + form.validate({}) + + hash(form).must_equal("credit_card_number" => "123", "transactions" => ["id" => 2]) + + form = CreditCardForm.new(Object.new) + form.validate(credit_card_number: "123", transactions: [id: 1]) + + form.credit_card_number.must_equal "123" # this is still readable in the UI. + form.transactions.first.id.must_equal 1 # this is still readable in the UI. + + form.sync + + hash(form).must_equal("credit_card_number" => "123", "transactions" => ["id" => 1]) + } +end