-
Notifications
You must be signed in to change notification settings - Fork 544
/
getChecksum.spec.ts
44 lines (37 loc) · 1.5 KB
/
getChecksum.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { getChecksum } from "./getChecksum";
import { isStreaming } from "./isStreaming";
import { stringHasher } from "./stringHasher";
jest.mock("./isStreaming");
jest.mock("./stringHasher");
describe(getChecksum.name, () => {
const mockOptions = {
streamHasher: jest.fn(),
checksumAlgorithmFn: jest.fn(),
base64Encoder: jest.fn(),
};
const mockBody = "mockBody";
const mockOutput = "mockOutput";
const mockRawOutput = Buffer.from(mockOutput);
beforeEach(() => {
mockOptions.base64Encoder.mockResolvedValueOnce(mockOutput);
});
afterEach(() => {
jest.clearAllMocks();
});
it("gets checksum from streamHasher if body is streaming", async () => {
(isStreaming as jest.Mock).mockReturnValue(true);
mockOptions.streamHasher.mockResolvedValue(mockRawOutput);
const checksum = await getChecksum(mockBody, mockOptions);
expect(checksum).toEqual(mockOutput);
expect(stringHasher).not.toHaveBeenCalled();
expect(mockOptions.streamHasher).toHaveBeenCalledWith(mockOptions.checksumAlgorithmFn, mockBody);
});
it("gets checksum from stringHasher if body is not streaming", async () => {
(isStreaming as jest.Mock).mockReturnValue(false);
(stringHasher as jest.Mock).mockResolvedValue(mockRawOutput);
const checksum = await getChecksum(mockBody, mockOptions);
expect(checksum).toEqual(mockOutput);
expect(mockOptions.streamHasher).not.toHaveBeenCalled();
expect(stringHasher).toHaveBeenCalledWith(mockOptions.checksumAlgorithmFn, mockBody);
});
});