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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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