Back to Repositories

Testing Hidden Field Implementation in Rails Admin

This test suite verifies the functionality of hidden fields in the Rails Admin interface, focusing on default value handling and field behavior. It ensures proper authentication, field rendering, and form submission workflows.

Test Coverage Overview

The test suite provides comprehensive coverage of hidden field functionality in Rails Admin:

  • Default value initialization and rendering
  • Field visibility and DOM presence verification
  • Form submission handling with hidden field values
  • Integration with user authentication

Implementation Analysis

The testing approach utilizes RSpec’s request specs to validate hidden field behavior:

Tests leverage page object interactions and DOM assertions to verify field attributes and values. The implementation demonstrates proper integration between Rails Admin configuration and user authentication contexts.

Technical Details

Key technical components include:

  • RSpec request specs with page object pattern
  • Warden authentication integration
  • Rails Admin configuration blocks
  • Capybara DOM interaction methods
  • Custom field configuration with dynamic default values

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test setup with proper authentication context
  • Comprehensive validation of both positive and negative scenarios
  • Clear separation of concerns between configuration and testing
  • Effective use of RSpec expectations and matchers

railsadminteam/rails_admin

spec/integration/fields/hidden_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Hidden field', type: :request do
  subject { page }

  describe '#default_value' do
    before do
      RailsAdmin::Config.authenticate_with { warden.authenticate! scope: :user }
      RailsAdmin::Config.current_user_method(&:current_user)
      login_as User.create(
        email: '[email protected]',
        password: 'password',
      )
    end

    before do
      RailsAdmin.config Player do
        include_all_fields
        edit do
          field :name, :hidden do
            default_value do
              bindings[:view]._current_user.email
            end
          end
        end
      end
    end

    it 'shows up with default value, hidden' do
      visit new_path(model_name: 'player')
      is_expected.to have_selector("#player_name[type=hidden][value='[email protected]']", visible: false)
      is_expected.not_to have_selector("#player_name[type=hidden][value='[email protected]']", visible: false)
    end

    it 'does not show label' do
      is_expected.not_to have_selector('label', text: 'Name')
    end

    it 'does not show help block' do
      is_expected.not_to have_xpath("id('player_name')/../p[@class='help-block']")
    end

    it 'submits the field value' do
      visit new_path(model_name: 'player')
      find("#player_name[type=hidden][value='[email protected]']", visible: false).set('[email protected]')
      fill_in 'Number', with: 1
      click_button 'Save'
      is_expected.to have_content('Player successfully created')
      expect(Player.first.name).to eq '[email protected]'
    end
  end
end