require 'test_helper' require 'ostruct' class FormForTest < ActiveSupport::TestCase class Context def initialize(resource) @resource = resource end end def resource OpenStruct.new( id: 1, name: 'Foo', body: 'Hello world', email: 'some@email.com', phone: '123123123', url: 'http://someurl.com', number: 123, dropdown: 'yes', gender: 'Male' ) end def setup @example_compiled = -> { ExpressTemplates::Components::FormFor.render_in(self) { "
" } } end def example_compiled_src # necessary because the #source method is not perfect yet # ideally we would have #source_body @example_compiled.source_body end def simple_form(resource) ctx = Context.new(resource) fragment = -> { form_for(:resource, method: :put, url: '/yolo') do |f| f.text_field :name, label: 'post title' f.text_field :body, class: 'string' f.email_field :email, wrapper_class: 'field input' f.phone_field :phone f.url_field :url f.number_field :number f.submit 'Save it!' end } return ctx, fragment end def select_form(resource) ctx = Context.new(resource) fragment = -> { form_for(:resource, method: :put) do |f| f.select :dropdown, ['yes', 'no'], selected: 'yes' f.select :dropdown, '{{ options_from_collection_for_select(@choices, "id", "name") }}' end } return ctx, fragment end def radio_form(resource) ctx = Context.new(resource) fragment = -> { form_for(:resource) do |f| f.radio :age, [[1, 'One'],[2, 'Two']], :first, :last end } return ctx, fragment end def checkbox_form(resource) ctx = Context.new(resource) fragment = -> { form_for(:resource, method: :put) do |f| f.checkbox :age, [[1, 'One'], [2, 'Two']], :first, :last end } return ctx, fragment end test "fields compiled source is legible and transparent" do ExpressTemplates::Markup::Tag.formatted do # ctx, fragment = simple_form(resource) # assert_equal example_compiled_src, ExpressTemplates.compile(&fragment) end end test "select compiled source is legible and transparent" do @example_compiled = -> { ExpressTemplates::Components::FormFor.render_in(self) { " " } } ExpressTemplates::Markup::Tag.formatted do ctx, fragment = select_form(resource) assert_equal example_compiled_src, ExpressTemplates.compile(&fragment) end end test "radio compiled source is legible and transparent" do @example_compiled = -> { ExpressTemplates::Components::FormFor.render_in(self) { " " } } ExpressTemplates::Markup::Tag.formatted do ctx, fragment = radio_form(resource) assert_equal example_compiled_src, ExpressTemplates.compile(&fragment) end end test "checkbox compiled source is legible and transparent" do @example_compiled = -> { ExpressTemplates::Components::FormFor.render_in(self) { " " } } ExpressTemplates::Markup::Tag.formatted do ctx, fragment = checkbox_form(resource) assert_equal example_compiled_src, ExpressTemplates.compile(&fragment) end end end