-
Notifications
You must be signed in to change notification settings - Fork 16
/
find_by_path_spec.rb
125 lines (103 loc) · 3.75 KB
/
find_by_path_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require "rails_helper"
describe FindByPath do
# Using an actual Model as it's a real pain to mock mongoid criterias and
# similar
class CompatibleModel
include Mongoid::Document
field :base_path, type: String
field :routes, type: Array, default: []
field :redirects, type: Array, default: []
end
FactoryBot.define do
factory :compatible_model do
base_path { "/base-path" }
transient do
exact_routes { [] }
prefix_routes { [] }
end
routes do
[{ path: base_path, type: "exact" }] +
Array(exact_routes).map { |path| { path: path, type: "exact" } } +
Array(prefix_routes).map { |path| { path: path, type: "prefix" } }
end
end
end
let(:model_class) { CompatibleModel }
describe ".find" do
subject { described_class.new(model_class).find(path) }
let(:path) { "/path" }
context "when there isn't an item matching the path" do
it { is_expected.to be_nil }
end
context "when there is a base_path that matches the path" do
let(:path) { "/base-path" }
let!(:instance) { create(:compatible_model, base_path: "/base-path") }
it { is_expected.to eq instance }
end
context "when there is a route exact match for the path" do
let(:exact_route_path) { "/base-path/exact-route" }
let(:path) { exact_route_path }
let!(:instance) { create(:compatible_model, exact_routes: exact_route_path) }
it { is_expected.to eq instance }
context "and there is also a base_path that matches the exact route path" do
let!(:superseding_instance) { create(:compatible_model, base_path: exact_route_path) }
it { is_expected.to eq superseding_instance }
end
end
context "when there is a redirect exact match for the path" do
let(:exact_route_path) { "/base-path/exact-route" }
let(:path) { exact_route_path }
let!(:instance) do
create(
:compatible_model,
routes: [],
redirects: [
{ path: exact_route_path, type: "exact", destination: "/somewhere" },
],
)
end
it { is_expected.to eq instance }
end
context "when there is a route with a prefix match" do
let(:prefix_route_path) { "/base-path/prefix-route" }
let!(:instance) { create(:compatible_model, prefix_routes: prefix_route_path) }
context "and the path matches the prefix path" do
let(:path) { prefix_route_path }
it { is_expected.to eq instance }
end
context "and the path is in a segment after the prefix path" do
let(:path) { "/base-path/prefix-route/a/b/c" }
it { is_expected.to eq instance }
end
context "and the path is a prefix at the same segment" do
let(:path) { "/base-path/prefix-route-longer-path" }
it "finds nothing as this isn't supported" do
is_expected.to be nil
end
end
context "but there is another item with a better path match" do
let(:path) { "/base-path/prefix-route/with-extra/segments" }
let!(:better_prefix_match) do
create(
:compatible_model,
base_path: "/base_path/prefix-route",
prefix_routes: "/base-path/prefix-route/with-extra",
)
end
it { is_expected.to eq better_prefix_match }
end
context "but there is another item with an exact path match" do
let(:path) { prefix_route_path }
let(:matching_base_path) { "/base-path/prefix-route" }
let!(:exact_match) do
create(
:compatible_model,
base_path: matching_base_path,
exact_routes: prefix_route_path,
)
end
it { is_expected.to eq exact_match }
end
end
end
end