|
| 1 | +import { |
| 2 | + getSupplierAllocationsForVolumeGroup, |
| 3 | + getVariantDetails, |
| 4 | + getVolumeGroupDetails, |
| 5 | +} from "../supplier-config"; |
| 6 | +import { Deps } from "../../config/deps"; |
| 7 | + |
| 8 | +function makeDeps(overrides: Partial<Deps> = {}): Deps { |
| 9 | + const logger = { |
| 10 | + info: jest.fn(), |
| 11 | + error: jest.fn(), |
| 12 | + } as unknown as Deps["logger"]; |
| 13 | + |
| 14 | + const supplierConfigRepo = { |
| 15 | + getLetterVariant: jest.fn(), |
| 16 | + getVolumeGroup: jest.fn(), |
| 17 | + getSupplierAllocationsForVolumeGroup: jest.fn(), |
| 18 | + } as unknown as Deps["supplierConfigRepo"]; |
| 19 | + |
| 20 | + const base: Partial<Deps> = { |
| 21 | + logger: logger as any, |
| 22 | + supplierConfigRepo: supplierConfigRepo as any, |
| 23 | + }; |
| 24 | + |
| 25 | + return { ...(base as Deps), ...overrides } as Deps; |
| 26 | +} |
| 27 | + |
| 28 | +describe("supplier-config service", () => { |
| 29 | + afterEach(() => jest.resetAllMocks()); |
| 30 | + |
| 31 | + describe("getVariantDetails", () => { |
| 32 | + it("returns variant details and logs when found", async () => { |
| 33 | + const variant = { id: "v1", volumeGroupId: "g1" } as any; |
| 34 | + const deps = makeDeps(); |
| 35 | + deps.supplierConfigRepo.getLetterVariant = jest |
| 36 | + .fn() |
| 37 | + .mockResolvedValue(variant); |
| 38 | + |
| 39 | + const result = await getVariantDetails("v1", deps); |
| 40 | + |
| 41 | + expect(result).toBe(variant); |
| 42 | + expect(deps.logger.info).toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("logs an error and returns undefined when not found", async () => { |
| 46 | + const deps = makeDeps(); |
| 47 | + deps.supplierConfigRepo.getLetterVariant = jest.fn().mockResolvedValue(); |
| 48 | + |
| 49 | + const result = await getVariantDetails("missing", deps); |
| 50 | + |
| 51 | + expect(result).toBeUndefined(); |
| 52 | + expect(deps.logger.error).toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("getVolumeGroupDetails", () => { |
| 57 | + it("returns group details when active", async () => { |
| 58 | + const group = { |
| 59 | + id: "g1", |
| 60 | + status: "PROD", |
| 61 | + startDate: "2020-01-01", |
| 62 | + } as any; |
| 63 | + const deps = makeDeps(); |
| 64 | + deps.supplierConfigRepo.getVolumeGroup = jest |
| 65 | + .fn() |
| 66 | + .mockResolvedValue(group); |
| 67 | + |
| 68 | + const result = await getVolumeGroupDetails("g1", deps); |
| 69 | + |
| 70 | + expect(result).toBe(group); |
| 71 | + expect(deps.logger.info).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("throws when group is not active based on dates/status", async () => { |
| 75 | + const future = new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(); |
| 76 | + const group = { id: "g2", status: "DRAFT", startDate: future } as any; |
| 77 | + const deps = makeDeps(); |
| 78 | + deps.supplierConfigRepo.getVolumeGroup = jest |
| 79 | + .fn() |
| 80 | + .mockResolvedValue(group); |
| 81 | + |
| 82 | + await expect(getVolumeGroupDetails("g2", deps)).rejects.toThrow( |
| 83 | + /not active/, |
| 84 | + ); |
| 85 | + expect(deps.logger.error).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe("getSupplierAllocationsForVolumeGroup", () => { |
| 90 | + const allocations = [ |
| 91 | + { supplier: "s1", variantId: "v1" }, |
| 92 | + { supplier: "s2", variantId: "v2" }, |
| 93 | + ] as any[]; |
| 94 | + |
| 95 | + it("returns all allocations when no supplierId provided", async () => { |
| 96 | + const deps = makeDeps(); |
| 97 | + deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup = jest |
| 98 | + .fn() |
| 99 | + .mockResolvedValue(allocations); |
| 100 | + |
| 101 | + const result = await getSupplierAllocationsForVolumeGroup("g1", "", deps); |
| 102 | + |
| 103 | + expect(result).toEqual(allocations); |
| 104 | + expect(deps.logger.info).toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("filters by supplierId when provided", async () => { |
| 108 | + const deps = makeDeps(); |
| 109 | + deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup = jest |
| 110 | + .fn() |
| 111 | + .mockResolvedValue(allocations); |
| 112 | + |
| 113 | + const result = await getSupplierAllocationsForVolumeGroup( |
| 114 | + "g1", |
| 115 | + "s2", |
| 116 | + deps, |
| 117 | + ); |
| 118 | + |
| 119 | + expect(result).toEqual([allocations[1]]); |
| 120 | + }); |
| 121 | + |
| 122 | + it("throws when supplierId provided but no matching allocation", async () => { |
| 123 | + const deps = makeDeps(); |
| 124 | + deps.supplierConfigRepo.getSupplierAllocationsForVolumeGroup = jest |
| 125 | + .fn() |
| 126 | + .mockResolvedValue(allocations); |
| 127 | + |
| 128 | + await expect( |
| 129 | + getSupplierAllocationsForVolumeGroup("g1", "missing", deps), |
| 130 | + ).rejects.toThrow(/No supplier allocations found/); |
| 131 | + expect(deps.logger.error).toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments