|
1 | | -jest.mock("buffer", () => { |
2 | | - const Buffer = jest.fn().mockReturnValue(new Uint8Array(0)); |
3 | | - (Buffer as any).from = jest.fn().mockReturnValue(new Uint8Array(0)); |
4 | | - |
5 | | - return { Buffer }; |
6 | | -}); |
7 | 1 | import { Buffer } from "buffer"; |
8 | 2 |
|
9 | 3 | import { fromArrayBuffer, fromString } from "./"; |
10 | 4 |
|
11 | | -describe("fromArrayBuffer", () => { |
12 | | - it("should throw if provided an argument that is not an ArrayBuffer", () => { |
13 | | - expect(() => fromArrayBuffer(255 as any)).toThrow(); |
14 | | - }); |
| 5 | +jest.mock("buffer"); |
15 | 6 |
|
16 | | - describe("Buffer.from", () => { |
17 | | - beforeEach(() => { |
18 | | - (Buffer.from as any).mockClear(); |
19 | | - }); |
20 | | - |
21 | | - it("should use Buffer.from if available", () => { |
22 | | - const underlyingBuffer = new ArrayBuffer(0); |
23 | | - const offsetArg = 12; |
24 | | - const lengthArg = 13; |
25 | | - fromArrayBuffer(underlyingBuffer, offsetArg, lengthArg); |
26 | | - |
27 | | - const { calls } = (Buffer.from as any).mock; |
28 | | - expect(calls.length).toBe(1); |
29 | | - expect(calls[0].length).toBe(3); |
| 7 | +afterEach(() => { |
| 8 | + jest.clearAllMocks(); |
| 9 | +}); |
30 | 10 |
|
31 | | - const [buffer, offset, length] = calls[0]; |
32 | | - expect(buffer).toBe(underlyingBuffer); |
33 | | - expect(offset).toBe(offsetArg); |
34 | | - expect(length).toBe(lengthArg); |
35 | | - }); |
| 11 | +describe("fromArrayBuffer", () => { |
| 12 | + it("throws if argument is not an ArrayBuffer", () => { |
| 13 | + const input = 255; |
| 14 | + // @ts-expect-error is not assignable to parameter of type 'ArrayBuffer' |
| 15 | + expect(() => fromArrayBuffer(input)).toThrow( |
| 16 | + new TypeError(`The "input" argument must be ArrayBuffer. Received type ${typeof input} (${input})`) |
| 17 | + ); |
36 | 18 | }); |
37 | 19 |
|
38 | | - describe("new Buffer", () => { |
39 | | - const bufferDotFrom = Buffer.from; |
40 | | - |
41 | | - beforeEach(() => { |
42 | | - (Buffer as any).mockClear(); |
43 | | - // @ts-ignore TODO: remove support as v3 is going to support Node.js v10+ |
44 | | - delete Buffer.from; |
45 | | - }); |
46 | | - |
47 | | - afterAll(() => { |
48 | | - Buffer.from = bufferDotFrom; |
49 | | - }); |
50 | | - |
51 | | - it("should use the Buffer constructor if Buffer.from is not defined", () => { |
52 | | - const underlyingBuffer = new ArrayBuffer(0); |
53 | | - fromArrayBuffer(underlyingBuffer); |
54 | | - |
55 | | - const { calls } = (Buffer as any).mock; |
56 | | - expect(calls.length).toBe(1); |
57 | | - expect(calls[0].length).toBe(1); |
58 | | - expect(calls[0][0]).toBe(underlyingBuffer); |
59 | | - }); |
60 | | - |
61 | | - it("should use the Buffer constructor if Buffer.from is inherited from Uint8Array", () => { |
62 | | - Buffer.from = Uint8Array.from as any; |
63 | | - const underlyingBuffer = new ArrayBuffer(0); |
64 | | - fromArrayBuffer(underlyingBuffer); |
| 20 | + describe("returns if argument is an ArrayBuffer", () => { |
| 21 | + const buffer = new ArrayBuffer(16); |
65 | 22 |
|
66 | | - const { calls } = (Buffer as any).mock; |
67 | | - expect(calls.length).toBe(1); |
68 | | - expect(calls[0].length).toBe(1); |
69 | | - expect(calls[0][0]).toBe(underlyingBuffer); |
| 23 | + it("with one arg", () => { |
| 24 | + fromArrayBuffer(buffer); |
| 25 | + expect(Buffer.from).toHaveBeenCalledTimes(1); |
| 26 | + expect(Buffer.from).toHaveBeenCalledWith(buffer, 0, buffer.byteLength); |
70 | 27 | }); |
71 | 28 |
|
72 | | - it("should throw if Buffer.from is undefined and a non-zero offset is provided", () => { |
73 | | - expect(() => fromArrayBuffer(new ArrayBuffer(0), 1)).toThrow(); |
| 29 | + it("with two args", () => { |
| 30 | + const offset = 12; |
| 31 | + fromArrayBuffer(buffer, offset); |
| 32 | + expect(Buffer.from).toHaveBeenCalledTimes(1); |
| 33 | + expect(Buffer.from).toHaveBeenCalledWith(buffer, offset, buffer.byteLength - offset); |
74 | 34 | }); |
75 | 35 |
|
76 | | - it("should not throw if Buffer.from is undefined and an offset of 0 is provided", () => { |
77 | | - expect(() => fromArrayBuffer(new ArrayBuffer(0), 0)).not.toThrow(); |
78 | | - }); |
79 | | - |
80 | | - it("should throw if Buffer.from is undefined and a length other than the length of the underlying buffer is provided", () => { |
81 | | - expect(() => fromArrayBuffer(new ArrayBuffer(10), 0, 9)).toThrow(); |
82 | | - }); |
83 | | - |
84 | | - it("should not throw if Buffer.from is undefined and a length of the length of the underlying buffer is provided", () => { |
85 | | - expect(() => fromArrayBuffer(new ArrayBuffer(10), 0, 10)).not.toThrow(); |
| 36 | + it("with three args", () => { |
| 37 | + const offset = 12; |
| 38 | + const length = 13; |
| 39 | + fromArrayBuffer(buffer, offset, length); |
| 40 | + expect(Buffer.from).toHaveBeenCalledTimes(1); |
| 41 | + expect(Buffer.from).toHaveBeenCalledWith(buffer, offset, length); |
86 | 42 | }); |
87 | 43 | }); |
88 | 44 | }); |
89 | 45 |
|
90 | 46 | describe("fromString", () => { |
91 | | - it("should throw if provided an argument that is not an ArrayBuffer", () => { |
92 | | - expect(() => fromString(255 as any)).toThrow(); |
| 47 | + it("throws if argument is not an ArrayBuffer", () => { |
| 48 | + const input = 255; |
| 49 | + // @ts-expect-error is not assignable to parameter of type 'ArrayBuffer' |
| 50 | + expect(() => fromString(input)).toThrow( |
| 51 | + new TypeError(`The "input" argument must be of type string. Received type ${typeof input} (${input})`) |
| 52 | + ); |
93 | 53 | }); |
94 | 54 |
|
95 | | - describe("Buffer.from", () => { |
96 | | - beforeEach(() => { |
97 | | - (Buffer.from as any).mockClear(); |
98 | | - }); |
| 55 | + describe("returns if argument is an ArrayBuffer", () => { |
| 56 | + const input = "a string"; |
99 | 57 |
|
100 | | - it("should use Buffer.from if available", () => { |
101 | | - const inputArg = "a string"; |
102 | | - const encodingArg = "utf16le"; |
103 | | - fromString(inputArg, encodingArg); |
104 | | - |
105 | | - const { calls } = (Buffer.from as any).mock; |
106 | | - expect(calls.length).toBe(1); |
107 | | - expect(calls[0].length).toBe(2); |
108 | | - |
109 | | - const [input, encoding] = calls[0]; |
110 | | - expect(input).toBe(inputArg); |
111 | | - expect(encoding).toBe(encodingArg); |
| 58 | + it("without explicit encoding", () => { |
| 59 | + fromString(input); |
| 60 | + expect(Buffer.from).toHaveBeenCalledTimes(1); |
| 61 | + expect(Buffer.from).toHaveBeenCalledWith(input); |
112 | 62 | }); |
113 | | - }); |
114 | | - |
115 | | - describe("new Buffer", () => { |
116 | | - const bufferDotFrom = Buffer.from; |
117 | | - |
118 | | - beforeEach(() => { |
119 | | - (Buffer as any).mockClear(); |
120 | | - // @ts-ignore TODO: remove support as v3 is going to support Node.js v10+ |
121 | | - delete Buffer.from; |
122 | | - }); |
123 | | - |
124 | | - afterAll(() => { |
125 | | - Buffer.from = bufferDotFrom; |
126 | | - }); |
127 | | - |
128 | | - it("should use the Buffer constructor if Buffer.from is not defined", () => { |
129 | | - fromString("string", "hex"); |
130 | | - |
131 | | - const { calls } = (Buffer as any).mock; |
132 | | - expect(calls.length).toBe(1); |
133 | | - expect(calls[0]).toEqual(["string", "hex"]); |
134 | | - }); |
135 | | - |
136 | | - it("should use the Buffer constructor if Buffer.from is inherited from Uint8Array", () => { |
137 | | - Buffer.from = Uint8Array.from as any; |
138 | | - fromString("string", "utf8"); |
139 | 63 |
|
140 | | - const { calls } = (Buffer as any).mock; |
141 | | - expect(calls.length).toBe(1); |
142 | | - expect(calls[0]).toEqual(["string", "utf8"]); |
| 64 | + it("with encoding", () => { |
| 65 | + const encoding = "utf16le"; |
| 66 | + fromString(input, encoding); |
| 67 | + expect(Buffer.from).toHaveBeenCalledTimes(1); |
| 68 | + expect(Buffer.from).toHaveBeenCalledWith(input, encoding); |
143 | 69 | }); |
144 | 70 | }); |
145 | 71 | }); |
0 commit comments