Skip to content

Commit

Permalink
better method of handling devise request
Browse files Browse the repository at this point in the history
  • Loading branch information
fugufish committed Sep 12, 2023
1 parent 57b562f commit 2fa7ada
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
11 changes: 11 additions & 0 deletions docs/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ class ExampleComponentTest < ViewComponent::TestCase
end
```

## Testing with Devise

Testing with Devise reqiures you include `Devise::Test::ControllerHelpers` in the `ViewComponent::TestCase` class. This
is typically done by adding the following to `test/test_helper.rb`:

```ruby
class ViewComponent::TestCase
include Devise::Test::ControllerHelpers
end
```

## Setting `request.path_parameters`

Since 2.31.0
Expand Down
22 changes: 2 additions & 20 deletions lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,13 @@ def refute_component_rendered
if ENV["DEBUG"]
warn(
"WARNING in `ViewComponent::TestHelpers`: Add `capybara` " \
"to Gemfile to use Capybara assertions."
"to Gemfile to use Capybara assertions."
)
end

# :nocov:
end

def self.included(base)
if defined?(Devise::Test::ControllerHelpers)
# the `@request` instance variable is used by Devise::Test::ControllerHelpers and must
# be in a `setup` block before including `Devise::Test::ControllerHelpers`
base.instance_eval do
setup do
@request = __vc_render_preview_controller.request
end
end
end
end

# Returns the result of a render_inline call.
#
# @return [ActionView::OutputBuffer]
Expand Down Expand Up @@ -87,7 +75,7 @@ def render_inline(component, **args, &block)
# @param params [Hash] Parameters to be passed to the preview.
# @return [Nokogiri::HTML]
def render_preview(name, from: __vc_test_helpers_preview_class, params: {})
previews_controller = __vc_render_preview_controller
previews_controller = __vc_test_helpers_build_controller(Rails.application.config.view_component.preview_controller.constantize)

# From what I can tell, it's not possible to overwrite all request parameters
# at once, so we set them individually here.
Expand Down Expand Up @@ -120,7 +108,6 @@ def render_in_view_context(*args, &block)
@rendered_content = vc_test_controller.view_context.instance_exec(*args, &block)
Nokogiri::HTML.fragment(@rendered_content)
end

ruby2_keywords(:render_in_view_context) if respond_to?(:ruby2_keywords, true)

# Set the Action Pack request variant for the given block:
Expand Down Expand Up @@ -239,7 +226,6 @@ def vc_test_request
end

# Note: We prefix private methods here to prevent collisions in consumer's tests.

private

def __vc_test_helpers_build_controller(klass)
Expand All @@ -258,9 +244,5 @@ def __vc_test_helpers_preview_class
rescue NameError
raise NameError, "`render_preview` expected to find #{result}, but it does not exist."
end

def __vc_render_preview_controller
@vc_render_preview_controller ||= __vc_test_helpers_build_controller(Rails.application.config.view_component.preview_controller.constantize)
end
end
end

0 comments on commit 2fa7ada

Please sign in to comment.