|
| 1 | +/** |
| 2 | + * Unit tests for s3-helpers.ts |
| 3 | + * |
| 4 | + * Mocks: |
| 5 | + * - @aws-sdk/client-s3: S3Client (with send) and PutObjectCommand |
| 6 | + * - node:fs.readFileSync to avoid touching the filesystem |
| 7 | + */ |
| 8 | + |
| 9 | +import * as s3Module from "@aws-sdk/client-s3"; |
| 10 | +import uploadFile from "../../helpers/s3-helpers"; |
| 11 | + |
| 12 | +jest.mock("@aws-sdk/client-s3", () => { |
| 13 | + const sendMock = jest.fn(); |
| 14 | + class PutObjectCommand { |
| 15 | + input: any; |
| 16 | + |
| 17 | + constructor(input: any) { |
| 18 | + this.input = input; |
| 19 | + } |
| 20 | + } |
| 21 | + const S3Client = jest.fn().mockImplementation(() => ({ send: sendMock })); |
| 22 | + return { |
| 23 | + S3Client, |
| 24 | + PutObjectCommand, |
| 25 | + __sendMock: sendMock, |
| 26 | + __esModule: true, |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +jest.mock("node:fs", () => ({ |
| 31 | + readFileSync: jest.fn().mockReturnValue(Buffer.from("fake-pdf-bytes")), |
| 32 | +})); |
| 33 | + |
| 34 | +describe("uploadFile", () => { |
| 35 | + const bucket = "my-bucket"; |
| 36 | + const supplierId = "supplier-1"; |
| 37 | + const sourceFilename = "some.pdf"; |
| 38 | + const targetFilename = "target.pdf"; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("calls S3Client.send with a PutObjectCommand containing correct params", async () => { |
| 45 | + const sendMock = (s3Module as any).__sendMock as jest.Mock; |
| 46 | + sendMock.mockResolvedValue({ ETag: '"etag-value"' }); |
| 47 | + |
| 48 | + await expect( |
| 49 | + uploadFile(bucket, supplierId, sourceFilename, targetFilename), |
| 50 | + ).resolves.toBeDefined(); |
| 51 | + |
| 52 | + // S3Client is a mocked constructor — grab the instance that was created |
| 53 | + const S3ClientMock = (s3Module as any).S3Client as jest.Mock; |
| 54 | + expect(S3ClientMock).toHaveBeenCalled(); |
| 55 | + |
| 56 | + const instance = S3ClientMock.mock.results[0].value; |
| 57 | + expect(instance.send).toHaveBeenCalledTimes(1); |
| 58 | + |
| 59 | + const calledWith = instance.send.mock.calls[0][0]; |
| 60 | + // The mocked PutObjectCommand stores input as `input` property |
| 61 | + expect(calledWith).toHaveProperty("input"); |
| 62 | + expect(calledWith.input).toEqual({ |
| 63 | + Bucket: bucket, |
| 64 | + Key: `${supplierId}/${targetFilename}`, |
| 65 | + Body: Buffer.from("fake-pdf-bytes"), |
| 66 | + ContentType: "application/pdf", |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("logs and rethrows when S3Client.send rejects", async () => { |
| 71 | + const sendMock = (s3Module as any).__sendMock as jest.Mock; |
| 72 | + const err = new Error("upload-failed"); |
| 73 | + sendMock.mockRejectedValueOnce(err); |
| 74 | + |
| 75 | + const consoleSpy = jest |
| 76 | + .spyOn(console, "error") |
| 77 | + .mockImplementation(() => {}); |
| 78 | + |
| 79 | + await expect( |
| 80 | + uploadFile(bucket, supplierId, sourceFilename, targetFilename), |
| 81 | + ).rejects.toThrow("upload-failed"); |
| 82 | + |
| 83 | + expect(consoleSpy).toHaveBeenCalledWith("Error uploading file:", err); |
| 84 | + |
| 85 | + consoleSpy.mockRestore(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments