Back to Repositories

Testing ActionText Field Integration in rails_admin

This integration test suite validates the ActionText field functionality in Rails Admin, focusing on asset loading behavior and warning messages. The tests ensure proper rendering and configuration of ActionText fields while handling different Rails versions and asset loading scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of ActionText field integration in Rails Admin:
  • Basic functionality verification of ActionText fields
  • Asset loading behavior testing
  • Warning message handling for dynamic asset loading
  • Version-specific behavior for Rails pre-7.0

Implementation Analysis

The testing approach utilizes RSpec’s request specs with JavaScript support to validate ActionText integration:
  • Uses page object pattern for UI interaction
  • Implements conditional testing based on Rails version
  • Leverages RSpec’s expectation syntax for assertions
  • Employs mocking for console logger interactions

Technical Details

Key technical components include:
  • RSpec request specs with JS capability
  • Capybara for page interaction
  • ConsoleLogger mock objects
  • Rails Admin configuration blocks
  • Version-specific conditional testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test scenarios with proper setup
  • Clear separation of concerns in test cases
  • Proper error handling verification
  • Configuration-based test variations
  • Version-aware conditional testing

railsadminteam/rails_admin

spec/integration/fields/action_text_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

if defined?(ActionText)
  RSpec.describe 'ActionText field', type: :request, js: true do
    subject { page }

    before do
      RailsAdmin.config FieldTest do
        edit do
          field :action_text_field
        end
      end
    end

    it 'works without error' do
      allow(ConsoleLogger).to receive(:warn).with(/ActionText assets should be loaded statically/)
      expect { visit new_path(model_name: 'field_test') }.not_to raise_error
      is_expected.to have_selector('trix-toolbar')
    end

    if RailsAdmin.config.asset_source == :sprockets && Rails.gem_version < Gem::Version.new('7.0')
      it 'shows a warning if ActionText assets are loaded dynamically' do
        expect(ConsoleLogger).to receive(:warn).with(/ActionText assets should be loaded statically/)
        visit new_path(model_name: 'field_test')
        is_expected.to have_selector('trix-toolbar')
      end

      it 'allows suppressing the warning' do
        RailsAdmin.config FieldTest do
          edit do
            field :action_text_field do
              warn_dynamic_load false
            end
          end
        end
        expect(ConsoleLogger).not_to receive(:warn).with(/ActionText assets should be loaded statically/)
        visit new_path(model_name: 'field_test')
        is_expected.to have_selector('trix-toolbar')
      end
    end
  end
end