Last active
March 22, 2023 19:43
-
-
Save aseroff/3755f228d352c9a2aa52e382280d5702 to your computer and use it in GitHub Desktop.
Testing rails controllers that change behavior based on "if turbo_frame_request?"
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
# Based on this code: https://github.com/hotwired/turbo-rails/blob/main/app/controllers/turbo/frames/frame_request.rb | |
# the response of turbo_frame_request? is based on the presence of a header named "Turbo-Frame". | |
# Therefore, you can exercise code behind this conditional in your controller tests by specifying that header in your GET requests. | |
# example controller action | |
class DashboardController < ApplicationController | |
# GET dashboard | |
def index | |
if turbo_frame_request? | |
# frame loading behavior | |
@frame_instance_variable = bar | |
render 'frame' | |
else | |
# page loading behavior | |
@dashboard = foo | |
render 'index' | |
end | |
end | |
end | |
# Example tests | |
test 'user gets dashboard' do | |
get dashboard_url | |
assert_response :success | |
end | |
test 'user gets dashboard frame' do | |
get dashboard_url, headers: { 'Turbo-Frame': '1' } | |
assert_response :success | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment