0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-11 02:59:07 +08:00
discourse/spec/system/nested_category_default_spec.rb
Mark VanLandingham 63c4950c90
DEV: route nested view through topic route (#40820)
## What

Draft spike for rendering nested replies through the normal topic
route/controller/template (`/t/:slug/:id[/post_number]`) instead of a
separate Ember `/n/...` browser page route.

This keeps `/n/...` for internal JSON/API endpoints and legacy browser
redirects, while using the canonical topic route for user-facing nested
topic navigation.

## Changes

- Add nested-topic loading/setup to `topic.fromParams` so `/t/...` can
render the nested tree.
- Render `<Nested>` from the topic template when the topic is in nested
mode.
- Keep the nested controller as the nested tree state owner, bridged
from the topic route/controller.
- Redirect legacy client-side `/n/...` browser routes to the
corresponding `/t/...` routes.
- Preserve nested context depth through model processing, controller
state, and cache snapshots.
- Update nested post/admin URLs, notifications, activity, copy links,
and system specs to canonical `/t/...` browser URLs.
- Split scroll-anchor persistence from full nested model cache snapshots
to avoid expensive cache writes on scroll.
- Add bounded scroll restoration retries for mobile browser-back focus
restoration.
2026-06-15 11:42:20 -05:00

119 lines
4.4 KiB
Ruby
Vendored

# frozen_string_literal: true
require_relative "../support/nested_replies_helpers"
RSpec.describe "Nested view category default" do
include NestedRepliesHelpers
fab!(:admin)
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:category)
fab!(:nested_category) { Fabricate(:category, name: "Nested Category") }
fab!(:topic) { Fabricate(:topic, user: user, category: nested_category) }
fab!(:op) { Fabricate(:post, topic: topic, user: user, post_number: 1) }
fab!(:reply) { Fabricate(:post, topic: topic, user: Fabricate(:user), raw: "A reply") }
let(:nested_view) { PageObjects::Pages::NestedView.new }
let(:category_page) { PageObjects::Pages::Category.new }
let(:topic_list) { PageObjects::Components::TopicList.new }
let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
before do
SiteSetting.nested_replies_enabled = true
nested_category.category_setting.update!(nested_replies_default: true)
Fabricate(:nested_topic, topic: topic)
end
describe "category settings UI" do
before { sign_in(admin) }
it "allows admin to enable nested view default for a category" do
unchecked_category = Fabricate(:category, name: "Unchecked Category")
category_page.visit_settings(unchecked_category)
form.field("category_setting.nested_replies_default").toggle
category_page.save_settings
unchecked_category.reload
expect(unchecked_category.nested_replies_default).to eq(true)
end
it "shows checkbox as checked when category has nested default enabled" do
category_page.visit_settings(nested_category)
expect(form.field("category_setting.nested_replies_default")).to be_checked
end
end
describe "topic routing" do
before { sign_in(user) }
it "renders nested view when visiting a topic URL directly" do
page.visit("/t/#{topic.slug}/#{topic.id}")
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}})
expect(nested_view).to have_nested_view
end
it "renders nested view when clicking a topic from the category page" do
page.visit("/c/#{nested_category.slug}/#{nested_category.id}")
find(".topic-list-item .raw-topic-link[data-topic-id='#{topic.id}']").click
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}})
expect(nested_view).to have_nested_view
end
it "takes the user to the top of a nested topic from a scrolled list" do
category_page.visit(nested_category)
expect(topic_list).to have_topic(topic)
topic_list.scroll_page_down
expect(topic_list).to be_scrolled_down
topic_list.visit_topic(topic)
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}})
expect(nested_view).to have_nested_view
try_until_success { expect(page.evaluate_script("window.scrollY")).to eq(0) }
end
it "does not redirect topics in categories without nested default" do
normal_topic = Fabricate(:topic, user: user, category: category)
Fabricate(:post, topic: normal_topic, user: user, post_number: 1)
page.visit("/t/#{normal_topic.slug}/#{normal_topic.id}")
expect(page).to have_current_path(%r{/t/#{normal_topic.slug}/#{normal_topic.id}})
expect(nested_view).to have_no_nested_view
end
it "respects ?flat=1 to force flat view even in nested-default category" do
page.visit("/t/#{topic.slug}/#{topic.id}?flat=1")
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}})
expect(page).to have_current_path(/flat=1/)
expect(nested_view).to have_no_nested_view
end
it "does not redirect to nested when navigating within flat view (e.g. topic timeline)" do
page.visit("/t/#{topic.slug}/#{topic.id}?flat=1")
expect(nested_view).to have_no_nested_view
# Simulate the exact code path the topic timeline uses:
# topic.urlForPostNumber() → DiscourseURL.routeTo()
# This exercises the topic-url-for-post-number transformer which rewrites
# URLs to /nested/ for nested-default categories.
page.execute_script(<<~JS)
(function() {
var topic = Discourse.lookup("controller:topic").model;
var url = topic.urlForPostNumber(#{reply.post_number});
require("discourse/lib/url").default.routeTo(url);
})();
JS
expect(page).to have_current_path(%r{/t/#{topic.slug}/#{topic.id}})
expect(nested_view).to have_no_nested_view
end
end
end