1
+ import { lstatSync } from "fs" ;
2
+
1
3
import { calculateBodyLength } from "./calculateBodyLength" ;
2
4
3
- const arrayBuffer = new ArrayBuffer ( 1 ) ;
4
- const typedArray = new Uint8Array ( 1 ) ;
5
- const view = new DataView ( arrayBuffer ) ;
5
+ jest . mock ( "fs" ) ;
6
6
7
7
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 ) ;
10
21
} ) ;
11
22
12
23
it ( "should handle string inputs" , ( ) => {
@@ -28,4 +39,13 @@ describe(calculateBodyLength.name, () => {
28
39
it ( "should handle DataView inputs" , ( ) => {
29
40
expect ( calculateBodyLength ( view ) ) . toEqual ( 1 ) ;
30
41
} ) ;
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
+ } ) ;
31
51
} ) ;
0 commit comments