From 9f16e0181de9e2b8029e33621b91565198706eea Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 23 Oct 2024 13:23:43 -0400 Subject: [PATCH 01/11] chore: delete unused `routeTabFactory.createList` --- assets/tests/factories/routeTab.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/tests/factories/routeTab.ts b/assets/tests/factories/routeTab.ts index e66c25b00..b7bfc2c81 100644 --- a/assets/tests/factories/routeTab.ts +++ b/assets/tests/factories/routeTab.ts @@ -23,5 +23,4 @@ const routeTabFactory = defaultRouteTabFactory.params({ presetName: undefined, }) -routeTabFactory.createList export default routeTabFactory From b8e0119f564c1530d1c315b53b1ae1476225f66e Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 23 Oct 2024 13:23:43 -0400 Subject: [PATCH 02/11] chore: export `routeTabFactory` --- assets/tests/factories/routeTab.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/tests/factories/routeTab.ts b/assets/tests/factories/routeTab.ts index b7bfc2c81..b952a7f21 100644 --- a/assets/tests/factories/routeTab.ts +++ b/assets/tests/factories/routeTab.ts @@ -19,7 +19,7 @@ export const routeTabPresetFactory = defaultRouteTabFactory.params({ ordering: undefined, }) -const routeTabFactory = defaultRouteTabFactory.params({ +export const routeTabFactory = defaultRouteTabFactory.params({ presetName: undefined, }) From de6b0e10fe1921b9c1c5c0abc7265e94381ea8da Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Tue, 22 Oct 2024 12:12:00 -0400 Subject: [PATCH 03/11] fix: convert `useSwings` calls to pass function --- assets/tests/hooks/useSwings.test.tsx | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index 60e400a9d..cd97a73fd 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -18,9 +18,7 @@ jest.mock("../../src/api", () => ({ describe("useSwings", () => { test("returns null while loading", () => { const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock - const { result } = renderHook(() => { - return useSwings() - }) + const { result } = renderHook(useSwings) expect(mockFetchSwings).toHaveBeenCalledTimes(1) expect(result.current).toEqual(null) }) @@ -49,9 +47,7 @@ describe("useSwings", () => { const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock mockFetchSwings.mockImplementationOnce(() => instantPromise(swings)) const { result } = renderHook( - () => { - return useSwings() - }, + useSwings, { wrapper: ({ children }) => ( { test("doesn't refetch swings on every render", () => { const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock const { rerender } = renderHook( - () => { - useSwings() - }, + useSwings, { wrapper: ({ children }) => ( { }), ] const { rerender } = renderHook( - () => { - useSwings() - }, + useSwings, { wrapper: ({ children }) => ( {children} @@ -138,9 +130,7 @@ describe("useSwings", () => { let routeTabs = [routeTabFactory.build({ selectedRouteIds: ["1"] })] const { rerender } = renderHook( - () => { - useSwings() - }, + useSwings, { wrapper: ({ children }) => ( {children} From c72087b143ada8b2de9b32ca4ddbe538df0c5b95 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 30 Oct 2024 12:04:07 -0400 Subject: [PATCH 04/11] chore: format `useSwings` --- assets/tests/hooks/useSwings.test.tsx | 98 ++++++++++++--------------- 1 file changed, 43 insertions(+), 55 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index cd97a73fd..240781848 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -46,33 +46,30 @@ describe("useSwings", () => { ] const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock mockFetchSwings.mockImplementationOnce(() => instantPromise(swings)) - const { result } = renderHook( - useSwings, - { - wrapper: ({ children }) => ( - - {children} - - ), - } - ) + const { result } = renderHook(useSwings, { + wrapper: ({ children }) => ( + + {children} + + ), + }) expect(mockFetchSwings).toHaveBeenCalledWith(["1", "2"]) expect(result.current).toEqual(swings) @@ -80,18 +77,15 @@ describe("useSwings", () => { test("doesn't refetch swings on every render", () => { const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock - const { rerender } = renderHook( - useSwings, - { - wrapper: ({ children }) => ( - - {children} - - ), - } - ) + const { rerender } = renderHook(useSwings, { + wrapper: ({ children }) => ( + + {children} + + ), + }) rerender() expect(mockFetchSwings).toHaveBeenCalledTimes(1) }) @@ -109,14 +103,11 @@ describe("useSwings", () => { isCurrentTab: false, }), ] - const { rerender } = renderHook( - useSwings, - { - wrapper: ({ children }) => ( - {children} - ), - } - ) + const { rerender } = renderHook(useSwings, { + wrapper: ({ children }) => ( + {children} + ), + }) routeTabs = [ routeTabFactory.build({ selectedRouteIds: ["1"], isCurrentTab: false }), routeTabFactory.build({ selectedRouteIds: ["2"], isCurrentTab: true }), @@ -129,14 +120,11 @@ describe("useSwings", () => { const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock let routeTabs = [routeTabFactory.build({ selectedRouteIds: ["1"] })] - const { rerender } = renderHook( - useSwings, - { - wrapper: ({ children }) => ( - {children} - ), - } - ) + const { rerender } = renderHook(useSwings, { + wrapper: ({ children }) => ( + {children} + ), + }) routeTabs = [routeTabFactory.build({ selectedRouteIds: ["2"] })] rerender() expect(mockFetchSwings).toHaveBeenCalledTimes(2) From 62a237370847d4b67c9a88a65ffd1f31f6716d8a Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Tue, 22 Oct 2024 12:12:00 -0400 Subject: [PATCH 05/11] chore: replace `jest.mock` with `beforeEach` --- assets/tests/hooks/useSwings.test.tsx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index 240781848..26d2d317d 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -1,4 +1,4 @@ -import { jest, describe, test, expect } from "@jest/globals" +import { jest, describe, test, expect, beforeEach } from "@jest/globals" import { renderHook } from "@testing-library/react" import React, { ReactNode } from "react" import * as Api from "../../src/api" @@ -8,12 +8,13 @@ import { initialState } from "../../src/state" import { StateDispatchProvider } from "../../src/contexts/stateDispatchContext" import { RouteTab } from "../../src/models/routeTab" import routeTabFactory from "../factories/routeTab" +import { neverPromise } from "../testHelpers/mockHelpers" -jest.mock("../../src/api", () => ({ - __esModule: true, +jest.mock("../../src/api") - fetchSwings: jest.fn(() => new Promise(() => {})), -})) +beforeEach(() => { + jest.mocked(Api.fetchSwings).mockReturnValue(neverPromise()) +}) describe("useSwings", () => { test("returns null while loading", () => { From 6b7b5466324e10e684ee1411dd9f58576cd2272d Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 30 Oct 2024 12:28:04 -0400 Subject: [PATCH 06/11] chore: replace swings with factory --- assets/tests/hooks/useSwings.test.tsx | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index 26d2d317d..c2fb810a4 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -9,6 +9,7 @@ import { StateDispatchProvider } from "../../src/contexts/stateDispatchContext" import { RouteTab } from "../../src/models/routeTab" import routeTabFactory from "../factories/routeTab" import { neverPromise } from "../testHelpers/mockHelpers" +import { swingFactory } from "../factories/swing" jest.mock("../../src/api") @@ -25,28 +26,10 @@ describe("useSwings", () => { }) test("returns result when loaded", () => { - const swings = [ - { - from_route_id: "1", - from_run_id: "123-456", - from_trip_id: "1234", - to_route_id: "1", - to_run_id: "123-789", - to_trip_id: "5678", - time: 100, - }, - { - from_route_id: "2", - from_run_id: "124-456", - from_trip_id: "4321", - to_route_id: "2", - to_run_id: "124-789", - to_trip_id: "8765", - time: 100, - }, - ] + const swings = swingFactory.buildList(2) const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock mockFetchSwings.mockImplementationOnce(() => instantPromise(swings)) + const { result } = renderHook(useSwings, { wrapper: ({ children }) => ( Date: Wed, 30 Oct 2024 12:25:24 -0400 Subject: [PATCH 07/11] chore: use `jest.mocked` --- assets/tests/hooks/useSwings.test.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index c2fb810a4..8735e26c4 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -19,7 +19,7 @@ beforeEach(() => { describe("useSwings", () => { test("returns null while loading", () => { - const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock + const mockFetchSwings = jest.mocked(Api.fetchSwings) const { result } = renderHook(useSwings) expect(mockFetchSwings).toHaveBeenCalledTimes(1) expect(result.current).toEqual(null) @@ -27,7 +27,7 @@ describe("useSwings", () => { test("returns result when loaded", () => { const swings = swingFactory.buildList(2) - const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock + const mockFetchSwings = jest.mocked(Api.fetchSwings) mockFetchSwings.mockImplementationOnce(() => instantPromise(swings)) const { result } = renderHook(useSwings, { @@ -60,7 +60,7 @@ describe("useSwings", () => { }) test("doesn't refetch swings on every render", () => { - const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock + const mockFetchSwings = jest.mocked(Api.fetchSwings) const { rerender } = renderHook(useSwings, { wrapper: ({ children }) => ( { }) test("doesn't refetch swings when route Ids don't change", () => { - const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock + const mockFetchSwings = jest.mocked(Api.fetchSwings) let routeTabs = [ routeTabFactory.build({ @@ -101,7 +101,7 @@ describe("useSwings", () => { }) test("does refetch swings when selected routes change", () => { - const mockFetchSwings: jest.Mock = Api.fetchSwings as jest.Mock + const mockFetchSwings = jest.mocked(Api.fetchSwings) let routeTabs = [routeTabFactory.build({ selectedRouteIds: ["1"] })] const { rerender } = renderHook(useSwings, { From 556f7039ad2bc73cd28a3f96fc62268ade8d51d5 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Wed, 30 Oct 2024 12:27:05 -0400 Subject: [PATCH 08/11] chore: inline `jest.mocked` variables --- assets/tests/hooks/useSwings.test.tsx | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index 8735e26c4..7d7374235 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -19,16 +19,16 @@ beforeEach(() => { describe("useSwings", () => { test("returns null while loading", () => { - const mockFetchSwings = jest.mocked(Api.fetchSwings) const { result } = renderHook(useSwings) - expect(mockFetchSwings).toHaveBeenCalledTimes(1) + expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledTimes(1) expect(result.current).toEqual(null) }) test("returns result when loaded", () => { const swings = swingFactory.buildList(2) - const mockFetchSwings = jest.mocked(Api.fetchSwings) - mockFetchSwings.mockImplementationOnce(() => instantPromise(swings)) + jest + .mocked(Api.fetchSwings) + .mockImplementationOnce(() => instantPromise(swings)) const { result } = renderHook(useSwings, { wrapper: ({ children }) => ( @@ -55,12 +55,11 @@ describe("useSwings", () => { ), }) - expect(mockFetchSwings).toHaveBeenCalledWith(["1", "2"]) + expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledWith(["1", "2"]) expect(result.current).toEqual(swings) }) test("doesn't refetch swings on every render", () => { - const mockFetchSwings = jest.mocked(Api.fetchSwings) const { rerender } = renderHook(useSwings, { wrapper: ({ children }) => ( { ), }) rerender() - expect(mockFetchSwings).toHaveBeenCalledTimes(1) + expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledTimes(1) }) test("doesn't refetch swings when route Ids don't change", () => { - const mockFetchSwings = jest.mocked(Api.fetchSwings) - let routeTabs = [ routeTabFactory.build({ selectedRouteIds: ["1"], @@ -97,12 +94,10 @@ describe("useSwings", () => { routeTabFactory.build({ selectedRouteIds: ["2"], isCurrentTab: true }), ] rerender() - expect(mockFetchSwings).toHaveBeenCalledTimes(1) + expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledTimes(1) }) test("does refetch swings when selected routes change", () => { - const mockFetchSwings = jest.mocked(Api.fetchSwings) - let routeTabs = [routeTabFactory.build({ selectedRouteIds: ["1"] })] const { rerender } = renderHook(useSwings, { wrapper: ({ children }) => ( @@ -111,7 +106,7 @@ describe("useSwings", () => { }) routeTabs = [routeTabFactory.build({ selectedRouteIds: ["2"] })] rerender() - expect(mockFetchSwings).toHaveBeenCalledTimes(2) + expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledTimes(2) }) }) From 4324b629232e0876b3c085d847e25313150fea21 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Tue, 22 Oct 2024 12:12:00 -0400 Subject: [PATCH 09/11] chore: remove `instantPromise` --- assets/tests/hooks/useSwings.test.tsx | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index 7d7374235..615a0cd41 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -1,9 +1,8 @@ import { jest, describe, test, expect, beforeEach } from "@jest/globals" -import { renderHook } from "@testing-library/react" +import { renderHook, waitFor } from "@testing-library/react" import React, { ReactNode } from "react" import * as Api from "../../src/api" import useSwings from "../../src/hooks/useSwings" -import { instantPromise } from "../testHelpers/mockHelpers" import { initialState } from "../../src/state" import { StateDispatchProvider } from "../../src/contexts/stateDispatchContext" import { RouteTab } from "../../src/models/routeTab" @@ -24,11 +23,9 @@ describe("useSwings", () => { expect(result.current).toEqual(null) }) - test("returns result when loaded", () => { + test("returns result when loaded", async () => { const swings = swingFactory.buildList(2) - jest - .mocked(Api.fetchSwings) - .mockImplementationOnce(() => instantPromise(swings)) + jest.mocked(Api.fetchSwings).mockResolvedValue(swings) const { result } = renderHook(useSwings, { wrapper: ({ children }) => ( @@ -55,8 +52,10 @@ describe("useSwings", () => { ), }) - expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledWith(["1", "2"]) - expect(result.current).toEqual(swings) + await waitFor(() => { + expect(jest.mocked(Api.fetchSwings)).toHaveBeenCalledWith(["1", "2"]) + expect(result.current).toEqual(swings) + }) }) test("doesn't refetch swings on every render", () => { From ae70ddd3f26862cceb2f45e02fd2837cd1a8fa40 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Tue, 22 Oct 2024 12:12:00 -0400 Subject: [PATCH 10/11] chore: replace `routeTabFactory` with `ordering: undefined` with `routeTabPresetFactory` --- assets/tests/hooks/useSwings.test.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/assets/tests/hooks/useSwings.test.tsx b/assets/tests/hooks/useSwings.test.tsx index 615a0cd41..0d002b685 100644 --- a/assets/tests/hooks/useSwings.test.tsx +++ b/assets/tests/hooks/useSwings.test.tsx @@ -6,7 +6,7 @@ import useSwings from "../../src/hooks/useSwings" import { initialState } from "../../src/state" import { StateDispatchProvider } from "../../src/contexts/stateDispatchContext" import { RouteTab } from "../../src/models/routeTab" -import routeTabFactory from "../factories/routeTab" +import routeTabFactory, { routeTabPresetFactory } from "../factories/routeTab" import { neverPromise } from "../testHelpers/mockHelpers" import { swingFactory } from "../factories/swing" @@ -41,8 +41,7 @@ describe("useSwings", () => { isCurrentTab: false, selectedRouteIds: ["2"], }), - routeTabFactory.build({ - ordering: undefined, + routeTabPresetFactory.build({ selectedRouteIds: ["3"], }), ]} From 2d6df467b938d55f5e1b741d6c2515d63c621dc0 Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Tue, 22 Oct 2024 12:12:00 -0400 Subject: [PATCH 11/11] 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) })