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