From dd2fde30b9cd5f5984ed82a7be27d4582bef6c7e Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 21 Jan 2025 15:22:56 -0800 Subject: [PATCH] do the same for mock routes --- test/generators.ts | 12 +++++++- ...UnoptimizedInMemoryRepositoryTests.test.ts | 29 +++++++++---------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/test/generators.ts b/test/generators.ts index 9d323b1..fb80ae3 100644 --- a/test/generators.ts +++ b/test/generators.ts @@ -1,5 +1,7 @@ -import { IShuttle, ISystem } from "../src/entities/entities"; +import { IRoute, IShuttle, ISystem } from "../src/entities/entities"; +// Use a single set of generators in case any of the +// interfaces change in the future export function generateMockSystems(): ISystem[] { return [ @@ -15,4 +17,12 @@ export function generateMockShuttles(): IShuttle[] { { id: "sh2", name: "Shuttle B", routeId: "r2", systemId: "sys2", coordinates: { latitude: 15, longitude: 25 } }, { id: "sh3", name: "Shuttle C", routeId: "r3", systemId: "sys3", coordinates: { latitude: 30, longitude: 40 } }, ]; +} + +export function generateMockRoutes(): IRoute[] { + return [ + { id: "r1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] }, + { id: "r2", name: "Route 2", color: "blue", systemId: "sys2", polylineCoordinates: [] }, + { id: "r3", name: "Route 3", color: "green", systemId: "sys3", polylineCoordinates: [] }, + ]; } \ No newline at end of file diff --git a/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts b/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts index 0cb20d8..3da1e9f 100644 --- a/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts +++ b/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts @@ -1,6 +1,6 @@ import { beforeEach, describe, expect, test } from "@jest/globals"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; -import { generateMockShuttles, generateMockSystems } from "../generators"; +import { generateMockRoutes, generateMockShuttles, generateMockSystems } from "../generators"; // For repositories created in the future, reuse core testing // logic from here and differentiate setup (e.g. creating mocks) @@ -93,11 +93,7 @@ describe("UnoptimizedInMemoryRepository", () => { describe("getRoutesBySystemId", () => { test("gets all routes for a specific system ID", async () => { - const mockRoutes = [ - { id: "r1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] }, - { id: "r2", name: "Route 2", color: "blue", systemId: "sys1", polylineCoordinates: [] }, - { id: "r3", name: "Route 3", color: "green", systemId: "sys2", polylineCoordinates: [] }, - ]; + const mockRoutes = generateMockRoutes(); for (const route of mockRoutes) { await repository.addOrUpdateRoute(route); } @@ -114,7 +110,8 @@ describe("UnoptimizedInMemoryRepository", () => { describe("getRouteById", () => { test("gets a route by ID if it exists", async () => { - const mockRoute = { id: "r1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] }; + const mockRoutes = generateMockRoutes(); + const mockRoute = mockRoutes[0]; await repository.addOrUpdateRoute(mockRoute); const result = await repository.getRouteById("r1"); @@ -322,7 +319,8 @@ describe("UnoptimizedInMemoryRepository", () => { describe("addOrUpdateRoute", () => { test("adds a new route if nonexistent", async () => { - const newRoute = { id: "route1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] }; + const mockRoutes = generateMockRoutes(); + const newRoute = mockRoutes[0]; await repository.addOrUpdateRoute(newRoute); @@ -331,8 +329,10 @@ describe("UnoptimizedInMemoryRepository", () => { }); test("updates an existing route if it exists", async () => { - const existingRoute = { id: "route1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] }; - const updatedRoute = { id: "route1", name: "Updated Route 1", color: "blue", systemId: "sys1", polylineCoordinates: [] }; + const mockRoutes = generateMockRoutes(); + const existingRoute = mockRoutes[0]; + const updatedRoute = structuredClone(existingRoute); + updatedRoute.name = "Updated Route"; await repository.addOrUpdateRoute(existingRoute); await repository.addOrUpdateRoute(updatedRoute); @@ -504,11 +504,10 @@ describe("UnoptimizedInMemoryRepository", () => { describe("clearRouteData", () => { test("clears all routes from the repository", async () => { - const route1 = { id: "route1", name: "Route 1", systemId: "sys1", color: "#ffffff", polylineCoordinates: [] }; - const route2 = { id: "route2", name: "Route 2", systemId: "sys2", color: "#ffffff", polylineCoordinates: [] }; - - await repository.addOrUpdateRoute(route1); - await repository.addOrUpdateRoute(route2); + const mockRoutes = generateMockRoutes(); + for (const route of mockRoutes) { + await repository.addOrUpdateRoute(route); + } await repository.clearRouteData();