How to test ActionText using RSpec?
Asked Answered
P

3

11

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
Preordain answered 2/8, 2019 at 3:54 Comment(0)
M
3

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.

Monogamist answered 25/5, 2020 at 4:49 Comment(0)
L
8

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."
Lhary answered 12/12, 2020 at 20:27 Comment(0)
M
3

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.

Monogamist answered 25/5, 2020 at 4:49 Comment(0)
S
0

I'm not 100% sure, but it seems like the ActionText::SystemTestHelper probably hasn't been yet released into a "stable" version of Rails. It will probably come with the next one (or maybe you're running an older version that doesn't contain it yet).

There are a few things you can try:

  • update Rails version and see if that helps
  • use current master Rails version from github (not recommended)
  • add the module yourself as a helper module for specs and then include it yourself where needed
Somnambulate answered 11/10, 2019 at 13:39 Comment(1)
This helper is now included in Rails stableHereunto

© 2022 - 2024 — McMap. All rights reserved.