diff --git a/assets/src/hooks/useDetour.ts b/assets/src/hooks/useDetour.ts index 998ead094..5c82d2256 100644 --- a/assets/src/hooks/useDetour.ts +++ b/assets/src/hooks/useDetour.ts @@ -5,8 +5,7 @@ import { FinishedDetour, OriginalRoute } from "../models/detour" import { useApiCall } from "./useApiCall" import { Ok, isErr, isOk } from "../util/result" -import { isOk as FetchIsOk } from "../util/fetchResult" -import { useNearestIntersectionFetchResult } from "./useNearestIntersection" +import { useNearestIntersection } from "./useNearestIntersection" const useDetourDirections = (shapePoints: ShapePoint[]) => useApiCall({ @@ -56,14 +55,10 @@ export const useDetour = ({ routePatternId, shape }: OriginalRoute) => { } }, [routePatternId, startPoint, endPoint]) - const nearestIntersectionResult = useNearestIntersectionFetchResult( - startPoint?.lat, - startPoint?.lon - ) - - const nearestIntersection = FetchIsOk(nearestIntersectionResult) - ? nearestIntersectionResult.ok - : null + const { result: nearestIntersection } = useNearestIntersection({ + latitude: startPoint?.lat, + longitude: startPoint?.lon, + }) const detourShape = useDetourDirections( useMemo( diff --git a/assets/src/hooks/useNearestIntersection.ts b/assets/src/hooks/useNearestIntersection.ts index b1e36233d..54bd23af4 100644 --- a/assets/src/hooks/useNearestIntersection.ts +++ b/assets/src/hooks/useNearestIntersection.ts @@ -30,7 +30,7 @@ export const useNearestIntersectionFetchResult = ( } } -const useNearestIntersection = ({ +export const useNearestIntersection = ({ latitude, longitude, }: { diff --git a/assets/tests/components/detours/diversionPage.test.tsx b/assets/tests/components/detours/diversionPage.test.tsx index 7903a51cc..c1a6242a9 100644 --- a/assets/tests/components/detours/diversionPage.test.tsx +++ b/assets/tests/components/detours/diversionPage.test.tsx @@ -13,6 +13,7 @@ import { FetchDetourDirectionsError, fetchDetourDirections, fetchFinishedDetour, + fetchNearestIntersection, } from "../../../src/api" import { DiversionPage as DiversionPageDefault } from "../../../src/components/detours/diversionPage" import shapeFactory from "../../factories/shape" @@ -33,8 +34,7 @@ import { stopIcon, } from "../../testHelpers/selectors/components/map/markers/stopIcon" import { Err, Ok } from "../../../src/util/result" -import { useNearestIntersectionFetchResult } from "../../../src/hooks/useNearestIntersection" -import { loading, ok } from "../../../src/util/fetchResult" +import { neverPromise } from "../../testHelpers/mockHelpers" const DiversionPage = ( props: Omit< @@ -72,14 +72,10 @@ beforeEach(() => { jest.mock("../../../src/api") -jest.mock("../../../src/hooks/useNearestIntersection") - beforeEach(() => { - jest - .mocked(fetchDetourDirections) - .mockImplementation(() => new Promise(() => {})) - jest.mocked(fetchFinishedDetour).mockResolvedValue(null) - jest.mocked(useNearestIntersectionFetchResult).mockReturnValue(loading()) + jest.mocked(fetchDetourDirections).mockReturnValue(neverPromise()) + jest.mocked(fetchFinishedDetour).mockReturnValue(neverPromise()) + jest.mocked(fetchNearestIntersection).mockReturnValue(neverPromise()) }) describe("DiversionPage", () => { @@ -108,9 +104,7 @@ describe("DiversionPage", () => { ) const intersection = "Avenue 1 & Street 2" - jest.mocked(useNearestIntersectionFetchResult).mockReturnValue({ - ok: intersection, - }) + jest.mocked(fetchNearestIntersection).mockResolvedValue(intersection) const { container } = render() @@ -523,9 +517,7 @@ describe("DiversionPage", () => { }) const intersection = "Avenue 1 & Street 2" - jest - .mocked(useNearestIntersectionFetchResult) - .mockReturnValue(ok(intersection)) + jest.mocked(fetchNearestIntersection).mockResolvedValue(intersection) userEvent.setup() // Configure the clipboard API diff --git a/assets/tests/components/mapPage.test.tsx b/assets/tests/components/mapPage.test.tsx index 61ccd92f4..b9330a8f0 100644 --- a/assets/tests/components/mapPage.test.tsx +++ b/assets/tests/components/mapPage.test.tsx @@ -63,6 +63,7 @@ import { mockScreenSize, mockTileUrls, mockUsePatternsByIdForVehicles, + neverPromise, } from "../testHelpers/mockHelpers" import usePatternsByIdForRoute from "../../src/hooks/usePatternsByIdForRoute" import { routePatternFactory } from "../factories/routePattern" @@ -92,8 +93,7 @@ import pieceFactory from "../factories/piece" import { mockUsePanelState } from "../testHelpers/usePanelStateMocks" import getTestGroups from "../../src/userTestGroups" import { TestGroups } from "../../src/userInTestGroup" -import { useNearestIntersectionFetchResult } from "../../src/hooks/useNearestIntersection" -import { loading } from "../../src/util/fetchResult" +import { fetchNearestIntersection } from "../../src/api" jest.mock("../../src/hooks/useLocationSearchResults", () => ({ useLocationSearchResults: jest.fn(() => null), @@ -113,8 +113,6 @@ jest.mock("../../src/hooks/usePatternsByIdForRoute", () => ({ default: jest.fn(() => null), })) -jest.mock("../../src/hooks/useNearestIntersection") - jest.mock("../../src/hooks/useVehicleForId", () => ({ __esModule: true, default: jest.fn(() => null), @@ -150,8 +148,10 @@ jest.mock("../../src/hooks/usePanelState") jest.mock("../../src/userTestGroups") +jest.mock("../../src/api") + beforeEach(() => { - jest.mocked(useNearestIntersectionFetchResult).mockReturnValue(loading()) + jest.mocked(fetchNearestIntersection).mockReturnValue(neverPromise()) }) const mockVehicleSearchResultsCategory = (