Prohlížeč zdrojového kódu

spec/system/source_browser_spec.rb

require "rails_helper"
RSpec.describe "Source browser", type: :system do
before { SourceBrowser.reload! }
it "navigates a .js file without a full browser reload and restores placeholder on back" do
visit source_index_path
within "#source-content" do
expect(page).to have_text("Vyberte soubor pro zobrazení zdrojového kódu.")
end
# Expand the full tree so every file link is reachable
page.execute_script("document.querySelectorAll('details').forEach(d => d.open = true)")
# Mark the current window — survives Turbo Drive navigation, gone after a full browser reload
page.execute_script("window.__pageMarker = true")
find("a[href='#{source_show_path(path: "app/javascript/application.js")}']").click
within "#source-content" do
expect(page).to have_text("@hotwired/turbo-rails")
end
expect(page.evaluate_script("window.__pageMarker")).to be true
# URL should have advanced in history
expect(page.current_url).to end_with(source_show_path(path: "app/javascript/application.js"))
# Back button should restore the previous URL and the placeholder
page.go_back
expect(page.current_url).to end_with(source_index_path)
within "#source-content" do
expect(page).to have_text("Vyberte soubor pro zobrazení zdrojového kódu.")
end
end
it "restores active item and expands its ancestors after back navigation" do
visit source_index_path
page.execute_script("document.querySelectorAll('details').forEach(d => d.open = true)")
# Step 1 – navigate to a deeply nested file
find("a[href='#{source_show_path(path: "app/javascript/controllers/application.js")}']").click
within "#source-content" do
expect(page).to have_text("Application.start()")
end
# Step 2 – navigate to a file in a different branch (expand tree first so it is reachable)
page.execute_script("document.querySelectorAll('details').forEach(d => d.open = true)")
find("a[href='#{source_show_path(path: "app/examples/sorting_algorithms/example.yml")}']").click
within "#source-content" do
expect(page).to have_text("bubble_sort")
end
# Step 3 – collapse the javascript directory
within "#source-tree" do
find("summary", text: "javascript").click
end
# Step 4 – go back; Turbo Drive restores the cached page, then syncActive() runs
page.go_back
within "#source-content" do
expect(page).to have_text("Application.start()")
end
# Active class must be set on the correct link
within "#source-tree" do
expect(page).to have_css(
".tree-file.active a[href='#{source_show_path(path: "app/javascript/controllers/application.js")}']"
)
end
# The link must be visible — syncActive() must have reopened the ancestor <details>
within "#source-tree" do
expect(page).to have_css(
"a[href='#{source_show_path(path: "app/javascript/controllers/application.js")}']",
visible: true
)
end
end
end