do the same for mock routes

This commit is contained in:
2025-01-21 15:22:56 -08:00
parent 02ebeb4782
commit dd2fde30b9
2 changed files with 25 additions and 16 deletions

View File

@@ -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 [
@@ -16,3 +18,11 @@ export function generateMockShuttles(): IShuttle[] {
{ 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: [] },
];
}

View File

@@ -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();