Back to Repositories

Testing Button Component Implementation in SimpleForm

This test suite validates the button creation functionality in SimpleForm’s form builder implementation. It ensures proper button generation with various configurations and options, while maintaining compatibility with Rails’ native button helpers.

Test Coverage Overview

The test suite comprehensively covers button creation functionality in SimpleForm:

  • Basic button creation with default settings
  • Custom button options handling
  • Options hash immutability verification
  • Record-specific button text generation
  • Configuration-based class assignment
  • Rails button helper integration

Implementation Analysis

The testing approach utilizes ActionView::TestCase for form builder validation. It employs helper methods for form generation and implements swap functionality for configuration testing. The suite demonstrates isolated component testing with precise DOM structure verification.

Technical Details

Key technical components include:

  • Minitest framework integration
  • ActionView::TestCase as the base test class
  • assert_select for DOM verification
  • Custom helper method with_button_for
  • SimpleForm configuration manipulation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear purpose
  • Proper setup and teardown through helper methods
  • State verification through DOM assertions
  • Configuration testing through context switching
  • Edge case handling for new records

heartcombo/simple_form

test/form_builder/button_test.rb

            
# frozen_string_literal: true
# encoding: UTF-8
require 'test_helper'

class ButtonTest < ActionView::TestCase
  def with_button_for(object, *args)
    with_concat_form_for(object) do |f|
      f.button(*args)
    end
  end

  test 'builder creates buttons' do
    with_button_for :post, :submit
    assert_select 'form input.button[type=submit][value="Save Post"]'
  end

  test 'builder creates buttons with options' do
    with_button_for :post, :submit, class: 'my_button'
    assert_select 'form input.button.my_button[type=submit][value="Save Post"]'
  end

  test 'builder does not modify the options hash' do
    options = { class: 'my_button' }
    with_button_for :post, :submit, options
    assert_select 'form input.button.my_button[type=submit][value="Save Post"]'
    assert_equal({ class: 'my_button' }, options)
  end

  test 'builder creates buttons for records' do
    @user.new_record!
    with_button_for @user, :submit
    assert_select 'form input.button[type=submit][value="Create User"]'
  end

  test "builder uses the default class from the configuration" do
    swap SimpleForm, button_class: 'btn' do
      with_button_for :post, :submit
      assert_select 'form input.btn[type=submit][value="Save Post"]'
    end
  end

  if ActionView::Helpers::FormBuilder.method_defined?(:button)
    test "allows to use Rails button helper when available" do
      with_button_for :post, :button, 'Save!'
      assert_select 'form button.button[type=submit]', 'Save!'
    end
  end
end