FIX: TOC anchors in a subfolder setup (#21985)

Clicking on TOC heading anchors in a subfolder setup was breaking the current URL for users.

Other than the fix this change introduces the ability to test the subfolder setup in system specs.
This commit is contained in:
Jarek Radosz 2023-06-12 13:59:54 +02:00 committed by GitHub
parent 0f4f8c45f9
commit b7568ea4a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 5 deletions

View File

@ -155,6 +155,10 @@ const DiscourseURL = EmberObject.extend({
},
replaceState(path) {
if (path.startsWith("#")) {
path = this.router.currentURL.replace(/#.*$/, "") + path;
}
if (this.router.currentURL !== path) {
// Always use replaceState in the next runloop to prevent weird routes changing
// while URLs are loading. For example, while a topic loads it sets `currentPost`

View File

@ -388,7 +388,6 @@ RSpec.configure do |config|
end
# Match the request hostname to the value in `database.yml`
config.before(:all, type: %i[request multisite system]) { host! "test.localhost" }
config.before(:each, type: %i[request multisite system]) { host! "test.localhost" }
last_driven_by = nil

View File

@ -149,13 +149,24 @@ module Helpers
capture_output(:stderr, &block)
end
def set_subfolder(f)
global_setting :relative_url_root, f
def set_subfolder(new_root)
global_setting :relative_url_root, new_root
old_root = ActionController::Base.config.relative_url_root
ActionController::Base.config.relative_url_root = f
Rails.application.routes.stubs(:relative_url_root).returns(f)
ActionController::Base.config.relative_url_root = new_root
Rails.application.routes.stubs(:relative_url_root).returns(new_root)
before_next_spec { ActionController::Base.config.relative_url_root = old_root }
if RSpec.current_example.metadata[:type] == :system
Capybara.app.map("/") { run lambda { |env| [404, {}, [""]] } }
Capybara.app.map(new_root) { run Rails.application }
before_next_spec do
Capybara.app.map(new_root) { run lambda { |env| [404, {}, [""]] } }
Capybara.app.map("/") { run Rails.application }
end
end
end
def setup_git_repo(files)

View File

@ -0,0 +1,38 @@
# frozen_string_literal: true
describe "Topic page", type: :system do
fab!(:topic) { Fabricate(:topic) }
before { Fabricate(:post, topic: topic, cooked: <<~HTML) }
<h2 dir="ltr" id="toc-h2-testing" data-d-toc="toc-h2-testing" class="d-toc-post-heading">
<a name="toc-h2-testing" class="anchor" href="#toc-h2-testing">x</a>
Testing
</h2>
HTML
it "allows TOC anchor navigation" do
visit("/t/#{topic.slug}/#{topic.id}")
find("#toc-h2-testing").hover
find("a.anchor").click
try_until_success(timeout: 5) do
expect(current_url).to match("/t/#{topic.slug}/#{topic.id}#toc-h2-testing")
end
end
context "with a subfolder setup" do
before { set_subfolder "/forum" }
it "allows TOC anchor navigation" do
visit("/forum/t/#{topic.slug}/#{topic.id}")
find("#toc-h2-testing").hover
find("a.anchor").click
try_until_success(timeout: 5) do
expect(current_url).to match("/forum/t/#{topic.slug}/#{topic.id}#toc-h2-testing")
end
end
end
end