Skip to content

Commit

Permalink
feat: make swings apply to current route tab
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Oct 30, 2024
1 parent ae70ddd commit adcbb1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 4 additions & 3 deletions assets/src/hooks/useSwings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions assets/tests/hooks/useSwings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand All @@ -78,7 +78,7 @@ describe("useSwings", () => {
isCurrentTab: true,
}),
routeTabFactory.build({
selectedRouteIds: ["2"],
selectedRouteIds: ["1"],
isCurrentTab: false,
}),
]
Expand All @@ -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 }) => (
<Wrapper routeTabs={routeTabs}>{children}</Wrapper>
),
})
routeTabs = [routeTabFactory.build({ selectedRouteIds: ["2"] })]
routeTabs = [
routeTabFactory.build({ selectedRouteIds: ["2"], isCurrentTab: true }),
]
rerender()
expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledTimes(2)
})
Expand Down

0 comments on commit adcbb1d

Please sign in to comment.