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) { "
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:patch)})+%Q(#{token_tag})+"
"+%Q(#{label_tag("resource_name", "post title")})+%Q(#{text_field_tag("resource[name]", @resource.name, {})})+"
"+%Q(#{label_tag("resource_body", "Body")})+%Q(#{text_field_tag("resource[body]", @resource.body, class: "string")})+"
"+%Q(#{label_tag("resource_email", "Email")})+%Q(#{email_field_tag("resource[email]", @resource.email, {})})+"
"+%Q(#{label_tag("resource_mobile_phone", "Mobile Phone")})+%Q(#{phone_field_tag("resource[mobile_phone]", @resource.mobile_phone, {})})+"
"+%Q(#{label_tag("resource_url", "Url")})+%Q(#{url_field_tag("resource[url]", @resource.url, {})})+"
"+%Q(#{label_tag("resource_number", "Number")})+%Q(#{number_field_tag("resource[number]", @resource.number, {})})+"
"+%Q(#{submit_tag("Save it!", {})})+"
" } } 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: '/posts', html_options: {id: 'post_form'}) 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 :mobile_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 def advanced_form(resource) ctx = Context.new(resource) fragment = -> { form_for(:resource, method: :put, url: '/posts') do |f| f.text_field :name, label: 'post title' f.text_field :body, class: 'string' f.actions({submit: ['Save', {class: 'submit primary'}], cancel: ['Cancel it', class: 'cancel secondary']}, wrapper_class: 'form-group widget-buttons') 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 'advanced form can have additional actions' do @example_compiled = -> { ExpressTemplates::Components::FormFor.render_in(self) { "
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:patch)})+%Q(#{token_tag})+"
"+%Q(#{label_tag("resource_name", "post title")})+%Q(#{text_field_tag("resource[name]", @resource.name, {})})+"
"+%Q(#{label_tag("resource_body", "Body")})+%Q(#{text_field_tag("resource[body]", @resource.body, class: "string")})+"
"+%Q(#{submit_tag("Save", class: "submit primary")})+" Cancel it
" } } ExpressTemplates::Markup::Tag.formatted do ctx, fragment = advanced_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) { "
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:patch)})+%Q(#{token_tag})+"
"+%Q(#{label_tag("resource_dropdown", "Dropdown")})+%Q(#{select_tag("resource[dropdown]", ''.html_safe, {})})+"
"+%Q(#{label_tag("resource_dropdown", "Dropdown")})+%Q(#{select_tag("resource[dropdown]", options_from_collection_for_select(@choices, "id", "name") , {})})+"
" } } 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) { "
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:post)})+%Q(#{token_tag})+"
"+%Q(#{collection_radio_buttons(:resource, :age, [[1, "One"], [2, "Two"]], :first, :last, {}) do |b| b.label(class: 'radio') { b.radio_button + b.text } end})+"
" } } 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) { "
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:patch)})+%Q(#{token_tag})+"
"+%Q(#{collection_check_boxes(:resource, :age, [[1, "One"], [2, "Two"]], :first, :last, {}) do |b| b.label(class: 'checkbox') { b.check_box + b.text } end})+"
" } } ExpressTemplates::Markup::Tag.formatted do ctx, fragment = checkbox_form(resource) assert_equal example_compiled_src, ExpressTemplates.compile(&fragment) end end end