Add tests for ReferenceClientV2 class
All checks were successful
Testing / run-tests (push) Successful in 3m12s

This commit is contained in:
Fred Boniface 2025-03-13 21:01:53 +00:00
parent 9e53756551
commit bfcacebe8f
2 changed files with 101 additions and 2 deletions

View File

@ -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<OwlBoardClientV2>;
beforeEach(() => {
// Create a mocked instance of OwlBoardClientV2
mockClient = new OwlBoardClientV2("https://api.example.com", "test-api-key") as jest.Mocked<OwlBoardClientV2>;
// 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<OwlBoardClientV2>;
beforeEach(() => {
// Create a mocked instance of OwlBoardClientV2
mockClient = new OwlBoardClientV2("https://api.example.com", "test-api-key") as jest.Mocked<OwlBoardClientV2>;
// 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);
});
});

View File

@ -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<ReferenceV2_LocationReferenceCodes> {
// Validation Required Here
const validators: Record<ReferenceV2_LocationReferenceCodeType, (code: string) => 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);
}