Skip to content

Commit c33dde5

Browse files
committed
feat(lib-storage): addEventListener tests and error checking adjustment and dispatchEvent tests
1 parent a788cad commit c33dde5

File tree

2 files changed

+143
-20
lines changed

2 files changed

+143
-20
lines changed

lib/lib-storage/src/s3-transfer-manager/S3TransferManager.spec.ts

Lines changed: 133 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { S3, S3Client } from "@aws-sdk/client-s3";
22
import { TransferCompleteEvent, TransferEvent } from "@aws-sdk/lib-storage/dist-types/s3-transfer-manager/types";
3-
import { beforeAll, describe, expect, test as it, vi } from "vitest";
3+
import { beforeAll, beforeEach, describe, expect, test as it, vi } from "vitest";
44

55
import { S3TransferManager } from "./S3TransferManager";
66

@@ -30,10 +30,6 @@ describe("S3TransferManager Unit Tests", () => {
3030
});
3131
describe("S3TransferManager Constructor", () => {
3232
it("Should create an instance of S3TransferManager with defaults given no parameters", () => {
33-
const defaultS3Client = new S3Client({
34-
requestChecksumCalculation: "WHEN_SUPPORTED",
35-
responseChecksumValidation: "WHEN_SUPPORTED",
36-
});
3733
const tm = new S3TransferManager() as any;
3834

3935
expect(tm.s3ClientInstance).toBeInstanceOf(S3Client);
@@ -112,14 +108,14 @@ describe("S3TransferManager Unit Tests", () => {
112108
};
113109
}
114110

115-
beforeAll(async () => {
111+
beforeEach(async () => {
116112
tm = new S3TransferManager({
117113
s3ClientInstance: client,
118114
});
119115
});
120116

121117
describe("addEventListener", () => {
122-
it("Should add a new listener for each callback", () => {
118+
it("Should register callbacks for all supported event types", () => {
123119
tm.addEventListener("transferInitiated", initiated);
124120
tm.addEventListener("bytesTransferred", transferring);
125121
tm.addEventListener("transferComplete", completed);
@@ -133,12 +129,139 @@ describe("S3TransferManager Unit Tests", () => {
133129
});
134130
});
135131

136-
it("Should append new listeners for a callback");
132+
it("Should handle registering the same listener multiple times", () => {
133+
const callback1 = vi.fn();
134+
tm.addEventListener("transferInitiated", callback1);
135+
tm.addEventListener("transferInitiated", callback1);
136+
137+
expect((tm as any).eventListeners.transferInitiated).toEqual([callback1, callback1]);
138+
});
139+
140+
it("Should handle different callbacks for the same event type", () => {
141+
const callback1 = vi.fn();
142+
const callback2 = vi.fn();
143+
144+
tm.addEventListener("bytesTransferred", callback1);
145+
tm.addEventListener("bytesTransferred", callback2);
146+
147+
expect((tm as any).eventListeners.bytesTransferred).toEqual([callback1, callback2]);
148+
});
149+
150+
it("Should handle object-style callbacks", () => {
151+
const objectCallback = {
152+
handleEvent: vi.fn(),
153+
};
154+
tm.addEventListener("transferInitiated", objectCallback as any);
155+
156+
expect((tm as any).eventListeners.transferInitiated).toEqual([objectCallback]);
157+
});
158+
159+
it("Should handle a mix of object-style callbacks and function for the same event", () => {
160+
const callback = vi.fn();
161+
const objectCallback = {
162+
handleEvent: vi.fn(),
163+
};
164+
tm.addEventListener("transferInitiated", objectCallback as any);
165+
tm.addEventListener("transferInitiated", callback);
166+
167+
expect((tm as any).eventListeners.transferInitiated).toEqual([objectCallback, callback]);
168+
});
169+
170+
it("Should throw an error for an invalid event type", () => {
171+
expect(() => {
172+
(tm as any).addEventListener("invalidEvent", initiated);
173+
}).toThrow("Unknown event type: invalidEvent");
174+
});
175+
176+
it("Should handle options.once correctly", () => {
177+
const mockCallback = vi.fn();
178+
tm.addEventListener("transferInitiated", mockCallback, { once: true });
179+
180+
const event = Object.assign(new Event("transferInitiated"), {
181+
request: {},
182+
snapshot: {},
183+
});
184+
185+
tm.dispatchEvent(event);
186+
tm.dispatchEvent(event);
187+
188+
expect(mockCallback).toHaveBeenCalledTimes(1);
189+
});
190+
191+
it("Should handle boolean options parameter", () => {
192+
tm.addEventListener("transferInitiated", initiated, true);
193+
expect((tm as any).eventListeners.transferInitiated).toContain(initiated);
194+
});
195+
196+
it("Should handle null callback", () => {
197+
expect(() => {
198+
(tm as any).addEventListener("transferInitiated", null);
199+
}).not.toThrow();
200+
});
201+
202+
it("Should handle object-style callback with handleEvent", () => {
203+
const objectCallback = { handleEvent: vi.fn() };
204+
tm.addEventListener("transferInitiated", objectCallback as any);
205+
expect((tm as any).eventListeners.transferInitiated).toContain(objectCallback);
206+
});
137207
});
138208

139-
describe.skip("dispatchEvent"), () => {};
209+
describe("dispatchEvent", () => {
210+
it("Should dispatch an event", () => {
211+
const mockCallback = vi.fn();
212+
tm.addEventListener("bytesTransferred", mockCallback);
213+
214+
const event = Object.assign(new Event("bytesTransferred"), {
215+
request: {},
216+
snapshot: {},
217+
});
218+
219+
const result = tm.dispatchEvent(event);
220+
221+
expect(mockCallback).toHaveBeenCalledTimes(1);
222+
expect(mockCallback).toHaveBeenCalledWith(event);
223+
expect(result).toBe(true);
224+
});
225+
226+
it("Should dispatch an event with request, snapshot, and response information", () => {
227+
const mockCompleted = vi.fn().mockImplementation(completed);
228+
tm.addEventListener("transferComplete", mockCompleted);
229+
230+
const event = Object.assign(new Event("transferComplete"), {
231+
request: { bucket: "test" },
232+
snapshot: { bytes: 100 },
233+
response: { status: "success" },
234+
});
235+
236+
tm.dispatchEvent(event);
237+
238+
expect(mockCompleted).toHaveBeenCalledWith(event);
239+
expect(mockCompleted).toHaveReturnedWith({
240+
request: { bucket: "test" },
241+
snapshot: { bytes: 100 },
242+
response: { status: "success" },
243+
});
244+
});
245+
246+
it("Should call multiple listeners for the same event type", () => {
247+
const mockCallback = vi.fn();
248+
tm.addEventListener("transferInitiated", mockCallback);
249+
tm.addEventListener("transferInitiated", mockCallback);
250+
251+
const event = Object.assign(new Event("transferInitiated"), {
252+
request: {},
253+
snapshot: {},
254+
});
255+
256+
const result = tm.dispatchEvent(event);
257+
258+
expect(mockCallback).toHaveBeenCalledTimes(2);
259+
expect(mockCallback).toHaveBeenCalledWith(event);
260+
expect(result).toBe(true);
261+
});
262+
});
140263

141-
describe.skip("removeEventListener"), () => {};
264+
describe.skip("removeEventListener", () => {});
142265
});
143266

144267
describe("validateExpectedRanges()", () => {

lib/lib-storage/src/s3-transfer-manager/S3TransferManager.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export class S3TransferManager implements IS3TransferManager {
9797
const eventType = type as keyof TransferEventListeners;
9898
const listeners = this.eventListeners[eventType];
9999

100+
if (!listeners) {
101+
throw new Error(`Unknown event type: ${eventType}`);
102+
}
103+
100104
// TODO: Add support for AbortSignal
101105

102106
const once = typeof options !== "boolean" && options?.once;
@@ -112,16 +116,12 @@ export class S3TransferManager implements IS3TransferManager {
112116
};
113117
}
114118

115-
if (listeners) {
116-
if (eventType === "transferInitiated" || eventType === "bytesTransferred" || eventType === "transferFailed") {
117-
listeners.push(updatedCallback as EventListener<TransferEvent>);
118-
} else if (eventType === "transferComplete") {
119-
(listeners as EventListener<TransferCompleteEvent>[]).push(
120-
updatedCallback as EventListener<TransferCompleteEvent>
121-
);
122-
} else {
123-
throw new Error(`Unknown event type: ${type}`);
124-
}
119+
if (eventType === "transferInitiated" || eventType === "bytesTransferred" || eventType === "transferFailed") {
120+
listeners.push(updatedCallback as EventListener<TransferEvent>);
121+
} else if (eventType === "transferComplete") {
122+
(listeners as EventListener<TransferCompleteEvent>[]).push(
123+
updatedCallback as EventListener<TransferCompleteEvent>
124+
);
125125
}
126126
}
127127

0 commit comments

Comments
 (0)