From 37a651af6d84d9a983affc35adca510bda7003fe Mon Sep 17 00:00:00 2001
From: Niklas Haeusele <niklas.haeusele@hey.com>
Date: Tue, 23 Apr 2024 00:03:52 +0200
Subject: [PATCH] 7.0, 7.1 github actions

---
 .github/workflows/ruby.yml                    | 37 ++++++++++++++
 .gitignore                                    |  1 +
 Appraisals                                    | 19 +++++++
 Gemfile                                       |  7 ++-
 Gemfile.lock                                  | 50 ++++++++++++++++++-
 Rakefile                                      |  5 +-
 bin/rails                                     | 14 ++++++
 custom_elements-rails.gemspec                 |  4 +-
 ...s_7.0__importmap_rails_version_1.2.gemfile | 13 +++++
 ...s_7.0__importmap_rails_version_2.0.gemfile | 13 +++++
 ...s_7.1__importmap_rails_version_1.2.gemfile | 13 +++++
 ...s_7.1__importmap_rails_version_2.0.gemfile | 13 +++++
 lib/custom_elements/rails/railtie.rb          |  4 +-
 test/custom_elements/integration_test.rb      |  9 ++++
 .../app/controllers/elements_controller.rb    |  4 ++
 test/dummy/app/helpers/elements_helper.rb     |  2 +
 test/dummy/app/javascript/application.js      |  2 +
 .../custom_elements/hello_element.js          |  9 ++++
 .../app/javascript/custom_elements/index.js   |  3 ++
 test/dummy/app/views/elements/show.html.erb   |  3 ++
 .../app/views/layouts/application.html.erb    |  1 +
 test/dummy/bin/importmap                      |  4 ++
 test/dummy/config/application.rb              |  2 +-
 test/dummy/config/importmap.rb                |  3 ++
 test/dummy/config/routes.rb                   |  9 +---
 test/dummy/vendor/javascript/.keep            |  0
 test/system_helper.rb                         |  7 +++
 27 files changed, 236 insertions(+), 15 deletions(-)
 create mode 100644 .github/workflows/ruby.yml
 create mode 100644 Appraisals
 create mode 100755 bin/rails
 create mode 100644 gemfiles/rails_7.0__importmap_rails_version_1.2.gemfile
 create mode 100644 gemfiles/rails_7.0__importmap_rails_version_2.0.gemfile
 create mode 100644 gemfiles/rails_7.1__importmap_rails_version_1.2.gemfile
 create mode 100644 gemfiles/rails_7.1__importmap_rails_version_2.0.gemfile
 create mode 100644 test/custom_elements/integration_test.rb
 create mode 100644 test/dummy/app/controllers/elements_controller.rb
 create mode 100644 test/dummy/app/helpers/elements_helper.rb
 create mode 100644 test/dummy/app/javascript/application.js
 create mode 100644 test/dummy/app/javascript/custom_elements/hello_element.js
 create mode 100644 test/dummy/app/javascript/custom_elements/index.js
 create mode 100644 test/dummy/app/views/elements/show.html.erb
 create mode 100755 test/dummy/bin/importmap
 create mode 100644 test/dummy/config/importmap.rb
 create mode 100644 test/dummy/vendor/javascript/.keep
 create mode 100644 test/system_helper.rb

diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml
new file mode 100644
index 0000000..72e2b40
--- /dev/null
+++ b/.github/workflows/ruby.yml
@@ -0,0 +1,37 @@
+name: Tests
+
+on: 
+  push:
+    branches:
+      - main
+  pull_request:
+    branches:
+      - main
+
+permissions:
+  contents: read
+
+jobs:
+  test:
+
+    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        ruby-version: ['3.1', '3.2', '3.3']
+        rails-version: ['7.0', '7.1']
+        importmap-rails-version: ['2.0', '1.2']
+      fail-fast: false
+
+    env:
+      BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails-version }}__importmap_rails_version_${{ matrix.importmap-rails-version }}.gemfile
+
+    steps:
+    - uses: actions/checkout@v3
+    - name: Set up Ruby
+      uses: ruby/setup-ruby@v1
+      with:
+        ruby-version: ${{ matrix.ruby-version }}
+    - name: Bundler
+      run: bundle install
+    - name: Run tests
+      run: bin/test
diff --git a/.gitignore b/.gitignore
index a3ee5aa..1729454 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,4 @@
 /test/dummy/log/*.log
 /test/dummy/storage/
 /test/dummy/tmp/
+/gemfiles/*.lock
diff --git a/Appraisals b/Appraisals
new file mode 100644
index 0000000..76dd3f4
--- /dev/null
+++ b/Appraisals
@@ -0,0 +1,19 @@
+appraise "rails_7.0__importmap_rails_version_1.2" do
+  gem "rails", "~> 7.0"
+  gem "importmap-rails", "~> 1.2"
+end
+
+appraise "rails_7.0__importmap_rails_version_2.0" do
+  gem "rails", "~> 7.0"
+  gem "importmap-rails", "~> 2.0"
+end
+
+appraise "rails_7.1__importmap_rails_version_1.2" do
+  gem "rails", "~> 7.1"
+  gem "importmap-rails", "~> 1.2"
+end
+
+appraise "rails_7.1__importmap_rails_version_2.0" do
+  gem "rails", "~> 7.1"
+  gem "importmap-rails", "~> 2.0"
+end
diff --git a/Gemfile b/Gemfile
index 7892cf5..9140f8f 100644
--- a/Gemfile
+++ b/Gemfile
@@ -4,9 +4,14 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
 # Specify your gem's dependencies in custom_elements-rails.gemspec.
 gemspec
 
+gem "importmap-rails"
+gem "propshaft"
+
 gem "puma"
 
-gem "sqlite3"
+gem "sqlite3", "~> 1.4"
 
 # Start debugger with binding.b [https://github.com/ruby/debug]
 # gem "debug", ">= 1.0.0"
+gem "capybara"
+gem "selenium-webdriver"
diff --git a/Gemfile.lock b/Gemfile.lock
index 64d1621..0a86f76 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -2,7 +2,7 @@ PATH
   remote: .
   specs:
     custom_elements-rails (0.1.2)
-      rails (>= 7)
+      rails (>= 7.0.0)
 
 GEM
   remote: https://rubygems.org/
@@ -81,9 +81,24 @@ GEM
       minitest (>= 5.1)
       mutex_m
       tzinfo (~> 2.0)
+    addressable (2.8.6)
+      public_suffix (>= 2.0.2, < 6.0)
+    appraisal (2.5.0)
+      bundler
+      rake
+      thor (>= 0.14.0)
     base64 (0.2.0)
     bigdecimal (3.1.7)
     builder (3.2.4)
+    capybara (3.40.0)
+      addressable
+      matrix
+      mini_mime (>= 0.1.3)
+      nokogiri (~> 1.11)
+      rack (>= 1.6.0)
+      rack-test (>= 0.6.3)
+      regexp_parser (>= 1.5, < 3.0)
+      xpath (~> 3.2)
     concurrent-ruby (1.2.3)
     connection_pool (2.4.1)
     crass (1.0.6)
@@ -94,6 +109,10 @@ GEM
       activesupport (>= 6.1)
     i18n (1.14.4)
       concurrent-ruby (~> 1.0)
+    importmap-rails (2.0.1)
+      actionpack (>= 6.0.0)
+      activesupport (>= 6.0.0)
+      railties (>= 6.0.0)
     io-console (0.7.2)
     irb (1.12.0)
       rdoc
@@ -107,6 +126,7 @@ GEM
       net-pop
       net-smtp
     marcel (1.0.4)
+    matrix (0.4.2)
     mini_mime (1.1.5)
     minitest (5.22.3)
     mutex_m (0.2.0)
@@ -122,8 +142,16 @@ GEM
     nio4r (2.7.1)
     nokogiri (1.16.4-arm64-darwin)
       racc (~> 1.4)
+    nokogiri (1.16.4-x86_64-linux)
+      racc (~> 1.4)
+    propshaft (0.8.0)
+      actionpack (>= 7.0.0)
+      activesupport (>= 7.0.0)
+      rack
+      railties (>= 7.0.0)
     psych (5.1.2)
       stringio
+    public_suffix (5.0.5)
     puma (6.4.2)
       nio4r (~> 2.0)
     racc (1.7.3)
@@ -167,27 +195,45 @@ GEM
     rake (13.2.1)
     rdoc (6.6.3.1)
       psych (>= 4.0.0)
+    regexp_parser (2.9.0)
     reline (0.5.1)
       io-console (~> 0.5)
+    rexml (3.2.6)
+    rubyzip (2.3.2)
+    selenium-webdriver (4.19.0)
+      base64 (~> 0.2)
+      rexml (~> 3.2, >= 3.2.5)
+      rubyzip (>= 1.2.2, < 3.0)
+      websocket (~> 1.0)
     sqlite3 (1.7.3-arm64-darwin)
+    sqlite3 (1.7.3-x86_64-linux)
     stringio (3.1.0)
     thor (1.3.1)
     timeout (0.4.1)
     tzinfo (2.0.6)
       concurrent-ruby (~> 1.0)
     webrick (1.8.1)
+    websocket (1.2.10)
     websocket-driver (0.7.6)
       websocket-extensions (>= 0.1.0)
     websocket-extensions (0.1.5)
+    xpath (3.2.0)
+      nokogiri (~> 1.8)
     zeitwerk (2.6.13)
 
 PLATFORMS
   arm64-darwin
+  x86_64-linux
 
 DEPENDENCIES
+  appraisal
+  capybara
   custom_elements-rails!
+  importmap-rails
+  propshaft
   puma
-  sqlite3
+  selenium-webdriver
+  sqlite3 (~> 1.4)
 
 BUNDLED WITH
    2.5.9
diff --git a/Rakefile b/Rakefile
index 7ca8948..4b696b3 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,3 +1,6 @@
-require "bundler/setup"
+APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
+load "rails/tasks/engine.rake"
+
+load "rails/tasks/statistics.rake"
 
 require "bundler/gem_tasks"
diff --git a/bin/rails b/bin/rails
new file mode 100755
index 0000000..8dcbcf3
--- /dev/null
+++ b/bin/rails
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+# This command will automatically be run when you run "rails" with Rails gems
+# installed from the root of your application.
+
+ENGINE_ROOT = File.expand_path("..", __dir__)
+ENGINE_PATH = File.expand_path("../lib/custom_elements/rails", __dir__)
+APP_PATH = File.expand_path("../test/dummy/config/application", __dir__)
+
+# Set up gems listed in the Gemfile.
+ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
+require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
+
+require "rails/all"
+require "rails/engine/commands"
diff --git a/custom_elements-rails.gemspec b/custom_elements-rails.gemspec
index a62cdc7..1e4022a 100644
--- a/custom_elements-rails.gemspec
+++ b/custom_elements-rails.gemspec
@@ -19,5 +19,7 @@ Gem::Specification.new do |spec|
     Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
   end
 
-  spec.add_dependency "rails", ">= 7"
+  spec.add_dependency "rails", ">= 7.0.0"
+
+  spec.add_development_dependency "appraisal"
 end
diff --git a/gemfiles/rails_7.0__importmap_rails_version_1.2.gemfile b/gemfiles/rails_7.0__importmap_rails_version_1.2.gemfile
new file mode 100644
index 0000000..a76c2c0
--- /dev/null
+++ b/gemfiles/rails_7.0__importmap_rails_version_1.2.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "importmap-rails", "~> 1.2"
+gem "propshaft"
+gem "puma"
+gem "sqlite3", "~> 1.4"
+gem "capybara"
+gem "selenium-webdriver"
+gem "rails", "~> 7.0"
+
+gemspec path: "../"
diff --git a/gemfiles/rails_7.0__importmap_rails_version_2.0.gemfile b/gemfiles/rails_7.0__importmap_rails_version_2.0.gemfile
new file mode 100644
index 0000000..f995426
--- /dev/null
+++ b/gemfiles/rails_7.0__importmap_rails_version_2.0.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "importmap-rails", "~> 2.0"
+gem "propshaft"
+gem "puma"
+gem "sqlite3", "~> 1.4"
+gem "capybara"
+gem "selenium-webdriver"
+gem "rails", "~> 7.0"
+
+gemspec path: "../"
diff --git a/gemfiles/rails_7.1__importmap_rails_version_1.2.gemfile b/gemfiles/rails_7.1__importmap_rails_version_1.2.gemfile
new file mode 100644
index 0000000..d8c5bad
--- /dev/null
+++ b/gemfiles/rails_7.1__importmap_rails_version_1.2.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "importmap-rails", "~> 1.2"
+gem "propshaft"
+gem "puma"
+gem "sqlite3", "~> 1.4"
+gem "capybara"
+gem "selenium-webdriver"
+gem "rails", "~> 7.1"
+
+gemspec path: "../"
diff --git a/gemfiles/rails_7.1__importmap_rails_version_2.0.gemfile b/gemfiles/rails_7.1__importmap_rails_version_2.0.gemfile
new file mode 100644
index 0000000..9359687
--- /dev/null
+++ b/gemfiles/rails_7.1__importmap_rails_version_2.0.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "importmap-rails", "~> 2.0"
+gem "propshaft"
+gem "puma"
+gem "sqlite3", "~> 1.4"
+gem "capybara"
+gem "selenium-webdriver"
+gem "rails", "~> 7.1"
+
+gemspec path: "../"
diff --git a/lib/custom_elements/rails/railtie.rb b/lib/custom_elements/rails/railtie.rb
index 599a8e0..60cff90 100644
--- a/lib/custom_elements/rails/railtie.rb
+++ b/lib/custom_elements/rails/railtie.rb
@@ -2,7 +2,9 @@ module CustomElements
   module Rails
     class Railtie < ::Rails::Engine
       initializer "custom_elements-rails.assets.precompile" do |app|
-        app.config.assets.precompile += %w( custom_elements-rails.js )
+        if app.config.respond_to? :assets
+          app.config.assets.precompile += %w( custom_elements-rails.js )
+        end
       end
     end
   end
diff --git a/test/custom_elements/integration_test.rb b/test/custom_elements/integration_test.rb
new file mode 100644
index 0000000..afadad4
--- /dev/null
+++ b/test/custom_elements/integration_test.rb
@@ -0,0 +1,9 @@
+require "system_helper"
+
+class CustomElements::IntegrationTest < ApplicationSystemTestCase
+  test "hello_element.js connects" do
+    visit elements_path
+
+    assert_text "connectedCallback(): hello_element.js"
+  end
+end
diff --git a/test/dummy/app/controllers/elements_controller.rb b/test/dummy/app/controllers/elements_controller.rb
new file mode 100644
index 0000000..aae3257
--- /dev/null
+++ b/test/dummy/app/controllers/elements_controller.rb
@@ -0,0 +1,4 @@
+class ElementsController < ApplicationController
+  def show
+  end
+end
diff --git a/test/dummy/app/helpers/elements_helper.rb b/test/dummy/app/helpers/elements_helper.rb
new file mode 100644
index 0000000..fed28ea
--- /dev/null
+++ b/test/dummy/app/helpers/elements_helper.rb
@@ -0,0 +1,2 @@
+module ElementsHelper
+end
diff --git a/test/dummy/app/javascript/application.js b/test/dummy/app/javascript/application.js
new file mode 100644
index 0000000..b38abf1
--- /dev/null
+++ b/test/dummy/app/javascript/application.js
@@ -0,0 +1,2 @@
+// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
+import "custom_elements"
\ No newline at end of file
diff --git a/test/dummy/app/javascript/custom_elements/hello_element.js b/test/dummy/app/javascript/custom_elements/hello_element.js
new file mode 100644
index 0000000..9bd85ca
--- /dev/null
+++ b/test/dummy/app/javascript/custom_elements/hello_element.js
@@ -0,0 +1,9 @@
+export default class extends HTMLElement {
+  constructor() {
+    super()
+  }
+
+  connectedCallback() {
+    this.textContent = "connectedCallback(): hello_element.js"
+  }
+}
diff --git a/test/dummy/app/javascript/custom_elements/index.js b/test/dummy/app/javascript/custom_elements/index.js
new file mode 100644
index 0000000..d0f0e54
--- /dev/null
+++ b/test/dummy/app/javascript/custom_elements/index.js
@@ -0,0 +1,3 @@
+import { eagerDefineCustomElementsFrom } from "custom_elements-rails"
+
+eagerDefineCustomElementsFrom("custom_elements", { prefix: "app" })
diff --git a/test/dummy/app/views/elements/show.html.erb b/test/dummy/app/views/elements/show.html.erb
new file mode 100644
index 0000000..9347b36
--- /dev/null
+++ b/test/dummy/app/views/elements/show.html.erb
@@ -0,0 +1,3 @@
+<h1>Elements#show</h1>
+
+<app-hello></app-hello>
diff --git a/test/dummy/app/views/layouts/application.html.erb b/test/dummy/app/views/layouts/application.html.erb
index f72b4ef..8217faf 100644
--- a/test/dummy/app/views/layouts/application.html.erb
+++ b/test/dummy/app/views/layouts/application.html.erb
@@ -7,6 +7,7 @@
     <%= csp_meta_tag %>
 
     <%= stylesheet_link_tag "application" %>
+    <%= javascript_importmap_tags %>
   </head>
 
   <body>
diff --git a/test/dummy/bin/importmap b/test/dummy/bin/importmap
new file mode 100755
index 0000000..36502ab
--- /dev/null
+++ b/test/dummy/bin/importmap
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+
+require_relative "../config/application"
+require "importmap/commands"
diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb
index ef2ca87..13a1711 100644
--- a/test/dummy/config/application.rb
+++ b/test/dummy/config/application.rb
@@ -13,7 +13,7 @@ class Application < Rails::Application
     # Please, add to the `ignore` list any other `lib` subdirectories that do
     # not contain `.rb` files, or that should not be reloaded or eager loaded.
     # Common ones are `templates`, `generators`, or `middleware`, for example.
-    config.autoload_lib(ignore: %w(assets tasks))
+    # config.autoload_lib(ignore: %w(assets tasks))
 
     # Configuration for the application, engines, and railties goes here.
     #
diff --git a/test/dummy/config/importmap.rb b/test/dummy/config/importmap.rb
new file mode 100644
index 0000000..c6d0d42
--- /dev/null
+++ b/test/dummy/config/importmap.rb
@@ -0,0 +1,3 @@
+pin "application"
+pin "custom_elements-rails"
+pin_all_from "app/javascript/custom_elements", under: "custom_elements"
diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb
index a125ef0..3743c9d 100644
--- a/test/dummy/config/routes.rb
+++ b/test/dummy/config/routes.rb
@@ -1,10 +1,3 @@
 Rails.application.routes.draw do
-  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
-
-  # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
-  # Can be used by load balancers and uptime monitors to verify that the app is live.
-  get "up" => "rails/health#show", as: :rails_health_check
-
-  # Defines the root path route ("/")
-  # root "posts#index"
+  resource :elements
 end
diff --git a/test/dummy/vendor/javascript/.keep b/test/dummy/vendor/javascript/.keep
new file mode 100644
index 0000000..e69de29
diff --git a/test/system_helper.rb b/test/system_helper.rb
new file mode 100644
index 0000000..53b4405
--- /dev/null
+++ b/test/system_helper.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+Capybara.server = :puma, { Silent: true }
+
+class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
+  driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
+end