From adcbb1d6fad974de40e359f2bdb415e64c521ccc Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Tue, 22 Oct 2024 12:12:00 -0400 Subject: [PATCH] feat: make swings apply to current route tab --- assets/src/hooks/useSwings.ts | 7 ++++--- assets/tests/hooks/useSwings.test.tsx | 14 +++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/assets/src/hooks/useSwings.ts b/assets/src/hooks/useSwings.ts index d5cdf9beb..7ab520562 100644 --- a/assets/src/hooks/useSwings.ts +++ b/assets/src/hooks/useSwings.ts @@ -12,14 +12,15 @@ const useSwings = (): Swing[] | null => { allOpenRouteIds(routeTabs) ) - const newRouteIds = allOpenRouteIds(routeTabs) + const newRouteIds = + routeTabs.find((v) => v.isCurrentTab)?.selectedRouteIds ?? [] - if (!equalByElements(routeIds, newRouteIds)) { + if (newRouteIds && !equalByElements(routeIds, newRouteIds)) { setRouteIds(newRouteIds) } useEffect(() => { - fetchSwings(routeIds).then(setSwings) + newRouteIds && fetchSwings(routeIds).then(setSwings) }, [routeIds]) return swings diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index 0d002b685..d4a3f160c 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -52,7 +52,7 @@ describe("useSwings", () => { }) await waitFor(() => { - expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledWith(["1", "2"]) + expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledWith(["1"]) expect(result.current).toEqual(swings) }) }) @@ -78,7 +78,7 @@ describe("useSwings", () => { isCurrentTab: true, }), routeTabFactory.build({ - selectedRouteIds: ["2"], + selectedRouteIds: ["1"], isCurrentTab: false, }), ] @@ -89,20 +89,24 @@ describe("useSwings", () => { }) routeTabs = [ routeTabFactory.build({ selectedRouteIds: ["1"], isCurrentTab: false }), - routeTabFactory.build({ selectedRouteIds: ["2"], isCurrentTab: true }), + routeTabFactory.build({ selectedRouteIds: ["1"], isCurrentTab: true }), ] rerender() expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledTimes(1) }) test("does refetch swings when selected routes change", () => { - let routeTabs = [routeTabFactory.build({ selectedRouteIds: ["1"] })] + let routeTabs = [ + routeTabFactory.build({ selectedRouteIds: ["1"], isCurrentTab: true }), + ] const { rerender } = renderHook(useSwings, { wrapper: ({ children }) => ( {children} ), }) - routeTabs = [routeTabFactory.build({ selectedRouteIds: ["2"] })] + routeTabs = [ + routeTabFactory.build({ selectedRouteIds: ["2"], isCurrentTab: true }), + ] rerender() expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledTimes(2) })