Back to Repositories

Testing Boolean Field Type Rendering in rails_admin

This test suite validates the Boolean field type implementation in RailsAdmin, focusing on field value presentation and HTML rendering. The tests ensure proper handling of true, false, and nil values with appropriate visual indicators.

Test Coverage Overview

The test suite provides comprehensive coverage of the Boolean field type functionality in RailsAdmin:

  • Validates generic field type behavior
  • Tests pretty_value rendering for all possible boolean states
  • Verifies HTML badge and icon generation

Implementation Analysis

The testing approach uses RSpec’s shared examples and context-based testing to validate boolean field rendering:

  • Implements shared behavior testing for field type consistency
  • Uses context blocks for different boolean values
  • Validates HTML output formatting with specific class and icon combinations

Technical Details

Testing infrastructure includes:

  • RSpec as the testing framework
  • FieldTest fixture class for object simulation
  • HTML validation for bootstrap badge classes
  • FontAwesome icon integration testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Consistent use of subject blocks for test focus
  • Comprehensive state coverage
  • Clear test case organization
  • Explicit expected vs actual value comparisons

railsadminteam/rails_admin

spec/rails_admin/config/fields/types/boolean_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RailsAdmin::Config::Fields::Types::Boolean do
  it_behaves_like 'a generic field type', :boolean_field, :boolean

  subject do
    RailsAdmin.config(FieldTest).fields.detect do |f|
      f.name == :boolean_field
    end.with(object: test_object)
  end

  describe '#pretty_value' do
    {
      false => %(<span class="badge bg-danger"><span class="fas fa-times"></span></span>),
      true => %(<span class="badge bg-success"><span class="fas fa-check"></span></span>),
      nil => %(<span class="badge bg-default"><span class="fas fa-minus"></span></span>),
    }.each do |field_value, expected_result|
      context "when field value is '#{field_value.inspect}'" do
        let(:test_object) { FieldTest.new(boolean_field: field_value) }

        it 'returns the appropriate html result' do
          expect(subject.pretty_value).to eq(expected_result)
        end
      end
    end
  end
end