Skip to content

Commit

Permalink
refactor: Have useDetour use the new useNearestIntersection (#2579)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson authored May 8, 2024
1 parent 8f8bbce commit d26f862
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
15 changes: 5 additions & 10 deletions assets/src/hooks/useDetour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion assets/src/hooks/useNearestIntersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useNearestIntersectionFetchResult = (
}
}

const useNearestIntersection = ({
export const useNearestIntersection = ({
latitude,
longitude,
}: {
Expand Down
22 changes: 7 additions & 15 deletions assets/tests/components/detours/diversionPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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(<DiversionPage />)

Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions assets/tests/components/mapPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
mockScreenSize,
mockTileUrls,
mockUsePatternsByIdForVehicles,
neverPromise,
} from "../testHelpers/mockHelpers"
import usePatternsByIdForRoute from "../../src/hooks/usePatternsByIdForRoute"
import { routePatternFactory } from "../factories/routePattern"
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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 = (
Expand Down

0 comments on commit d26f862

Please sign in to comment.