Контроллеры Stimulus javascript не загружаются в rspec

У меня есть приложение Rails 6.1 с конвейером активов, importmaps и hotwire, и я пытаюсь загрузить свои контроллеры стимулов в тесты функций Rspec.

У меня есть форма, в которой я делаю некоторые расчеты через контроллер стимулов. Они работают в разработке, но не в моих тестах. Я вижу, что он не загружает мой файл javascript контроллера стимулов, но запускает файл application_html.js и выполняет какой-то javascript.

Функция Rspec

      require "rails_helper"

RSpec.describe "create tax assessment", :type => :feature do
  before :each do
    admin_sign_in
  end

  it "creates tax assessment" do
    visit "/menus/tax_system"
    send_keys("1")

    # Display the new form
    expect(page).to have_text("TAX ASSESSMENT")

    fill_in "tax_assessment_tax_payable", with: "20000"
    expect(find_by_id("tax_assessment_total_debits").value).to have_text("20000")
  end
end

Вот мое приложение контроллера стимулов/javascript/controllers/tax_assessment_controller.js

      import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  static targets = ["taxPayable", "totalDebits"]

  connect(){
    console.log('loading tax assessment controller');
  }

  calc_total_debits(){
    console.log('calc total debits');
    let total = this.taxPayableTarget.value;
    this.totalDebitsTarget.value = total;
    console.log(`calc_total_debits: ${total}`);
  }
}

При приостановке моего теста я не вижу ни одной из этих записей консоли, но вижу одну из моего application_html.js.

У меня есть следующая настройка манифеста

приложение/активы/config/manifest.js

      //= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link_tree ../javascripts .js

Мой application_html.js находится в assets_pipeline, в то время как весь мой код стимула находится в app/javascript/controllers.

Вот файл importmap.rb

      # Use direct uploads for Active Storage (remember to import "@rails/activestorage" in your application.js)
# pin "@rails/activestorage", to: "activestorage.esm.js"

# Use node modules from a JavaScript CDN by running ./bin/importmap

pin "application", preload: true
pin "@hotwired/stimulus", to: "stimulus.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "@hotwired/turbo-rails", to: "turbo.js"

0 ответов

Другие вопросы по тегам