From 2d6df467b938d55f5e1b741d6c2515d63c621dc0 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 | 4 ++-- assets/tests/hooks/useSwings.test.tsx | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/assets/src/hooks/useSwings.ts b/assets/src/hooks/useSwings.ts index d5cdf9beb..25c3f1712 100644 --- a/assets/src/hooks/useSwings.ts +++ b/assets/src/hooks/useSwings.ts @@ -12,9 +12,9 @@ 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) } 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) })