diff --git a/assets/ts/schedule/components/ScheduleFinder.tsx b/assets/ts/schedule/components/ScheduleFinder.tsx index 41f64731c4..c7d2efa777 100644 --- a/assets/ts/schedule/components/ScheduleFinder.tsx +++ b/assets/ts/schedule/components/ScheduleFinder.tsx @@ -1,5 +1,5 @@ import React, { ReactElement } from "react"; -import { useDispatch, useSelector } from "react-redux"; +import { useSelector } from "react-redux"; import { Dispatch } from "redux"; import { Route, DirectionId } from "../../__v3api"; import { @@ -44,43 +44,18 @@ const ScheduleFinder = ({ scheduleNote, hasServiceToday }: Props): ReactElement => { - const dispatch = useDispatch(); const { modalOpen, selectedOrigin } = useSelector( (state: StoreProps) => state ); const currentDirection = useDirectionChangeEvent(directionId); - const openOriginModal = (): void => { - if (!modalOpen) { - dispatch({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "origin" - } - }); - } - }; const openScheduleModal = (): void => { if (selectedOrigin !== undefined && !modalOpen) { - dispatch({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "schedule" - } - }); + document.location.href = `/departures/?route_id=${route.id}&direction_id=${directionId}&stop_id=${selectedOrigin}`; } }; - const handleOriginSelectClick = (): void => { - dispatch({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "origin" - } - }); - }; - const isFerryRoute = routeToModeName(route) === "ferry"; return ( @@ -92,7 +67,6 @@ const ScheduleFinder = ({ { updateURL(""); }; -export const handleOriginSelectClick = (dispatch: Dispatch): void => { - dispatch({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "origin" - } - }); -}; - const getDirectionAndMap = ( schedulePageData: SchedulePageData, mapData: MapData, @@ -239,7 +223,6 @@ const ScheduleNote = ({ closeModal={closeModal} directionChanged={changeDirection} initialDirection={currentDirection} - handleOriginSelectClick={handleOriginSelectClick} originChanged={changeOrigin} route={route} routePatternsByDirection={routePatternsByDirection} diff --git a/assets/ts/schedule/components/__tests__/ScheduleFinderTest.tsx b/assets/ts/schedule/components/__tests__/ScheduleFinderTest.tsx index 0fcc289ab7..1b21cff772 100644 --- a/assets/ts/schedule/components/__tests__/ScheduleFinderTest.tsx +++ b/assets/ts/schedule/components/__tests__/ScheduleFinderTest.tsx @@ -256,47 +256,5 @@ describe("ScheduleFinder", () => { expect(lastSelectedOriginElement.selected).toBeFalse(); }); - it("Opens the origin modal when clicking on the origin drop-down in the schedule modal", async () => { - const user = userEvent.setup(); - const dispatchSpy = jest.fn(); - jest.spyOn(reactRedux, "useDispatch").mockImplementation(() => dispatchSpy); - - renderWithProviders( - {}} - changeDirection={() => {}} - changeOrigin={() => {}} - closeModal={() => {}} - scheduleNote={null} - hasServiceToday={true} - />, - { - preloadedState: { - modalOpen: true, - selectedOrigin: "123", - modalMode: "schedule" - } - } - ); - - // select the last node (i.e. origin drop-down) and choose an option - const scheduleFinderModal = screen.getByLabelText(/Schedules on the.*/); - const originSelectElement = within(scheduleFinderModal).getByTestId( - "schedule-finder-origin-select" - ); - await user.click(originSelectElement); - - expect(dispatchSpy).toHaveBeenCalledWith({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "origin" - } - }); - }); + }); diff --git a/assets/ts/schedule/components/__tests__/SchedulePageTest.tsx b/assets/ts/schedule/components/__tests__/SchedulePageTest.tsx index 566a336800..2d47ca50d9 100644 --- a/assets/ts/schedule/components/__tests__/SchedulePageTest.tsx +++ b/assets/ts/schedule/components/__tests__/SchedulePageTest.tsx @@ -11,8 +11,7 @@ import { MapData, StaticMapData } from "../../../leaflet/components/__mapdata"; import { SchedulePage, changeOrigin, - changeDirection, - handleOriginSelectClick + changeDirection } from "../SchedulePage"; import * as schedulePage from "../SchedulePage"; import * as routePatternsByDirectionData from "./test-data/routePatternsByDirectionData.json"; @@ -575,65 +574,9 @@ describe("SchedulePage", () => { await user.click(originSelect); - expect(dispatchSpy).toHaveBeenCalledTimes(2); + expect(dispatchSpy).toHaveBeenCalledTimes(1); }); - it("Opens the origin modal", async () => { - const user = userEvent.setup(); - const dispatchSpy = jest.fn(); - jest.spyOn(reactRedux, "useDispatch").mockImplementation(() => { - return dispatchSpy; - }); - renderWithProviders( - - ); - - jest.spyOn(reactRedux, "useSelector").mockImplementation(() => { - return { - selectedDirection: 0, - selectedOrigin: "place-welln", - modalMode: "origin", - modalOpen: false - }; - }); - - const buttons = screen.getAllByRole("button"); - expect(buttons.length).toBeGreaterThan(1); - - await user.click(buttons[1]); - - // first call is with INITIALIZE - expect(dispatchSpy).toHaveBeenNthCalledWith(2, { - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "origin" - } - }); - }); it("Closes the schedule modal", async () => { const user = userEvent.setup(); @@ -732,96 +675,6 @@ describe("SchedulePage", () => { ); }); - it("Opens the origin modal when clicking on the origin drop-down in the schedule modal", async () => { - const user = userEvent.setup(); - const changeOriginSpy = jest.spyOn(schedulePage, "handleOriginSelectClick"); - - renderWithProviders( - , - { - preloadedState: { - selectedDirection: 0, - selectedOrigin: "place-welln", - modalMode: "schedule", - modalOpen: true - } - } - ); - const originSelect = await waitFor(() => - screen.getByTestId("schedule-finder-origin-select") - ); - - await user.click(originSelect); - - await waitFor(() => - expect(changeOriginSpy).toHaveBeenCalledWith(expect.any(Function)) - ); - }); - - it("Changes the origin", async () => { - const user = userEvent.setup(); - renderWithProviders( - - ); - - const dispatchSpy = jest.fn(); - jest.spyOn(reactRedux, "useDispatch").mockImplementation(() => { - return dispatchSpy; - }); - const originSelect = screen.getByTestId("schedule-finder-origin-select"); - await user.selectOptions(originSelect, "123"); - - expect(dispatchSpy).toHaveBeenCalledTimes(2); - }); - it("Checks if it is a unidirectional route", () => { const dispatchSpy = jest.fn(); jest.spyOn(reactRedux, "useDispatch").mockImplementation(() => { @@ -959,7 +812,7 @@ describe("SchedulePage", () => { }); describe("changeOrigin", () => { - it("should call the dispatch function twice", () => { + it("should call the dispatch function once", () => { const dispatchSpy = jest.fn(); const testOrigin = "test-origin"; changeOrigin(testOrigin, dispatchSpy); @@ -969,12 +822,6 @@ describe("SchedulePage", () => { selectedOrigin: testOrigin } }); - expect(dispatchSpy).toHaveBeenCalledWith({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "schedule" - } - }); }); }); @@ -993,16 +840,4 @@ describe("SchedulePage", () => { }); }); - describe("handleOriginSelectClick", () => { - it("should call the dispatch function setting the new direction in the state", () => { - const dispatchSpy = jest.fn(); - handleOriginSelectClick(dispatchSpy); - expect(dispatchSpy).toHaveBeenCalledWith({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "origin" - } - }); - }); - }); }); diff --git a/assets/ts/schedule/components/line-diagram/LineDiagram.tsx b/assets/ts/schedule/components/line-diagram/LineDiagram.tsx index bb0b2fcdba..9431959810 100644 --- a/assets/ts/schedule/components/line-diagram/LineDiagram.tsx +++ b/assets/ts/schedule/components/line-diagram/LineDiagram.tsx @@ -1,6 +1,5 @@ import React, { ReactElement, useState } from "react"; import { Provider, useDispatch, useSelector } from "react-redux"; -import { updateInLocation } from "use-query-params"; import { uniqBy } from "lodash"; import SearchBox from "../../../components/SearchBox"; import { stopForId, stopIds } from "../../../helpers/stop-tree"; @@ -31,21 +30,6 @@ interface Props { const stationsOrStops = (routeType: number): string => [0, 1, 2].includes(routeType) ? "Stations" : "Stops"; -const updateURL = (origin: SelectedOrigin, direction?: DirectionId): void => { - /* istanbul ignore else */ - if (window) { - // eslint-disable-next-line camelcase - const newQuery = { - "schedule_finder[direction_id]": - direction !== undefined ? direction.toString() : "", - "schedule_finder[origin]": origin - }; - const newLoc = updateInLocation(newQuery, window.location); - // newLoc is not a true Location, so toString doesn't work - window.history.replaceState({}, "", `${newLoc.pathname}${newLoc.search}`); - } -}; - const LineDiagram = ({ alerts, directionId, @@ -85,28 +69,16 @@ const LineDiagram = ({ selectedOrigin: origin } }); - // reopen modal depending on choice: - dispatch({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: origin ? "schedule" : "origin" - } - }); }; const handleStopClick = (stop: RouteStop): void => { changeOrigin(stop.id); const { modalOpen: modalIsOpen, selectedOrigin } = currentState; - updateURL(stop.id, directionId); - if (selectedOrigin !== undefined && !modalIsOpen) { - dispatch({ - type: "OPEN_MODAL", - newStoreValues: { - modalMode: "schedule" - } - }); + window.location.assign( + `/departures/?route_id=${route.id}&direction_id=${directionId}&stop_id=${stop.id}` + ); } }; diff --git a/assets/ts/schedule/components/line-diagram/__tests__/LineDiagramTest.tsx b/assets/ts/schedule/components/line-diagram/__tests__/LineDiagramTest.tsx index 2c34a4a3ec..e376dd27dd 100644 --- a/assets/ts/schedule/components/line-diagram/__tests__/LineDiagramTest.tsx +++ b/assets/ts/schedule/components/line-diagram/__tests__/LineDiagramTest.tsx @@ -117,32 +117,6 @@ describe("LineDiagram", () => { expect(screen.getByText("Stations")).toBeInTheDocument(); }); - it("should update the URL when the schedule finder modal is opened", async () => { - const updateInLocationSpy = jest.spyOn(UseQueryParams, "updateInLocation"); - const user = userEvent.setup(); - const dispatchSpy = jest.fn(); - jest.spyOn(reactRedux, "useDispatch").mockImplementation(() => { - return dispatchSpy; - }); - renderWithProviders( - - ); - - const scheduleLinks = screen.getAllByText("View departures"); - await user.click(scheduleLinks[0]); - - expect(dispatchSpy).toHaveBeenCalledWith( - expect.objectContaining({ type: "OPEN_MODAL" }) - ); - expect(updateInLocationSpy).toHaveBeenCalled(); - }); - it("should display the No Results card when a user doesn't query a stop", async () => { renderWithProviders( { expect(screen.getByText("a")).toBeDefined(); }); - it("should fire the open modal event when a user clicks on the results stop card", async () => { + it("should go to /departures/ when a user clicks on the results stop card", async () => { const user = userEvent.setup(); - const dispatchSpy = jest.fn(); - jest.spyOn(reactRedux, "useDispatch").mockImplementation(() => dispatchSpy); + const locationSpy = jest.fn(); + Object.defineProperty(window, 'location', { + writable: true, + value: { assign: locationSpy }, + }); renderWithProviders( { await userEvent.click(scheduleButton); await waitFor(() => { - expect(dispatchSpy).toHaveBeenCalledWith({ - type: "OPEN_MODAL", - newStoreValues: { modalMode: "schedule" } - }); + expect(locationSpy).toHaveBeenCalled() }); }); }); diff --git a/assets/ts/schedule/components/schedule-finder/ScheduleFinderForm.tsx b/assets/ts/schedule/components/schedule-finder/ScheduleFinderForm.tsx index 37c6f00661..2b8ae5e9e9 100644 --- a/assets/ts/schedule/components/schedule-finder/ScheduleFinderForm.tsx +++ b/assets/ts/schedule/components/schedule-finder/ScheduleFinderForm.tsx @@ -14,7 +14,6 @@ const validDirections = (directionInfo: DirectionInfo): DirectionId[] => interface Props { onDirectionChange: (direction: DirectionId, dispatch: Dispatch) => void; onOriginChange: (origin: SelectedOrigin, dispatch: Dispatch) => void; - onOriginSelectClick: (dispatch: Dispatch) => void; onSubmit?: () => void; route: Route; selectedDirection: DirectionId; @@ -26,7 +25,6 @@ const ScheduleFinderForm = ({ onDirectionChange, onOriginChange, onSubmit = () => {}, - onOriginSelectClick, route, selectedDirection, selectedOrigin, @@ -41,11 +39,6 @@ const ScheduleFinderForm = ({ const [originError, setOriginError] = useState(false); - const handleOriginClick = (): void => { - setOriginError(false); - onOriginSelectClick(dispatch); - }; - const handleSubmit = (event: FormEvent): void => { event.preventDefault(); @@ -112,15 +105,15 @@ const ScheduleFinderForm = ({