1 # frozen_string_literal: true
3 require "application_system_test_case"
5 class DirectionsSystemTest < ApplicationSystemTestCase
6 test "updates route output on mode change" do
8 stub_straight_routing(:start_instruction => "Start popup text")
10 find_by_id("route_from").set("60 30").send_keys :enter
11 find_by_id("route_to").set("61 31").send_keys :enter
14 assert_content "Start popup text (car)"
17 choose :option => "bicycle", :allow_label_click => true
20 assert_content "Start popup text (bicycle)"
24 test "swaps route endpoints on reverse button click" do
26 stub_straight_routing(:start_instruction => "Start popup text")
28 find_by_id("route_from").set("60 30").send_keys :enter
29 find_by_id("route_to").set("61 31").send_keys :enter
31 click_on :class => "reverse_directions"
33 start_location = find_by_id("route_from").value
34 finish_location = find_by_id("route_to").value
36 click_on :class => "reverse_directions"
38 assert_equal finish_location, find_by_id("route_from").value
39 assert_equal start_location, find_by_id("route_to").value
42 test "removes popup on sidebar close" do
44 stub_straight_routing(:start_instruction => "Start popup text")
46 find_by_id("route_from").set("60 30").send_keys :enter
47 find_by_id("route_to").set("61 31").send_keys :enter
50 assert_no_content "Start popup text"
54 direction_entry = find "td", :text => "Start popup text"
59 assert_content "Start popup text"
62 find("#sidebar .sidebar-close-controls button[aria-label='Close']").click
65 assert_no_content "Start popup text"
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({
78 ["start", `<b>1.</b> #{start_instruction} (${this.mode})`, distance, points],
79 ["destination", `<b>2.</b> #{finish_instruction} (${this.mode})`, 0, [points[1]]]
87 def stub_routing(callback_code)
88 execute_script <<~SCRIPT
90 for (const engine of OSM.Directions.engines) {
91 engine.getRoute = function(points, signal) {