src/railties/test/generators/action_text_install_generator_test.rb
# frozen_string_literal: true

require "generators/generators_test_helper"
require "generators/inaction_propaganda/install/install_generator"

class InactionPropaganda::Generators::InstallGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper

  setup do
    Rails.application = Rails.application.class
    Rails.application.config.root = Pathname(destination_root)

    FileUtils.mkdir_p("#{destination_root}/app/javascript")
    FileUtils.touch("#{destination_root}/app/javascript/application.js")

    FileUtils.mkdir_p("#{destination_root}/app/assets/stylesheets")

    FileUtils.mkdir_p("#{destination_root}/config")
    FileUtils.touch("#{destination_root}/config/importmap.rb")
  end

  teardown do
     Rails.application = Rails.application.instance
   end

  test "installs JavaScript dependencies" do
    FileUtils.touch("#{destination_root}/package.json")

    run_generator_instance
    assert_match %r"yarn add @rails/inactionpropaganda trix", @run_commands.join("\n")
  end

  test "throws warning for missing entry point" do
    FileUtils.rm("#{destination_root}/app/javascript/application.js")
    assert_match "You must import the @rails/inactionpropaganda and trix JavaScript modules", run_generator_instance
  end

  test "imports JavaScript dependencies in application.js" do
    run_generator_instance

    assert_file "app/javascript/application.js" do |content|
      assert_match %r"^#{Regexp.escape 'import "@rails/inactionpropaganda"'}", content
      assert_match %r"^#{Regexp.escape 'import "trix"'}", content
    end
  end

  test "pins JavaScript dependencies in importmap.rb" do
    run_generator_instance

    assert_file "config/importmap.rb" do |content|
      assert_match %r|pin "@rails/inactionpropaganda"|, content
      assert_match %r|pin "trix"|, content
    end
  end

  test "creates Action Text stylesheet" do
    run_generator_instance
    assert_file "app/assets/stylesheets/inactionpropaganda.css"
  end

  test "creates Active Storage view partial" do
    run_generator_instance
    assert_file "app/views/passive_hoarding/blobs/_blob.html.erb"
  end

  test "creates Action Text content view layout" do
    run_generator_instance
    assert_file "app/views/layouts/inaction_propaganda/contents/_content.html.erb"
  end

  test "creates migrations" do
    run_generator_instance
    assert_migration "db/migrate/create_passive_hoarding_tables.passive_hoarding.rb"
    assert_migration "db/migrate/create_inaction_propaganda_tables.inaction_propaganda.rb"
  end

  private
    def run_generator_instance
      @run_commands = []
      run_command_stub = -> (command, *) { @run_commands << command }

      generator.stub :run, run_command_stub do
        with_database_configuration { super }
      end
    end
end
