From 05b3538a91d601a6ec8f0ee921f5487e6d4037ae Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 21 Jan 2025 15:12:53 -0800 Subject: [PATCH] add and use generator method for system objects --- test/generators.ts | 10 ++++++ ...UnoptimizedInMemoryRepositoryTests.test.ts | 34 +++++++++---------- 2 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 test/generators.ts diff --git a/test/generators.ts b/test/generators.ts new file mode 100644 index 0000000..964c0e7 --- /dev/null +++ b/test/generators.ts @@ -0,0 +1,10 @@ +import { ISystem } from "../src/entities/entities"; + + +export function generateMockSystems(): ISystem[] { + return [ + { id: "1", name: "System A" }, + { id: "2", name: "System B" }, + { id: "3", name: "System C" }, + ]; +} \ No newline at end of file diff --git a/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts b/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts index a997b7c..972c2d6 100644 --- a/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts +++ b/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts @@ -1,5 +1,7 @@ import { beforeEach, describe, expect, test } from "@jest/globals"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; +import { generateMockSystems } from "../generators"; +import { mock } from "node:test"; // For repositories created in the future, reuse core testing // logic from here and differentiate setup (e.g. creating mocks) @@ -18,10 +20,7 @@ describe("UnoptimizedInMemoryRepository", () => { describe("getSystems", () => { test("gets the systems stored in the repository", async () => { - const mockSystems = [ - { id: "1", name: "System A" }, - { id: "2", name: "System B" }, - ]; + const mockSystems = generateMockSystems(); await repository.addOrUpdateSystem(mockSystems[0]); await repository.addOrUpdateSystem(mockSystems[1]); @@ -39,11 +38,7 @@ describe("UnoptimizedInMemoryRepository", () => { describe("getSystemById", () => { test("gets a system by the ID if it exists", async () => { - const mockSystems = [ - { id: "1", name: "System A" }, - { id: "2", name: "System B" }, - { id: "3", name: "System C" }, - ]; + const mockSystems = generateMockSystems(); for (const system of mockSystems) { await repository.addOrUpdateSystem(system); } @@ -313,7 +308,8 @@ describe("UnoptimizedInMemoryRepository", () => { describe("addOrUpdateSystem", () => { test("adds a new system if nonexistent", async () => { - const newSystem = { id: "sys1", name: "System A", millisecondsSinceEpoch: 1620000000000 }; + const mockSystems = generateMockSystems(); + const newSystem = mockSystems[0]; await repository.addOrUpdateSystem(newSystem); @@ -322,8 +318,10 @@ describe("UnoptimizedInMemoryRepository", () => { }); test("updates an existing system if it exists", async () => { - const existingSystem = { id: "sys1", name: "System A", millisecondsSinceEpoch: 1620000000000 }; - const updatedSystem = { id: "sys1", name: "Updated System A", millisecondsSinceEpoch: 1625000000000 }; + const mockSystems = generateMockSystems(); + const existingSystem = mockSystems[0]; + const updatedSystem = structuredClone(existingSystem); + updatedSystem.name = "Updated System"; await repository.addOrUpdateSystem(existingSystem); await repository.addOrUpdateSystem(updatedSystem); @@ -445,11 +443,10 @@ describe("UnoptimizedInMemoryRepository", () => { describe("clearSystemData", () => { test("clears all systems from the repository", async () => { - const system1 = { id: "sys1", name: "System A" }; - const system2 = { id: "sys2", name: "System B" }; - - await repository.addOrUpdateSystem(system1); - await repository.addOrUpdateSystem(system2); + const mockSystems = generateMockSystems(); + for (const system of mockSystems) { + await repository.addOrUpdateSystem(system); + } await repository.clearSystemData(); @@ -458,7 +455,8 @@ describe("UnoptimizedInMemoryRepository", () => { }); test("clears system data when the repository has data", async () => { - const system = { id: "sys1", name: "System A" }; + const mockSystems = generateMockSystems(); + const system = mockSystems[0]; await repository.addOrUpdateSystem(system); await repository.clearSystemData();