]> git.openstreetmap.org Git - rails.git/blob - test/system/query_features_test.rb
Test overpass query enclosing features sorting
[rails.git] / test / system / query_features_test.rb
1 require "application_system_test_case"
2
3 class QueryFeaturesSystemTest < ApplicationSystemTestCase
4   test "sorts enclosing features correctly" do
5     visit "/#map=15/54.18315/7.88473"
6
7     within "#map" do
8       click_on "Query features"
9
10       stub_overpass :enclosing_elements => [
11         {
12           "type" => "relation",
13           "id" => 51477,
14           "bounds" => {
15             "minlat" => 47.2701114,
16             "minlon" => 5.8663153,
17             "maxlat" => 55.0991610,
18             "maxlon" => 15.0419309
19           },
20           "tags" => {
21             "admin_level" => "2",
22             "border_type" => "country",
23             "boundary" => "administrative",
24             "name" => "Deutschland",
25             "name:de" => "Deutschland",
26             "name:en" => "Germany",
27             "type" => "boundary"
28           }
29         },
30         {
31           "type" => "relation",
32           "id" => 51529,
33           "bounds" => {
34             "minlat" => 53.3598160,
35             "minlon" => 7.5211615,
36             "maxlat" => 55.0991610,
37             "maxlon" => 11.6723860
38           },
39           "tags" => {
40             "admin_level" => "4",
41             "border_type" => "state",
42             "boundary" => "administrative",
43             "name" => "Schleswig-Holstein",
44             "name:de" => "Schleswig-Holstein",
45             "name:en" => "Schleswig-Holstein",
46             "type" => "boundary"
47           }
48         },
49         {
50           "type" => "relation",
51           "id" => 3787052,
52           "bounds" => {
53             "minlat" => 54.1693099,
54             "minlon" => 7.8648436,
55             "maxlat" => 54.1907839,
56             "maxlon" => 7.9001626
57           },
58           "tags" => {
59             "name" => "Helgoland",
60             "name:de" => "Helgoland",
61             "name:en" => "Heligoland",
62             "place" => "island"
63           }
64         }
65       ]
66
67       click
68     end
69
70     within_sidebar do
71       assert_link "Heligoland"
72       assert_link "Schleswig-Holstein", :below => find_link("Heligoland")
73       assert_link "Germany", :below => find_link("Schleswig-Holstein")
74     end
75   end
76
77   test "sorts enclosing features correctly across antimeridian" do
78     visit "/#map=15/60/30"
79
80     within "#map" do
81       click_on "Query features"
82
83       stub_overpass :enclosing_elements => [
84         {
85           "type" => "relation",
86           "id" => 60189,
87           "bounds" => {
88             "minlat" => 41.1850968,
89             "minlon" => 19.4041722,
90             "maxlat" => 82.0586232,
91             "maxlon" => -168.9769440
92           },
93           "tags" => {
94             "admin_level" => "2",
95             "border_type" => "nation",
96             "boundary" => "administrative",
97             "name" => "Россия",
98             "name:en" => "Russia",
99             "name:ru" => "Россия",
100             "type" => "boundary"
101           }
102         },
103         {
104           "type" => "relation",
105           "id" => 1114253,
106           "bounds" => {
107             "minlat" => 59.8587853,
108             "minlon" => 29.6720697,
109             "maxlat" => 60.0370554,
110             "maxlon" => 30.2307057
111           },
112           "tags" => {
113             "name" => "Невская губа",
114             "name:en" => "Neva Bay",
115             "name:ru" => "Невская губа",
116             "natural" => "bay",
117             "type" => "multipolygon"
118           }
119         }
120       ]
121
122       click
123     end
124
125     within_sidebar do
126       assert_link "Neva Bay"
127       assert_link "Russia", :below => find_link("Neva Bay")
128     end
129   end
130
131   private
132
133   def stub_overpass(nearby_elements: [], enclosing_elements: [])
134     execute_script <<~SCRIPT
135       {
136         const originalFetch = fetch;
137         window.fetch = (...args) => {
138           const [resource, options] = args;
139
140           if (resource != OSM.OVERPASS_URL) return originalFetch(...args);
141
142           const data = options.body.get("data"),
143                 elements = data.includes("is_in") ? #{enclosing_elements.to_json} : #{nearby_elements.to_json};
144
145           return Promise.resolve({
146             json: () => Promise.resolve({ elements })
147           });
148         }
149       }
150     SCRIPT
151   end
152 end