Skip to content

Commit 80c7c8e

Browse files
Add support for using view components in rails engine (#1951)
* Add module namespacing to generated components and other files First step to add support for using view components in rails engine * Config#default_preview_paths should include preview paths generated for rails engine * Add some dummy tests * Create test group for engine tests * Fix test and simplecov setup for engine tests * Tweak tests * Allow to run tests with 'm' ;) * Fix tests * Improve tests * Fix preview paths * Return unique values for default_preview_path * One step closer with tests * Remove trailing whitespaces * Update CHANGELOG * Update contributors list * Remove .keep * Update changelog - be more precise in changes description * Don't wrap with module namespace in tests and specs Instead of wraping test/spec with module namespacing pass module namespace inline with described class * Rename Dummy => TestEngine --------- Co-authored-by: Joel Hawksley <[email protected]>
1 parent 2d48c49 commit 80c7c8e

File tree

22 files changed

+209
-6
lines changed

22 files changed

+209
-6
lines changed

Rakefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ require "yard/mattr_accessor_handler"
88
Rake::TestTask.new(:test) do |t|
99
t.libs << "test"
1010
t.libs << "lib"
11-
t.test_files = FileList["test/**/*_test.rb"]
11+
t.test_files = FileList["test/sandbox/**/*_test.rb", "test/view_component/**/*_test.rb"]
12+
end
13+
14+
Rake::TestTask.new(:engine_test) do |t|
15+
t.libs << "test/test_engine"
16+
t.libs << "test/test_engine/lib"
17+
t.test_files = FileList["test/test_engine/**/*_test.rb"]
1218
end
1319

1420
begin
@@ -128,4 +134,4 @@ namespace :docs do
128134
end
129135
end
130136

131-
task default: :test
137+
task default: [:test, :engine_test]

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Add Rails engine support to generators.
14+
15+
*Tomasz Kowalewski*
16+
1317
## 3.14.0
1418

1519
* Defer to built-in caching for language environment setup, rather than manually using `actions/cache` in CI.

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ ViewComponent is built by over a hundred members of the community, including:
225225
<img src="https://avatars.githubusercontent.com/neanias?s=64" alt="neanias" width="32" />
226226
<img src="https://avatars.githubusercontent.com/allan-pires?s=64" alt="allan-pires" width="32" />
227227
<img src="https://avatars.githubusercontent.com/jasonkim?s=64" alt="jasonkim" width="32" />
228+
<img src="https://avatars.githubusercontent.com/tkowalewski" alt="tkowalewski" width="32" />
228229

229230
## Who uses ViewComponent?
230231

lib/rails/generators/component/templates/component.rb.tt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
<% module_namespacing do -%>
34
class <%= class_name %>Component < <%= parent_class %>
45
<%- if initialize_signature -%>
56
def initialize(<%= initialize_signature %>)
@@ -11,5 +12,5 @@ class <%= class_name %>Component < <%= parent_class %>
1112
content_tag :h1, "Hello world!"<%= ", data: { controller: \"#{stimulus_controller}\" }" if options["stimulus"] %>
1213
end
1314
<%- end -%>
14-
1515
end
16+
<% end -%>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# frozen_string_literal: true
22

3+
<% module_namespacing do -%>
34
class <%= class_name %>ComponentPreview < ViewComponent::Preview
45
def default
56
render(<%= class_name %>Component.new<%= "(#{render_signature})" if render_signature %>)
67
end
78
end
9+
<% end -%>

lib/rails/generators/rspec/templates/component_spec.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "rails_helper"
44

5-
RSpec.describe <%= class_name %>Component, type: :component do
5+
RSpec.describe <%= namespaced? ? "#{namespace.name}::" : '' %><%= class_name %>Component, type: :component do
66
pending "add some examples to (or delete) #{__FILE__}"
77

88
# it "renders something useful" do

lib/rails/generators/test_unit/templates/component_test.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "test_helper"
44

5-
class <%= class_name %>ComponentTest < ViewComponent::TestCase
5+
class <%= namespaced? ? "#{namespace.name}::" : '' %><%= class_name %>ComponentTest < ViewComponent::TestCase
66
def test_component_renders_something_useful
77
# assert_equal(
88
# %(<span>Hello, components!</span>),

lib/view_component/config.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,29 @@ def defaults
168168
# Defaults to `false`.
169169

170170
def default_preview_paths
171+
(default_rails_preview_paths + default_rails_engines_preview_paths).uniq
172+
end
173+
174+
def default_rails_preview_paths
171175
return [] unless defined?(Rails.root) && Dir.exist?("#{Rails.root}/test/components/previews")
172176

173177
["#{Rails.root}/test/components/previews"]
174178
end
175179

180+
def default_rails_engines_preview_paths
181+
return [] unless defined?(Rails::Engine)
182+
183+
registered_rails_engines_with_previews.map do |descendant|
184+
"#{descendant.root}/test/components/previews"
185+
end
186+
end
187+
188+
def registered_rails_engines_with_previews
189+
Rails::Engine.descendants.select do |descendant|
190+
defined?(descendant.root) && Dir.exist?("#{descendant.root}/test/components/previews")
191+
end
192+
end
193+
176194
def default_generate_options
177195
options = ActiveSupport::OrderedOptions.new(false)
178196
options.preview_path = ""

test/sandbox/test/generators/component_generator_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def test_component
1616
run_generator
1717

1818
assert_file "app/components/user_component.rb" do |component|
19+
assert_no_match(/module/, component)
1920
assert_match(/class UserComponent < ViewComponent::Base/, component)
2021
assert_no_match(/def initialize/, component)
2122
end
@@ -25,6 +26,7 @@ def test_component_tests
2526
run_generator %w[user --test-framework test_unit]
2627

2728
assert_file "test/components/user_component_test.rb" do |component|
29+
assert_no_match(/module/, component)
2830
assert_match(/class UserComponentTest < /, component)
2931
assert_match(/def test_component_renders_something_useful/, component)
3032
end
@@ -35,6 +37,7 @@ def test_component_preview
3537
run_generator %w[user --preview]
3638

3739
assert_file "test/components/previews/user_component_preview.rb" do |component|
40+
assert_no_match(/module/, component)
3841
assert_match(/class UserComponentPreview < /, component)
3942
assert_match(/render\(UserComponent.new\)/, component)
4043
end
@@ -45,6 +48,7 @@ def test_component_with_arguments
4548
run_generator %w[user name]
4649

4750
assert_file "app/components/user_component.rb" do |component|
51+
assert_no_match(/module/, component)
4852
assert_match(/class UserComponent < /, component)
4953
assert_match(/def initialize\(name:\)/, component)
5054
end
@@ -65,6 +69,7 @@ def test_component_with_parent
6569
run_generator %w[user --parent MyOtherBaseComponent]
6670

6771
assert_file "app/components/user_component.rb" do |component|
72+
assert_no_match(/module/, component)
6873
assert_match(/class UserComponent < MyOtherBaseComponent/, component)
6974
end
7075
end
@@ -74,6 +79,7 @@ def test_component_with_parent_and_application_component_class
7479
run_generator %w[user --parent MyOtherBaseComponent]
7580

7681
assert_file "app/components/user_component.rb" do |component|
82+
assert_no_match(/module/, component)
7783
assert_match(/class UserComponent < MyOtherBaseComponent/, component)
7884
end
7985
end
@@ -84,6 +90,7 @@ def test_component_with_parent_and_custom_component_parent_class
8490
run_generator %w[user --parent MyOtherBaseComponent]
8591

8692
assert_file "app/components/user_component.rb" do |component|
93+
assert_no_match(/module/, component)
8794
assert_match(/class UserComponent < MyOtherBaseComponent/, component)
8895
end
8996
end
@@ -92,13 +99,17 @@ def test_component_with_parent_and_custom_component_parent_class
9299
def test_component_with_namespace
93100
run_generator %w[admins/user]
94101

95-
assert_file "app/components/admins/user_component.rb", /class Admins::UserComponent < /
102+
assert_file "app/components/admins/user_component.rb" do |component|
103+
assert_no_match(/module/, component)
104+
assert_match(/class Admins::UserComponent < /, component)
105+
end
96106
end
97107

98108
def test_component_tests_with_namespace
99109
run_generator %w[admins/user --test-framework test_unit]
100110

101111
assert_file "test/components/admins/user_component_test.rb" do |component|
112+
assert_no_match(/module/, component)
102113
assert_match(/class Admins::UserComponentTest < /, component)
103114
assert_match(/def test_component_renders_something_useful/, component)
104115
end
@@ -142,6 +153,7 @@ def test_generating_components_with_application_component_class
142153
run_generator %w[user]
143154

144155
assert_file "app/components/user_component.rb" do |component|
156+
assert_no_match(/module/, component)
145157
assert_match(/class UserComponent < ApplicationComponent/, component)
146158
end
147159
end
@@ -152,6 +164,7 @@ def test_generating_components_with_custom_component_parent_class
152164
run_generator %w[user]
153165

154166
assert_file "app/components/user_component.rb" do |component|
167+
assert_no_match(/module/, component)
155168
assert_match(/class UserComponent < MyBaseComponent/, component)
156169
end
157170
end
@@ -163,6 +176,7 @@ def test_generating_components_with_application_component_class_and_custom_paren
163176
run_generator %w[user]
164177

165178
assert_file "app/components/user_component.rb" do |component|
179+
assert_no_match(/module/, component)
166180
assert_match(/class UserComponent < MyBaseComponent/, component)
167181
end
168182
end

test/sandbox/test/generators/preview_generator_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_component_preview
1818
run_generator %w[user --preview]
1919

2020
assert_file "test/components/previews/user_component_preview.rb" do |component|
21+
assert_no_match(/module/, component)
2122
assert_match(/class UserComponentPreview < /, component)
2223
assert_match(/render\(UserComponent.new\)/, component)
2324
end
@@ -29,6 +30,7 @@ def test_component_preview_with_preview_path_option
2930
run_generator %w[user --preview --preview-path other/test/components/previews]
3031

3132
assert_file "other/test/components/previews/user_component_preview.rb" do |component|
33+
assert_no_match(/module/, component)
3234
assert_match(/class UserComponentPreview < /, component)
3335
assert_match(/render\(UserComponent.new\)/, component)
3436
end
@@ -40,6 +42,7 @@ def test_component_preview_with_one_overridden_preview_path
4042
run_generator %w[user --preview]
4143

4244
assert_file "spec/components/previews/user_component_preview.rb" do |component|
45+
assert_no_match(/module/, component)
4346
assert_match(/class UserComponentPreview < /, component)
4447
assert_match(/render\(UserComponent.new\)/, component)
4548
end
@@ -61,6 +64,7 @@ def test_component_preview_with_two_overridden_preview_paths_and_preview_path_op
6164
run_generator %w[user --preview --preview-path other/test/components/previews]
6265

6366
assert_file "other/test/components/previews/user_component_preview.rb" do |component|
67+
assert_no_match(/module/, component)
6468
assert_match(/class UserComponentPreview < /, component)
6569
assert_match(/render\(UserComponent.new\)/, component)
6670
end
@@ -72,6 +76,7 @@ def test_component_preview_with_namespace
7276
run_generator %w[admins/user --preview]
7377

7478
assert_file "test/components/previews/admins/user_component_preview.rb" do |component|
79+
assert_no_match(/module/, component)
7580
assert_match(/class Admins::UserComponentPreview < /, component)
7681
assert_match(/render\(Admins::UserComponent.new\)/, component)
7782
end
@@ -83,6 +88,7 @@ def test_component_generator_with_attributes
8388
run_generator %w[user nickname fullname]
8489

8590
assert_file "test/components/previews/user_component_preview.rb" do |preview|
91+
assert_no_match(/module/, preview)
8692
assert_match(/class UserComponentPreview/, preview)
8793
assert_match(/render\(UserComponent.new\(nickname: "nickname", fullname: "fullname"\)\)/, preview)
8894
end
@@ -94,6 +100,7 @@ def test_component_with_namespace_and_attributes
94100
run_generator %w[admins/user nickname fullname]
95101

96102
assert_file "test/components/previews/admins/user_component_preview.rb" do |preview|
103+
assert_no_match(/module/, preview)
97104
assert_match(/class Admins::UserComponentPreview/, preview)
98105
assert_match(/render\(Admins::UserComponent.new\(nickname: "nickname", fullname: "fullname"\)\)/, preview)
99106
end

0 commit comments

Comments
 (0)