From 9760d60ef24d3782c3069c5145b3063304089911 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Wed, 3 Jan 2024 09:52:04 -0500 Subject: [PATCH] Update Turbo Drive `` across navigations Closes [#549] Add System Test level coverage to ensure that Turbo Drive navigations will re-render any `` elements nested within the document's ``. To achieve this coverage, introduce the `PagesController#show` action that links to HTML pages that render their `` based on the `turbo_refresh_method` and `turbo_refresh_scroll` query parameters. [#549]: https://github.com/hotwired/turbo-rails/issues/549 --- .../dummy/app/controllers/pages_controller.rb | 2 ++ test/dummy/app/views/pages/show.html.erb | 16 ++++++++++++ test/dummy/config/routes.rb | 1 + test/system/navigations_test.rb | 26 +++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 test/dummy/app/controllers/pages_controller.rb create mode 100644 test/dummy/app/views/pages/show.html.erb create mode 100644 test/system/navigations_test.rb diff --git a/test/dummy/app/controllers/pages_controller.rb b/test/dummy/app/controllers/pages_controller.rb new file mode 100644 index 00000000..ce3bf586 --- /dev/null +++ b/test/dummy/app/controllers/pages_controller.rb @@ -0,0 +1,2 @@ +class PagesController < ApplicationController +end diff --git a/test/dummy/app/views/pages/show.html.erb b/test/dummy/app/views/pages/show.html.erb new file mode 100644 index 00000000..8dd87427 --- /dev/null +++ b/test/dummy/app/views/pages/show.html.erb @@ -0,0 +1,16 @@ +<%= turbo_refreshes_with method: params.fetch(:turbo_refresh_method, :replace).to_sym, scroll: params.fetch(:turbo_refresh_scroll, :reset).to_sym %> + +

<%= params[:id].titleize %>

+ +<% { + classic: { + turbo_refresh_method: :replace, + turbo_refresh_scroll: :reset, + }, + morph: { + turbo_refresh_method: :morph, + turbo_refresh_scroll: :preserve, + } +}.each do |id, refresh| %> + <%= link_to_unless_current id.to_s.titleize, page_path(id, refresh) %> +<% end %> diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index f84b8760..66b31081 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :pages, only: :show resources :articles do delete :destroy_all, on: :collection end diff --git a/test/system/navigations_test.rb b/test/system/navigations_test.rb new file mode 100644 index 00000000..6b75df9e --- /dev/null +++ b/test/system/navigations_test.rb @@ -0,0 +1,26 @@ +require "application_system_test_case" + +class NavigationsTest < ApplicationSystemTestCase + test "navigation updates Turbo Refresh meta tags" do + visit page_path(:classic) + + within "head", visible: false do + assert_selector :element, "meta", name: "turbo-refresh-method", content: "replace", visible: false, count: 1 + assert_selector :element, "meta", name: "turbo-refresh-scroll", content: "reset", visible: false, count: 1 + end + + click_link "Morph" + + within "head", visible: false do + assert_selector :element, "meta", name: "turbo-refresh-method", content: "morph", visible: false, count: 1 + assert_selector :element, "meta", name: "turbo-refresh-scroll", content: "preserve", visible: false, count: 1 + end + + click_link "Classic" + + within "head", visible: false do + assert_selector :element, "meta", name: "turbo-refresh-method", content: "replace", visible: false, count: 1 + assert_selector :element, "meta", name: "turbo-refresh-scroll", content: "reset", visible: false, count: 1 + end + end +end