Skip to content

Commit eb0a33b

Browse files
committed
test: add tests for undefined and file stream
1 parent 6a8e449 commit eb0a33b

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

packages/util-body-length-node/src/calculateBodyLength.spec.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
import { lstatSync } from "fs";
2+
13
import { calculateBodyLength } from "./calculateBodyLength";
24

3-
const arrayBuffer = new ArrayBuffer(1);
4-
const typedArray = new Uint8Array(1);
5-
const view = new DataView(arrayBuffer);
5+
jest.mock("fs");
66

77
describe(calculateBodyLength.name, () => {
8-
it("should handle null/undefined objects", () => {
9-
expect(calculateBodyLength(null)).toEqual(0);
8+
const arrayBuffer = new ArrayBuffer(1);
9+
const typedArray = new Uint8Array(1);
10+
const view = new DataView(arrayBuffer);
11+
12+
afterEach(() => {
13+
jest.clearAllMocks();
14+
});
15+
16+
it.each([
17+
[0, null],
18+
[0, undefined],
19+
])("should return %s for %s", (output, input) => {
20+
expect(calculateBodyLength(input)).toEqual(output);
1021
});
1122

1223
it("should handle string inputs", () => {
@@ -28,4 +39,13 @@ describe(calculateBodyLength.name, () => {
2839
it("should handle DataView inputs", () => {
2940
expect(calculateBodyLength(view)).toEqual(1);
3041
});
42+
43+
it("should handle stream created using fs.createReadStream", () => {
44+
const mockSize = { size: 10 };
45+
(lstatSync as jest.Mock).mockReturnValue(mockSize);
46+
47+
// Populate path as string to mock body created from fs.createReadStream
48+
const mockBody = { path: "mockPath" };
49+
expect(calculateBodyLength(mockBody)).toEqual(mockSize.size);
50+
});
3151
});

0 commit comments

Comments
 (0)