Sha256: 09989f6a57e488805dc087a244224f0ebea8e77991e253924a824b883a868f9f

Contents?: true

Size: 1.84 KB

Versions: 9

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require 'test_helper'

# Module that represents a custom component.
module Numbers
  def number(wrapper_options = nil)
    @number ||= options[:number].to_s.html_safe
  end
end

# Module that represents a custom component.
module InputGroup
  def prepend(wrapper_options = nil)
    span_tag = content_tag(:span, options[:prepend], class: 'input-group-text')
    template.content_tag(:div, span_tag, class: 'input-group-prepend')
  end

  def append(wrapper_options = nil)
    span_tag = content_tag(:span, options[:append], class: 'input-group-text')
    template.content_tag(:div, span_tag, class: 'input-group-append')
  end
end

class CustomComponentsTest < ActionView::TestCase
  test 'includes the custom components' do
    SimpleForm.include_component Numbers

    custom_wrapper = SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
      b.use :number, wrap_with: { tag: 'div', class: 'number' }
    end

    with_form_for @user, :name, number: 1, wrapper: custom_wrapper

    assert_select 'div.number', text: '1'
  end

  test 'includes custom components and use it as optional in the wrapper' do
    SimpleForm.include_component InputGroup

    custom_wrapper = SimpleForm.build tag: :div, class: 'custom_wrapper' do |b|
      b.use :label
      b.optional :prepend
      b.use :input
      b.use :append
    end

    with_form_for @user, :name, prepend: true, wrapper: custom_wrapper

    assert_select 'div.input-group-prepend > span.input-group-text'
    assert_select 'div.input-group-append > span.input-group-text'
  end

  test 'raises a TypeError when the component is not a Module' do
    component = 'MyComponent'

    exception = assert_raises TypeError do 
      SimpleForm.include_component(component)
    end
    assert_equal exception.message, "SimpleForm.include_component expects a module but got: String"
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
simple_form-5.2.0 test/components/custom_components_test.rb
simple_form-5.1.0 test/components/custom_components_test.rb
simple_form-5.0.3 test/components/custom_components_test.rb
simple_form-5.0.2 test/components/custom_components_test.rb
simple_form-5.0.1 test/components/custom_components_test.rb
simple_form-5.0.0 test/components/custom_components_test.rb
simple_form-4.1.0 test/components/custom_components_test.rb
simple_form-4.0.1 test/components/custom_components_test.rb
simple_form-4.0.0 test/components/custom_components_test.rb