Back to Repositories

Testing Weekday Select Input Generation in Simple Form

This test suite validates the WeekdayInput functionality in Simple Form, focusing on weekday select field generation and placeholder handling. The tests ensure proper HTML select element creation with appropriate attributes and classes.

Test Coverage Overview

The test suite covers essential weekday input functionality in Simple Form, specifically for ActionView 7+ compatibility.

  • Tests basic weekday select generation with proper CSS classes and IDs
  • Verifies placeholder attribute handling in weekday selects
  • Ensures proper DOM element creation and attribute assignment

Implementation Analysis

The testing approach utilizes ActionView::TestCase for view component testing, focusing on DOM element verification.

Key patterns include:
  • Version-specific testing using ActionView::VERSION::MAJOR checks
  • Helper method ‘with_input_for’ for form input generation
  • assert_select for DOM element validation

Technical Details

Testing infrastructure includes:
  • Minitest framework integration
  • ActionView test case inheritance
  • assert_select for HTML element assertions
  • Simple Form helper methods
  • Ruby frozen_string_literal pragma

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for distinct functionality
  • Version compatibility checks
  • Clear test naming conventions
  • Focused assertion scope
  • Proper setup and organization of test cases

heartcombo/simple_form

test/inputs/weekday_input_test.rb

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

class WeekdayInputTest < ActionView::TestCase
  test 'input generates a weekday select' do
    if ActionView::VERSION::MAJOR >= 7
      with_input_for @user, :born_at, :weekday
      assert_select 'select.weekday#user_born_at'
    end
  end

  test 'input generates a weekday select that accepts placeholder' do
    if ActionView::VERSION::MAJOR >= 7
      with_input_for @user, :born_at, :weekday, placeholder: 'Put in a weekday'
      assert_select 'select.weekday[placeholder="Put in a weekday"]'
    end
  end
end