Back to Repositories

Testing File Upload Field Implementation in rails_admin

This test suite validates file upload field functionality in the Rails Admin interface, focusing on file handling, deletion capabilities, and preview features. The tests ensure proper integration between the file upload component and the admin panel.

Test Coverage Overview

The test suite covers essential file upload field operations in Rails Admin:
  • File deletion functionality with boolean field integration
  • Preview generation for uploaded images
  • Resource URL handling and field configuration
  • User interaction validation through JavaScript-enabled tests

Implementation Analysis

The testing approach utilizes RSpec’s request specs with JavaScript support for comprehensive UI testing. It implements factory-based test data generation and custom field configuration, demonstrating Rails Admin’s extensible field system.

The tests employ page object patterns and explicit expectations for reliable UI interaction verification.

Technical Details

Key technical components include:
  • RSpec request specs with JS capability
  • FactoryBot for test data generation
  • Custom field configuration using RailsAdmin DSL
  • Capybara for DOM interaction
  • File attachment handling

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test scenarios, clear setup procedures, and explicit expectations. It demonstrates proper use of subject definitions, before blocks for configuration, and factory-based test data management.

The implementation shows careful attention to UI component testing with proper selector usage and visibility checks.

railsadminteam/rails_admin

spec/integration/fields/file_upload_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

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

  before do
    RailsAdmin.config FieldTest do
      field :string_field, :file_upload do
        delete_method 'boolean_field'
        def resource_url(_thumb = false)
          value
        end
      end
    end
  end
  let(:field_test) { FactoryBot.create :field_test, string_field: 'http://localhost/dummy.jpg' }

  it 'supports deletion', js: true do
    visit edit_path(model_name: 'field_test', id: field_test.id)
    expect(find('#field_test_boolean_field', visible: false)).not_to be_checked
    click_link "Delete 'String field'"
    expect(find('#field_test_boolean_field', visible: false)).to be_checked
  end

  it 'shows a inline preview', js: true do
    visit new_path(model_name: 'field_test')
    attach_file 'String field', file_path('test.jpg')
    is_expected.to have_selector('#field_test_string_field_field img.preview')
  end
end