-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <joel@hawksley.org>
- Loading branch information
1 parent
2d48c49
commit 80c7c8e
Showing
22 changed files
with
209 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
lib/rails/generators/preview/templates/component_preview.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
<% module_namespacing do -%> | ||
class <%= class_name %>ComponentPreview < ViewComponent::Preview | ||
def default | ||
render(<%= class_name %>Component.new<%= "(#{render_signature})" if render_signature %>) | ||
end | ||
end | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/test_engine/app/components/test_engine/example_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>Add Example template here</div> |
5 changes: 5 additions & 0 deletions
5
test/test_engine/app/components/test_engine/example_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module TestEngine | ||
class ExampleComponent < ViewComponent::Base; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "test_engine/version" | ||
require "test_engine/engine" | ||
|
||
module TestEngine | ||
# Your code goes here... | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module TestEngine | ||
class Engine < ::Rails::Engine | ||
isolate_namespace TestEngine | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module TestEngine | ||
VERSION = "0.1.0" | ||
end |
9 changes: 9 additions & 0 deletions
9
test/test_engine/test/components/previews/test_engine/example_component_preview.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module TestEngine | ||
class ExampleComponentPreview < ViewComponent::Preview | ||
def default | ||
render(ExampleComponent.new) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../test_helper" | ||
|
||
module ViewComponent | ||
class ConfigTest < TestCase | ||
def setup | ||
@config = ViewComponent::Config.new | ||
end | ||
|
||
def test_defaults_are_correct | ||
assert_equal @config.generate, {preview_path: ""} | ||
assert_equal @config.preview_controller, "ViewComponentsController" | ||
assert_equal @config.preview_route, "/rails/view_components" | ||
assert_equal @config.show_previews_source, false | ||
assert_equal @config.instrumentation_enabled, false | ||
assert_equal @config.use_deprecated_instrumentation_name, true | ||
assert_equal @config.render_monkey_patch_enabled, true | ||
assert_equal @config.show_previews, true | ||
assert_equal @config.preview_paths, ["#{TestEngine::Engine.root}/test/components/previews"] | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
test/test_engine/test/generators/component_generator_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../test_helper" | ||
require "rails/generators/component/component_generator" | ||
|
||
class ComponentGeneratorTest < Rails::Generators::TestCase | ||
tests Rails::Generators::ComponentGenerator | ||
destination Dir.mktmpdir | ||
setup :prepare_destination | ||
|
||
def test_component | ||
run_generator %w[example] | ||
|
||
assert_file "app/components/test_engine/example_component.rb" do |component| | ||
assert_match(/module TestEngine/, component) | ||
assert_match(/class ExampleComponent < ViewComponent::Base/, component) | ||
assert_no_match(/def initialize/, component) | ||
end | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
test/test_engine/test/generators/preview_generator_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../test_helper" | ||
require "rails/generators/preview/component_generator" | ||
|
||
class PreviewGeneratorTest < Rails::Generators::TestCase | ||
tests Preview::Generators::ComponentGenerator | ||
destination Dir.mktmpdir | ||
setup :prepare_destination | ||
|
||
def test_component_preview | ||
with_preview_paths([]) do | ||
run_generator %w[example --preview] | ||
|
||
assert_file "test/components/previews/test_engine/example_component_preview.rb" do |component| | ||
assert_match(/module TestEngine/, component) | ||
assert_match(/class ExampleComponentPreview < /, component) | ||
assert_match(/render\(ExampleComponent.new\)/, component) | ||
end | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
test/test_engine/test/generators/test_unit_generator_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../test_helper" | ||
require "rails/generators/test_unit/component_generator" | ||
|
||
class TestUnitGeneratorTest < Rails::Generators::TestCase | ||
tests TestUnit::Generators::ComponentGenerator | ||
destination Dir.mktmpdir | ||
setup :prepare_destination | ||
|
||
def test_component_tests | ||
run_generator %w[example --test-framework test_unit] | ||
|
||
assert_file "test/components/test_engine/example_component_test.rb" do |component| | ||
assert_no_match(/module/, component) | ||
assert_match(/class TestEngine::ExampleComponentTest < /, component) | ||
assert_match(/def test_component_renders_something_useful/, component) | ||
end | ||
end | ||
end |
Oops, something went wrong.