diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js index b2a4046388a..492d5027a26 100644 --- a/app/assets/javascripts/discourse/app/lib/url.js +++ b/app/assets/javascripts/discourse/app/lib/url.js @@ -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` diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 9c2c569e4cb..becb2974646 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index 085a1bb0f54..24a9802be02 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -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) diff --git a/spec/system/topic_page_spec.rb b/spec/system/topic_page_spec.rb new file mode 100644 index 00000000000..3a93951a0ed --- /dev/null +++ b/spec/system/topic_page_spec.rb @@ -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) } +

+ x + Testing +

+ 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