Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test ActionText using RSpec?

I am trying to write an RSpec system test that involves filling out an ActionText / Trix field on a page.

It seems as though ActionText::SystemTestHelper as defined here is available to help with this task but so far I have not had any success in implementing it.

My RSpec test looks like the following:

# spec/system/add_job_posting_spec.rb

require 'rails_helper'

RSpec.describe 'adding a job posting', type: :system do
  let(:user) { FactoryBot.create :user }

  before do
    login_as(user)
    visit new_job_posting_path
  end

  context 'with valid information' do
    let(:valid_job_posting) { FactoryBot.create :job_posting }

    scenario 'creates a job posting', js: true do
      fill_in 'Title', with: valid_job_posting.title
      fill_in_rich_text_area 'Description', with: 'Job Info'

      click_button 'Submit'
      expect(page).to have_content 'Job was successfully created.'
    end
  end
end

I have also tried to create a RSpec support file as such

# spec/support/action_text.rb

RSpec.configure do |config|
  config.include ActionText::SystemTestHelper, type: :system
end

When I run this using

bundle exec rspec spec/system/add_job_posting_spec.rb

I get the following error uninitialized constant ActionText::SystemTestHelper

When I try to remove the file located at spec/support/action_text.rb I get this error instead:

undefined method `fill_in_rich_text_area'

I have also tried to just use the regular fill_in RSpec method to avoid that entirely but then get:

Unable to find field "Description" that is not disabled

Despite the fact that the test is marked as js: true and I have another RSpec support file to handle that which is setup as follows:

# spec/support/system/driver.rb

RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :rack_test
  end

  config.before(:each, type: :system, js: true) do
    ActiveRecord::Base.establish_connection
    driven_by :selenium_chrome_headless
  end
end
like image 465
mdh Avatar asked Aug 02 '19 03:08

mdh


2 Answers

In spec/rails_helper.rb:

Require the helper file and include the module in the system specs:

require "action_text/system_test_helper"

RSpec.configure do |config|
  config.include ActionText::SystemTestHelper, type: :system
end

You can now use the fill_in_rich_text_area helper in your system specs:

fill_in_rich_text_area "page_content", with: "Some content."

…where "page_content" is the id of the trix-editor.

The rich text area can also be found by the value of placeholder attribute, value of the aria-label attribute, the text from its label or by the name of its input.

The first argument can be omitted if you only have one editor on the page, like this:

fill_in_rich_text_area with: "Some content."
like image 70
Fred Bergman Avatar answered Sep 20 '22 15:09

Fred Bergman


Here’s the script I’m using (just include it as a helper):

def fill_in_trix_editor(id, with:)
  find(:css, "##{id}").click.set(with)
end

ActionText creates an underscored id of the model + attribute. You can also inspect to see.

like image 40
Dan Brown Avatar answered Sep 16 '22 15:09

Dan Brown