Skip to content

Commit

Permalink
feat(StopPageDepartures): omit noncanon headsign
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen committed Nov 9, 2023
1 parent d2ba1af commit 3eb9895
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 10 deletions.
16 changes: 14 additions & 2 deletions apps/site/assets/ts/models/route-patterns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { GroupedRoutePatterns } from "../stop/stop-redesign-loader";
import {
GroupedRoutePatterns,
RoutePatternWithPolyline
} from "../stop/stop-redesign-loader";
import { DepartureInfo } from "./departureInfo";

type RoutePatternGroup = GroupedRoutePatterns[keyof GroupedRoutePatterns];
export type RoutePatternGroupEntries = [
Expand All @@ -21,5 +25,13 @@ const sortedGroupedRoutePatterns = (
});
};

const isNoncanonicalAndNoDepartures = (
routePatterns: RoutePatternWithPolyline[],
departures: DepartureInfo[]
): boolean => {
const isNonCanonical = !routePatterns.find(rp => !!rp.canonical);
return isNonCanonical && departures.length === 0;
};

// eslint-disable-next-line import/prefer-default-export
export { sortedGroupedRoutePatterns };
export { sortedGroupedRoutePatterns, isNoncanonicalAndNoDepartures };
52 changes: 52 additions & 0 deletions apps/site/assets/ts/stop/__tests__/StopPageDeparturesTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { screen, waitFor } from "@testing-library/react";
import StopPageDepartures from "../components/StopPageDepartures";
import { Route } from "../../__v3api";
import { TEST_LOADER_VALUE, baseRoute, renderWithRouter } from "./helpers";
import { cloneDeep } from "lodash";

const routeData: Route[] = [baseRoute("16", 3), baseRoute("Red", 1)];

Expand Down Expand Up @@ -57,4 +58,55 @@ describe("StopPageDepartures", () => {
).toBeNull();
});
});

it("doesn't show headsigns for uncommon subway route patterns with no upcoming service", async () => {
const subwayRoute = baseRoute("Red", 1);
const withUncommonRoutePattern = cloneDeep(TEST_LOADER_VALUE);
// give Ashmont non-canonical route patterns
withUncommonRoutePattern["Red"][
"Ashmont"
].route_patterns = withUncommonRoutePattern["Red"][
"Ashmont"
].route_patterns.map(rp => ({ ...rp, canonical: false }));
renderWithRouter(
<StopPageDepartures
routes={[subwayRoute]}
alerts={[]}
departureInfos={[]}
groupedRoutePatterns={withUncommonRoutePattern}
/>
);
await waitFor(() => {
expect(screen.queryByText("Braintree")).toBeTruthy();
expect(screen.queryByText("Alewife")).toBeTruthy();
expect(screen.queryByText("Ashmont")).toBeFalsy();
});
});

it("doesn't show departure card when all headsigns for uncommon subway route patterns have no upcoming service", async () => {
const subwayRoute = baseRoute("Red", 1);
const withUncommonRoutePattern = cloneDeep(TEST_LOADER_VALUE);
["Braintree", "Alewife", "Ashmont"].forEach(headsign => {
withUncommonRoutePattern["Red"][
`${headsign}`
].route_patterns = withUncommonRoutePattern["Red"][
`${headsign}`
].route_patterns.map(rp => ({ ...rp, canonical: false }));
});

renderWithRouter(
<StopPageDepartures
routes={[subwayRoute]}
alerts={[]}
departureInfos={[]}
groupedRoutePatterns={withUncommonRoutePattern}
/>
);
await waitFor(() => {
expect(screen.queryByText("Braintree")).toBeFalsy();
expect(screen.queryByText("Alewife")).toBeFalsy();
expect(screen.queryByText("Ashmont")).toBeFalsy();
expect(screen.queryByText("Red Line")).toBeFalsy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const renderWithAct = (children: React.ReactElement) =>
renderWithRouter(children);
});

const testRoutes = Object.keys(TEST_LOADER_VALUE).map(id => baseRoute(id, 1));
const testRoutes = Object.keys(TEST_LOADER_VALUE).map(id => baseRoute(id, 3));

describe("StopPageRedesign", () => {
beforeEach(() => {
Expand Down
39 changes: 32 additions & 7 deletions apps/site/assets/ts/stop/components/StopPageDepartures.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { filter, groupBy, sortBy } from "lodash";
import { filter, groupBy, reject, sortBy } from "lodash";
import React, { ReactElement, useState } from "react";
import { Alert, Route } from "../../__v3api";
import DeparturesFilters, { ModeChoice } from "./DeparturesFilters";
import { isRailReplacementBus, modeForRoute } from "../../models/route";
import {
isRailReplacementBus,
isSubwayRoute,
modeForRoute
} from "../../models/route";
import DepartureCard from "./DepartureCard";
import { alertsByRoute, isInNextXDays } from "../../models/alert";
import { DepartureInfo } from "../../models/departureInfo";
import { GroupedRoutePatterns } from "../stop-redesign-loader";
import { sortedGroupedRoutePatterns } from "../../models/route-patterns";
import {
isNoncanonicalAndNoDepartures,
sortedGroupedRoutePatterns
} from "../../models/route-patterns";
import { departureInfoInRoutePatterns } from "../../helpers/departureInfo";

interface StopPageDeparturesProps {
routes: Route[];
Expand Down Expand Up @@ -56,18 +64,35 @@ const StopPageDepartures = ({
<ul className="stop-departures list-unstyled">
{sortBy(filteredRoutes, [modeSortFn, "sort_order"]).map(route => {
const groupedByHeadsign = groupedRoutePatterns[route.id];
const sortedRoutePatternsByHeadsign = sortedGroupedRoutePatterns(
let sortedRoutePatternsByHeadsign = sortedGroupedRoutePatterns(
groupedByHeadsign
);
const departuresForRoute = departureInfos.filter(
d => d.route.id === route.id
);
if (isSubwayRoute(route)) {
// remove certain headsigns based on route pattern and departures
sortedRoutePatternsByHeadsign = reject(
sortedRoutePatternsByHeadsign,
entry => {
const [, { route_patterns: routePatterns }] = entry;
const departures = departuresForRoute.filter(d =>
departureInfoInRoutePatterns(d, routePatterns)
);
return isNoncanonicalAndNoDepartures(routePatterns, departures);
}
);
// don't render a route card if there's no headsigns left to show
if (sortedRoutePatternsByHeadsign.length === 0) return null;
}

return (
<DepartureCard
key={route.id}
route={route}
routePatternsByHeadsign={sortedRoutePatternsByHeadsign}
alertsForRoute={groupedAlerts[route.id] || []}
departuresForRoute={departureInfos.filter(
d => d.route.id === route.id
)}
departuresForRoute={departuresForRoute}
/>
);
})}
Expand Down

0 comments on commit 3eb9895

Please sign in to comment.