discourse/spec/system/scroll_manager_service_spec.rb
David Taylor dfe94ba118
DEV: Move all scroll position reset/remember logic to a shared service (#22552)
Previously we were implementing scroll reset/memorization on a per-page basis. Many of these approaches relied on the `didInsertElement` hook, which is no longer appropriate since Discourse changed to use the 'loading slider' strategy for page transitions.

This commit rips out all of our custom scroll resetting/memorizing, and implements those things in a generic service. There are two features:

1. After every route transition, scroll to the top of the page
2. When using browser back/forward buttons, restore the last known scroll position for those routes

To opt-out of the behaviour, individual routes can add a scrollOnTransition boolean to their RouteInfo metadata using Ember's `buildRouteInfoMetadata` hook.
2023-07-13 13:40:08 +01:00

46 lines
1.3 KiB
Ruby

# frozen_string_literal: true
describe "Ember route-scroll-manager service", type: :system do
before do
Fabricate(:admin)
Fabricate.times(50, :post)
end
let(:discovery) { PageObjects::Pages::Discovery.new }
let(:topic) { PageObjects::Pages::Topic.new }
def current_scroll_y
page.evaluate_script("window.scrollY")
end
it "scrolls to top when navigating to new routes, and remembers scroll position when going back" do
visit("/")
expect(page).to have_css("body.navigation-topics")
expect(discovery.topic_list).to have_topics
page.execute_script <<~JS
document.querySelectorAll('.topic-list-item')[10].scrollIntoView(true);
JS
topic_list_scroll_y = current_scroll_y
try_until_success { expect(topic_list_scroll_y).to be > 0 }
find(".sidebar-section-link[data-link-name='all-categories']").click
expect(page).to have_css("body.navigation-categories")
try_until_success { expect(current_scroll_y).to eq(0) }
page.go_back
expect(page).to have_css("body.navigation-topics")
expect(discovery.topic_list).to have_topics
try_until_success { expect(current_scroll_y).to eq(topic_list_scroll_y) }
# Clicking site logo triggers refresh and scrolls to top
find("#site-logo").click
try_until_success { expect(current_scroll_y).to eq(0) }
end
end