From bfcacebe8fe4be96c355951e09fe19f818121603 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 13 Mar 2025 21:01:53 +0000 Subject: [PATCH] Add tests for ReferenceClientV2 class --- src/clients/ReferenceClientV2.test.ts | 86 +++++++++++++++++++++++++++ src/clients/ReferenceClientV2.ts | 17 +++++- 2 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/clients/ReferenceClientV2.test.ts diff --git a/src/clients/ReferenceClientV2.test.ts b/src/clients/ReferenceClientV2.test.ts new file mode 100644 index 0000000..2ed3326 --- /dev/null +++ b/src/clients/ReferenceClientV2.test.ts @@ -0,0 +1,86 @@ +import type { ReferenceV2_LocationReferenceCodeType } from "../types/reference/ReferenceTypesV2"; +import { ValidationError } from "../errors"; +import { OwlBoardClientV2 } from "./client"; +import { ReferenceClientV2 } from "./ReferenceClientV2"; + +// Create a Jest mock for OwlBoardClientV2 +jest.mock("./client", () => { + return { + OwlBoardClientV2: jest.fn().mockImplementation(() => ({ + makeRequest: jest.fn().mockResolvedValue({ success: true }) + })) + }; +}); + +describe("defineReasonCode Tests", () => { + let instance: ReferenceClientV2; + let mockClient: jest.Mocked; + + beforeEach(() => { + // Create a mocked instance of OwlBoardClientV2 + mockClient = new OwlBoardClientV2("https://api.example.com", "test-api-key") as jest.Mocked; + + // Create an instance of MyClass with the mocked client + instance = new ReferenceClientV2(mockClient); + }); + + test("Calls makeRequest with correct method and path when code is valid", async () => { + await expect(instance.defineReasonCode(503)).resolves.toEqual({ success: true }); + + expect(mockClient.makeRequest).toHaveBeenCalledWith("GET", "/api/v2/ref/reasonCode/503"); + }) + + test("Throw ValidationError when code is invalid", async () => { + await expect(instance.defineReasonCode(1234)).rejects.toThrow(ValidationError); + }) +}) + +describe("lookupLocationReference Tests", () => { + let instance: ReferenceClientV2; + let mockClient: jest.Mocked; + + beforeEach(() => { + // Create a mocked instance of OwlBoardClientV2 + mockClient = new OwlBoardClientV2("https://api.example.com", "test-api-key") as jest.Mocked; + + // Create an instance of MyClass with the mocked client + instance = new ReferenceClientV2(mockClient); + }); + + test("Calls makeRequest with the correct path for valid CRS", async () => { + await expect(instance.lookupLocationReference("crs", "LDS")).resolves.toEqual({ success: true }); + + expect(mockClient.makeRequest).toHaveBeenCalledWith("GET", "/api/v2/ref/locationCode/crs/LDS"); + }); + + test("Calls makeRequest with the correct path for valid NLC", async () => { + await expect(instance.lookupLocationReference("nlc", "123456")).resolves.toEqual({ success: true }); + + expect(mockClient.makeRequest).toHaveBeenCalledWith("GET", "/api/v2/ref/locationCode/nlc/123456"); + }); + + test("Calls makeRequest with the correct path for valid STANOX", async () => { + await expect(instance.lookupLocationReference("stanox", "12345")).resolves.toEqual({ success: true }); + + expect(mockClient.makeRequest).toHaveBeenCalledWith("GET", "/api/v2/ref/locationCode/stanox/12345"); + }) + + test("Calls makeRequest with the correct path for valid STANOX", async () => { + await expect(instance.lookupLocationReference("tiploc", "bathspa")).resolves.toEqual({ success: true }); + + expect(mockClient.makeRequest).toHaveBeenCalledWith("GET", "/api/v2/ref/locationCode/tiploc/bathspa"); + }) + + test("Throws ValidationError for invalid CRS", async () => { + await expect(instance.lookupLocationReference("crs", "XYZA")).rejects.toThrow(ValidationError); + }); + + test("Throws ValidationError for invalid NLC", async () => { + await expect(instance.lookupLocationReference("nlc", "9999")).rejects.toThrow(ValidationError); + }); + + test("Throws error for unsupported type", async () => { + await expect(instance.lookupLocationReference("UNKNOWN" as ReferenceV2_LocationReferenceCodeType, "123")) + .rejects.toThrow(Error); + }); +}); diff --git a/src/clients/ReferenceClientV2.ts b/src/clients/ReferenceClientV2.ts index ad83c70..d99ab7c 100644 --- a/src/clients/ReferenceClientV2.ts +++ b/src/clients/ReferenceClientV2.ts @@ -1,4 +1,4 @@ -import { validateReasonCode } from "../inputValidation/inputValidation"; +import { validateCrs, validateNlc, validateReasonCode, validateStanox, validateTiploc } from "../inputValidation/inputValidation"; import { ReferenceV2_LocationReferenceCodes, ReferenceV2_LocationReferenceCodeType, ReferenceV2_ReasonCode } from "../types/reference/ReferenceTypesV2"; import { BaseOwlBoardClient } from "./client"; @@ -16,7 +16,20 @@ export class ReferenceClientV2 { } async lookupLocationReference(type: ReferenceV2_LocationReferenceCodeType, referenceCode: string): Promise { - // Validation Required Here + const validators: Record boolean> = { + crs: validateCrs, + tiploc: validateTiploc, + nlc: validateNlc, + stanox: validateStanox, + }; + + const validate = validators[type]; + if (!validate) { + throw new Error(`Unsupported code type: ${type}`); + } + + validate(referenceCode); + const path = `/api/v2/ref/locationCode/${type}/${referenceCode}` return this.client.makeRequest("GET", path); }