-
Notifications
You must be signed in to change notification settings - Fork 166
Implement BaseComponent for per-component script packs #5475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aecf876
Implement BaseComponent for per-component script packs
aduth a1b0430
parentheses for lint appeasement
aduth 7b99775
Try to improve test isolation for BaseComponent
aduth 1015161
Add feature spec coverage for toggled USA banner
aduth f1c022d
Use view context to enqueue scripts
aduth 1357eb6
Add components to additional_paths
aduth a2518cf
Opt-in component scripts to type-checking
aduth 138f944
Abstract component script rendering
aduth eb2d5c5
Revert accordion script
aduth 23383d2
Use sidecar_files API for loading adjacent scripts
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,18 @@ | ||
| class BaseComponent < ViewComponent::Base | ||
| def render_in(view_context, &block) | ||
| render_scripts_in(view_context) | ||
| super(view_context, &block) | ||
| end | ||
|
|
||
| def render_scripts_in(view_context) | ||
| return if @rendered_scripts | ||
| @rendered_scripts = true | ||
| if view_context.respond_to?(:render_component_script) && self.class.scripts.present? | ||
| view_context.render_component_script(*self.class.scripts) | ||
| end | ||
| end | ||
|
|
||
| def self.scripts | ||
| @scripts ||= _sidecar_files(['js']).map { |file| File.basename(file, '.js') } | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| }, | ||
| "dependencies": { | ||
| "acorn": "^6.4.2", | ||
| "fast-glob": "^3.2.4", | ||
| "fast-glob": "^3.2.7", | ||
| "p-all": "^3.0.0" | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe BaseComponent, type: :component do | ||
| class ExampleComponent < BaseComponent | ||
| def call | ||
| '' | ||
| end | ||
| end | ||
|
|
||
| let(:lookup_context) { ActionView::LookupContext.new(ActionController::Base.view_paths) } | ||
| let(:view_context) { ActionView::Base.new(lookup_context, {}, controller) } | ||
|
|
||
| before do | ||
| allow_any_instance_of(ApplicationController).to receive(:view_context).and_return(view_context) | ||
| end | ||
|
|
||
| it 'does nothing when rendered' do | ||
| expect(view_context).not_to receive(:render_component_script) | ||
|
|
||
| render_inline(ExampleComponent.new) | ||
| end | ||
|
|
||
| context 'with sidecar script' do | ||
| class ExampleComponentWithScript < BaseComponent | ||
| def call | ||
| '' | ||
| end | ||
|
|
||
| def self._sidecar_files(extensions) | ||
| return ['/path/to/app/components/example_component_with_script.js'] if extensions == ['js'] | ||
| super(extensions) | ||
| end | ||
| end | ||
|
|
||
| it 'adds script to class variable when rendered' do | ||
| expect(view_context).to receive(:render_component_script). | ||
| with('example_component_with_script') | ||
|
|
||
| render_inline(ExampleComponentWithScript.new) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we do anything for
type: :componenttests?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pulls in ViewComponent and Capybara test helpers.
identity-idp/spec/rails_helper.rb
Lines 43 to 44 in 54fc578