Back to Repositories

Testing Country Select Input Generation in SimpleForm

This test suite validates the country select input functionality in SimpleForm, focusing on proper HTML generation and attribute handling for country selection fields. The tests ensure correct rendering of country options, priority handling, and required field validation.

Test Coverage Overview

The test suite provides comprehensive coverage of the CountryInput component functionality in SimpleForm.

Key areas tested include:
  • Basic country select field generation
  • Default country priority handling
  • Custom priority country options
  • Required field attribute validation
Edge cases cover disabled separator options and proper ordering of priority countries.

Implementation Analysis

The testing approach uses ActionView::TestCase as the base framework, implementing isolated unit tests for each country select feature. The implementation leverages SimpleForm’s configuration swapping capability to test different priority settings.

Key patterns include:
  • DOM assertion testing using assert_select
  • Configuration context switching
  • Isolated test cases for each feature

Technical Details

Testing tools and setup:
  • Minitest framework
  • ActionView::TestCase for view component testing
  • SimpleForm configuration helpers
  • CSS selector-based assertions
  • Test helper methods for input generation

Best Practices Demonstrated

The test suite exemplifies several testing best practices including atomic test cases, clear test naming, and comprehensive feature coverage.

Notable practices:
  • Isolated test scenarios
  • Descriptive test names
  • Thorough attribute validation
  • Configuration state management
  • HTML structure verification

heartcombo/simple_form

test/inputs/country_input_test.rb

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

class CountryInputTest < ActionView::TestCase
  test 'input generates a country select field' do
    with_input_for @user, :country, :country
    assert_select 'select#user_country'
    assert_select 'select option[value=BR]', 'Brazil'
    assert_no_select 'select option[value=""][disabled=disabled]'
  end

  test 'input generates a country select with SimpleForm default' do
    swap SimpleForm, country_priority: [ 'Brazil' ] do
      with_input_for @user, :country, :country
      assert_select 'select option[value="BR"] + option[value="---------------"][disabled=disabled]'
    end
  end

  test 'input generates a country select using options priority' do
    with_input_for @user, :country, :country, priority: [ 'Ukraine' ]
    assert_select 'select option[value="UA"] + option[value="---------------"][disabled=disabled]'
  end

  test 'input does generate select element with required html attribute' do
    with_input_for @user, :country, :country
    assert_select 'select.required'
    assert_select 'select[required]'
  end
end