Back to Repositories

Testing Form Input Disabled States in Simple Form

This test suite validates the disabled functionality for various input types in Simple Form. It ensures proper handling of the disabled attribute across different form elements including string, text, numeric, date, and datetime inputs.

Test Coverage Overview

The test suite provides comprehensive coverage for disabled state handling across form inputs:

  • Tests disabled behavior for string, text, numeric, date, and datetime inputs
  • Verifies disabled attribute presence when explicitly set to true
  • Confirms absence of disabled attribute when set to false or not specified
  • Tests disabled functionality in collection inputs and select fields

Implementation Analysis

The testing approach utilizes ActionView::TestCase with helper methods for input generation. The suite employs a systematic pattern of testing each input type under three conditions: disabled=true, disabled=false, and disabled not specified. The implementation leverages Simple Form’s with_input_for and with_input_field_for helpers for consistent test setup.

Technical Details

Testing tools and configuration:

  • Minitest framework with ActionView::TestCase
  • Simple Form’s custom test helpers
  • CSS selector assertions for DOM validation
  • User model fixtures for form input testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Consistent test naming and organization
  • Thorough coverage of edge cases
  • Clear separation of concerns for different input types
  • Explicit test cases for both positive and negative scenarios
  • Effective use of helper methods to reduce code duplication

heartcombo/simple_form

test/inputs/disabled_test.rb

            
# frozen_string_literal: true
require 'test_helper'

class DisabledTest < ActionView::TestCase
  test 'string input is disabled when disabled option is true' do
    with_input_for @user, :name, :string, disabled: true
    assert_select 'input.string.disabled[disabled]'
  end

  test 'text input is disabled when disabled option is true' do
    with_input_for @user, :description, :text, disabled: true
    assert_select 'textarea.text.disabled[disabled]'
  end

  test 'numeric input is disabled when disabled option is true' do
    with_input_for @user, :age, :integer, disabled: true
    assert_select 'input.integer.disabled[disabled]'
  end

  test 'date input is disabled when disabled option is true' do
    with_input_for @user, :born_at, :date, disabled: true
    assert_select 'select.date.disabled[disabled]'
  end

  test 'datetime input is disabled when disabled option is true' do
    with_input_for @user, :created_at, :datetime, disabled: true
    assert_select 'select.datetime.disabled[disabled]'
  end

  test 'string input does not be disabled when disabled option is false' do
    with_input_for @user, :name, :string, disabled: false
    assert_no_select 'input.string.disabled[disabled]'
  end

  test 'text input does not be disabled when disabled option is false' do
    with_input_for @user, :description, :text, disabled: false
    assert_no_select 'textarea.text.disabled[disabled]'
  end

  test 'numeric input does not be disabled when disabled option is false' do
    with_input_for @user, :age, :integer, disabled: false
    assert_no_select 'input.integer.disabled[disabled]'
  end

  test 'date input does not be disabled when disabled option is false' do
    with_input_for @user, :born_at, :date, disabled: false
    assert_no_select 'select.date.disabled[disabled]'
  end

  test 'datetime input does not be disabled when disabled option is false' do
    with_input_for @user, :created_at, :datetime, disabled: false
    assert_no_select 'select.datetime.disabled[disabled]'
  end

  test 'string input does not be disabled when disabled option is not present' do
    with_input_for @user, :name, :string
    assert_no_select 'input.string.disabled[disabled]'
  end

  test 'text input does not be disabled when disabled option is not present' do
    with_input_for @user, :description, :text
    assert_no_select 'textarea.text.disabled[disabled]'
  end

  test 'numeric input does not be disabled when disabled option is not present' do
    with_input_for @user, :age, :integer
    assert_no_select 'input.integer.disabled[disabled]'
  end

  test 'date input does not be disabled when disabled option is not present' do
    with_input_for @user, :born_at, :date
    assert_no_select 'select.date.disabled[disabled]'
  end

  test 'datetime input does not be disabled when disabled option is not present' do
    with_input_for @user, :created_at, :datetime
    assert_no_select 'select.datetime.disabled[disabled]'
  end

  test 'input_field collection allows disabled select' do
    with_input_field_for @user, :description, collection: ['foo', 'bar'], disabled: true
    assert_select 'select[disabled]'
    assert_no_select 'option[disabled]'
  end

  test 'input_field collection allows individual disabled options' do
    with_input_field_for @user, :description, collection: ['foo', 'bar'], disabled: 'bar'
    assert_no_select 'select[disabled]'
    assert_no_select 'option[disabled][value=foo]'
    assert_select 'option[disabled][value=bar]'
  end
end