diff --git a/test/generators.ts b/test/generators.ts index fb80ae3..dffaf3b 100644 --- a/test/generators.ts +++ b/test/generators.ts @@ -1,4 +1,4 @@ -import { IRoute, IShuttle, ISystem } from "../src/entities/entities"; +import { IRoute, IShuttle, IStop, ISystem } from "../src/entities/entities"; // Use a single set of generators in case any of the // interfaces change in the future @@ -25,4 +25,12 @@ export function generateMockRoutes(): IRoute[] { { id: "r2", name: "Route 2", color: "blue", systemId: "sys2", polylineCoordinates: [] }, { id: "r3", name: "Route 3", color: "green", systemId: "sys3", polylineCoordinates: [] }, ]; +} + +export function generateMockStops(): IStop[] { + return [ + { id: "st1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } }, + { id: "st2", name: "Stop B", systemId: "sys2", coordinates: { latitude: 15, longitude: 25 } }, + { id: "st3", name: "Stop C", systemId: "sys3", coordinates: { latitude: 30, longitude: 40 } }, + ]; } \ No newline at end of file diff --git a/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts b/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts index 3da1e9f..eff0fbd 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 { generateMockRoutes, generateMockShuttles, generateMockSystems } from "../generators"; +import { generateMockRoutes, generateMockShuttles, generateMockStops, generateMockSystems } from "../generators"; // For repositories created in the future, reuse core testing // logic from here and differentiate setup (e.g. creating mocks) @@ -57,11 +57,7 @@ describe("UnoptimizedInMemoryRepository", () => { describe("getStopsBySystemId", () => { test("gets stops by system ID", async () => { - const mockStops = [ - { id: "1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } }, - { id: "2", name: "Stop B", systemId: "sys1", coordinates: { latitude: 15, longitude: 25 } }, - { id: "3", name: "Stop C", systemId: "sys2", coordinates: { latitude: 30, longitude: 40 } }, - ]; + const mockStops = generateMockStops(); for (const stop of mockStops) { await repository.addOrUpdateStop(stop); } @@ -78,7 +74,8 @@ describe("UnoptimizedInMemoryRepository", () => { describe("getStopById", () => { test("gets a stop by ID if it exists", async () => { - const mockStop = { id: "st1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } }; + const mockStops = generateMockStops(); + const mockStop = mockStops[0]; await repository.addOrUpdateStop(mockStop); const result = await repository.getStopById("st1"); @@ -369,7 +366,8 @@ describe("UnoptimizedInMemoryRepository", () => { describe("addOrUpdateStop", () => { test("adds a new stop if nonexistent", async () => { - const newStop = { id: "stop1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } }; + const mockStops = generateMockStops(); + const newStop = mockStops[0]; await repository.addOrUpdateStop(newStop); @@ -378,8 +376,10 @@ describe("UnoptimizedInMemoryRepository", () => { }); test("updates an existing stop if it exists", async () => { - const existingStop = { id: "stop1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } }; - const updatedStop = { id: "stop1", name: "Updated Stop A", systemId: "sys1", coordinates: { latitude: 30, longitude: 40 } }; + const mockStops = generateMockStops(); + const existingStop = mockStops[0]; + const updatedStop = structuredClone(existingStop); + updatedStop.name = "Updated Stop"; await repository.addOrUpdateStop(existingStop); await repository.addOrUpdateStop(updatedStop); @@ -518,11 +518,10 @@ describe("UnoptimizedInMemoryRepository", () => { describe("clearStopData", () => { test("clears all stops from the repository", async () => { - const stop1 = { id: "stop1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } }; - const stop2 = { id: "stop2", name: "Stop B", systemId: "sys2", coordinates: { latitude: 10, longitude: 20 } }; - - await repository.addOrUpdateStop(stop1); - await repository.addOrUpdateStop(stop2); + const mockStops = generateMockStops(); + for (const stop of mockStops) { + await repository.addOrUpdateStop(stop); + } await repository.clearStopData();