]> git.openstreetmap.org Git - rails.git/blob - test/system/directions_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / system / directions_test.rb
1 # frozen_string_literal: true
2
3 require "application_system_test_case"
4
5 class DirectionsSystemTest < ApplicationSystemTestCase
6   test "updates route output on mode change" do
7     visit directions_path
8     stub_straight_routing(:start_instruction => "Start popup text")
9
10     find_by_id("route_from").set("60 30").send_keys :enter
11     find_by_id("route_to").set("61 31").send_keys :enter
12
13     within "#sidebar" do
14       assert_content "Start popup text (car)"
15     end
16
17     choose :option => "bicycle", :allow_label_click => true
18
19     within "#sidebar" do
20       assert_content "Start popup text (bicycle)"
21     end
22   end
23
24   test "swaps route endpoints on reverse button click" do
25     visit directions_path
26     stub_straight_routing(:start_instruction => "Start popup text")
27
28     find_by_id("route_from").set("60 30").send_keys :enter
29     find_by_id("route_to").set("61 31").send_keys :enter
30
31     click_on :class => "reverse_directions"
32
33     start_location = find_by_id("route_from").value
34     finish_location = find_by_id("route_to").value
35
36     click_on :class => "reverse_directions"
37
38     assert_equal finish_location, find_by_id("route_from").value
39     assert_equal start_location, find_by_id("route_to").value
40   end
41
42   test "removes popup on sidebar close" do
43     visit directions_path
44     stub_straight_routing(:start_instruction => "Start popup text")
45
46     find_by_id("route_from").set("60 30").send_keys :enter
47     find_by_id("route_to").set("61 31").send_keys :enter
48
49     within "#map" do
50       assert_no_content "Start popup text"
51     end
52
53     within_sidebar do
54       direction_entry = find "td", :text => "Start popup text"
55       direction_entry.click
56     end
57
58     within "#map" do
59       assert_content "Start popup text"
60     end
61
62     find("#sidebar .sidebar-close-controls button[aria-label='Close']").click
63
64     within "#map" do
65       assert_no_content "Start popup text"
66     end
67   end
68
69   private
70
71   def stub_straight_routing(start_instruction: "Start here", finish_instruction: "Finish there")
72     stub_routing <<~CALLBACK
73       const distance = points[0].distanceTo(points[1]);
74       const time = distance * 30;
75       return Promise.resolve({
76         line: points,
77         steps: [
78           ["start", `<b>1.</b> #{start_instruction} (${this.mode})`, distance, points],
79           ["destination", `<b>2.</b> #{finish_instruction} (${this.mode})`, 0, [points[1]]]
80         ],
81         distance,
82         time
83       });
84     CALLBACK
85   end
86
87   def stub_routing(callback_code)
88     execute_script <<~SCRIPT
89       $(() => {
90         for (const engine of OSM.Directions.engines) {
91           engine.getRoute = function(points, signal) {
92               #{callback_code}
93           };
94         }
95       });
96     SCRIPT
97   end
98 end