]> git.openstreetmap.org Git - rails.git/blob - test/system/directions_test.rb
Merge remote-tracking branch 'upstream/pull/5603'
[rails.git] / test / system / directions_test.rb
1 require "application_system_test_case"
2
3 class DirectionsSystemTest < ApplicationSystemTestCase
4   test "removes popup on sidebar close" do
5     visit directions_path
6     stub_straight_routing(:start_instruction => "Start popup text")
7
8     fill_in "route_from", :with => "60 30"
9     fill_in "route_to", :with => "61 31"
10     click_on "Go"
11
12     within "#map" do
13       assert_no_content "Start popup text"
14     end
15
16     within_sidebar do
17       direction_entry = find "td", :text => "Start popup text"
18       direction_entry.click
19     end
20
21     within "#map" do
22       assert_content "Start popup text"
23     end
24
25     within_sidebar do
26       find("button[aria-label='Close']").click
27     end
28
29     within "#map" do
30       assert_no_content "Start popup text"
31     end
32   end
33
34   private
35
36   def stub_straight_routing(start_instruction: "Start here", finish_instruction: "Finish there")
37     stub_routing <<~CALLBACK
38       const distance = points[0].distanceTo(points[1]);
39       const time = distance * 30;
40       callback(false, {
41         line: points,
42         steps: [
43           [points[0],  8, "<b>1.</b> #{start_instruction}", distance, points],
44           [points[1], 14, "<b>2.</b> #{finish_instruction}", 0, [points[1]]]
45         ],
46         distance,
47         time
48       });
49     CALLBACK
50   end
51
52   def stub_routing(callback_code)
53     execute_script <<~SCRIPT
54       $(() => {
55         for (const engine of OSM.Directions.engines) {
56           engine.getRoute = (points, callback) => {
57             setTimeout(() => {
58               #{callback_code}
59             });
60           };
61         }
62       });
63     SCRIPT
64   end
65 end