|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +module Alchemy |
| 6 | + RSpec.describe PageFinder do |
| 7 | + let(:language) { create(:alchemy_language) } |
| 8 | + let!(:language_root) do |
| 9 | + create(:alchemy_page, :language_root, language: language) |
| 10 | + end |
| 11 | + |
| 12 | + before do |
| 13 | + PageDefinition.reset! |
| 14 | + Current.language = language |
| 15 | + end |
| 16 | + |
| 17 | + def create_page(name:, layout: "standard", parent: language_root) |
| 18 | + create( |
| 19 | + :alchemy_page, :public, |
| 20 | + name: name, |
| 21 | + page_layout: layout, |
| 22 | + parent: parent, |
| 23 | + language: language |
| 24 | + ) |
| 25 | + end |
| 26 | + |
| 27 | + # Page tree used by most tests: |
| 28 | + # |
| 29 | + # Products (standard) -> /products |
| 30 | + # +-- Product Details (product_detail) -> /products/:id (integer constraint) |
| 31 | + # | +-- Comments (standard) -> /products/:id/comments |
| 32 | + # +-- Product By Slug (product_by_slug) -> /products/:slug (no constraint) |
| 33 | + # |
| 34 | + # Blog (standard) -> /blog |
| 35 | + # +-- Blog Post (blog_post) -> /blog/:year/:slug (integer + string) |
| 36 | + # |
| 37 | + # Users (standard) -> /users |
| 38 | + # +-- User Profile (user_profile) -> /users/:uuid/profile (uuid constraint) |
| 39 | + |
| 40 | + let!(:products_page) { create_page(name: "Products") } |
| 41 | + let!(:product_detail_page) { create_page(name: "Product Details", layout: "product_detail", parent: products_page) } |
| 42 | + let!(:comments_page) { create_page(name: "Comments", parent: product_detail_page) } |
| 43 | + let!(:product_by_slug_page) { create_page(name: "Product By Slug", layout: "product_by_slug", parent: products_page) } |
| 44 | + |
| 45 | + let!(:blog_page) { create_page(name: "Blog") } |
| 46 | + let!(:blog_post_page) { create_page(name: "Blog Post", layout: "blog_post", parent: blog_page) } |
| 47 | + |
| 48 | + let!(:users_page) { create_page(name: "Users") } |
| 49 | + let!(:user_profile_page) { create_page(name: "User Profile", layout: "user_profile", parent: users_page) } |
| 50 | + |
| 51 | + let(:params) { ActionController::Parameters.new } |
| 52 | + |
| 53 | + it "defaults to Current.language.root_page" do |
| 54 | + page = described_class.new(params: params).call("products/123") |
| 55 | + expect(page).to eq(product_detail_page) |
| 56 | + expect(params[:id]).to eq("123") |
| 57 | + end |
| 58 | + |
| 59 | + it "finds a page by exact urlname" do |
| 60 | + page = described_class.new(params: params).call("products") |
| 61 | + expect(page).to eq(products_page) |
| 62 | + end |
| 63 | + |
| 64 | + it "returns nil when the parent page is nil" do |
| 65 | + expect(described_class.new(nil, params: params).call("products/123")).to be_nil |
| 66 | + end |
| 67 | + |
| 68 | + it "returns nil for a blank path" do |
| 69 | + expect(described_class.new(language_root, params: params).call("")).to be_nil |
| 70 | + end |
| 71 | + |
| 72 | + it "returns nil for a path with no matching prefix" do |
| 73 | + expect(described_class.new(language_root, params: params).call("other/123")).to be_nil |
| 74 | + end |
| 75 | + |
| 76 | + context "single-segment pattern with integer constraint" do |
| 77 | + it "matches /products/123" do |
| 78 | + page = described_class.new(language_root, params: params).call("products/123") |
| 79 | + expect(page).to eq(product_detail_page) |
| 80 | + expect(params[:id]).to eq("123") |
| 81 | + end |
| 82 | + |
| 83 | + it "rejects non-integer and falls through to unconstrained sibling" do |
| 84 | + page = described_class.new(language_root, params: params).call("products/some-slug") |
| 85 | + expect(page).to eq(product_by_slug_page) |
| 86 | + expect(params[:slug]).to eq("some-slug") |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context "multi-segment pattern" do |
| 91 | + it "matches /blog/2024/my-post and extracts both params" do |
| 92 | + page = described_class.new(language_root, params: params).call("blog/2024/my-post") |
| 93 | + expect(page).to eq(blog_post_page) |
| 94 | + expect(params[:year]).to eq("2024") |
| 95 | + expect(params[:slug]).to eq("my-post") |
| 96 | + end |
| 97 | + |
| 98 | + it "does not match with wrong segment count" do |
| 99 | + expect(described_class.new(language_root, params: params).call("blog/2024")).to be_nil |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context "pattern with static segments" do |
| 104 | + let(:uuid) { "550e8400-e29b-41d4-a716-446655440000" } |
| 105 | + |
| 106 | + it "matches /users/:uuid/profile" do |
| 107 | + page = described_class.new(language_root, params: params).call("users/#{uuid}/profile") |
| 108 | + expect(page).to eq(user_profile_page) |
| 109 | + expect(params[:uuid]).to eq(uuid) |
| 110 | + end |
| 111 | + |
| 112 | + it "does not match without the trailing static segment" do |
| 113 | + expect(described_class.new(language_root, params: params).call("users/#{uuid}")).to be_nil |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context "hierarchical patterns (grandchild under pattern page)" do |
| 118 | + it "matches /products/42/comments through the pattern parent" do |
| 119 | + page = described_class.new(language_root, params: params).call("products/42/comments") |
| 120 | + expect(page).to eq(comments_page) |
| 121 | + expect(params[:id]).to eq("42") |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + context "with competing sibling patterns of different segment counts" do |
| 126 | + let!(:shared_page) { create_page(name: "Shared") } |
| 127 | + let!(:multi_segment_page) { create_page(name: "Multi Segment", layout: "blog_post", parent: shared_page) } |
| 128 | + let!(:single_segment_page) { create_page(name: "Single Segment", layout: "product_by_slug", parent: shared_page) } |
| 129 | + let!(:child_of_single) { create_page(name: "My Post", parent: single_segment_page) } |
| 130 | + |
| 131 | + it "matches the first pattern sibling even when the other could also match via its child" do |
| 132 | + page = described_class.new(language_root, params: params).call("shared/2024/my-post") |
| 133 | + expect(page).to eq(multi_segment_page) |
| 134 | + expect(params[:year]).to eq("2024") |
| 135 | + expect(params[:slug]).to eq("my-post") |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + context "with a regex constraint" do |
| 140 | + let!(:warehouse_page) { create_page(name: "Warehouse") } |
| 141 | + let!(:sku_page) { create_page(name: "SKU Lookup", layout: "product_by_sku", parent: warehouse_page) } |
| 142 | + |
| 143 | + it "matches when the value satisfies the regex" do |
| 144 | + page = described_class.new(language_root, params: params).call("warehouse/AB-1234") |
| 145 | + expect(page).to eq(sku_page) |
| 146 | + expect(params[:sku]).to eq("AB-1234") |
| 147 | + end |
| 148 | + |
| 149 | + it "does not match when the value violates the regex" do |
| 150 | + expect(described_class.new(language_root, params: params).call("warehouse/invalid")).to be_nil |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | +end |
0 commit comments