@@ -17,9 +17,14 @@ jest.mock('../../BatchedBridge/NativeModules', () => ({
1717 } ,
1818} ) ) ;
1919
20+ const MockBlobModule = require ( '../__mocks__/BlobModule' ) . default ;
2021const Blob = require ( '../Blob' ) . default ;
2122
2223describe ( 'Blob' , function ( ) {
24+ beforeEach ( ( ) => {
25+ MockBlobModule . createFromParts . mockClear ( ) ;
26+ } ) ;
27+
2328 it ( 'should create empty blob' , ( ) => {
2429 const blob = new Blob ( ) ;
2530 expect ( blob ) . toBeInstanceOf ( Blob ) ;
@@ -58,6 +63,86 @@ describe('Blob', function () {
5863 expect ( blob . type ) . toBe ( '' ) ;
5964 } ) ;
6065
66+ it ( 'should send array buffer and typed array parts as binary parts' , ( ) => {
67+ const bytes = Uint8Array . from ( [ 10 , 20 , 30 , 40 ] ) ;
68+ const blob = new Blob ( [ bytes . buffer , bytes . subarray ( 1 , 3 ) ] ) ;
69+
70+ expect ( blob . size ) . toBe ( 6 ) ;
71+
72+ const [ parts , binaryParts ] = MockBlobModule . createFromParts . mock . calls [ 0 ] ;
73+
74+ expect ( parts ) . toEqual ( [
75+ { type : 'binaryPart' , data : 0 } ,
76+ { type : 'binaryPart' , data : 1 } ,
77+ ] ) ;
78+ expect ( binaryParts . map ( b => Array . from ( new Uint8Array ( b ) ) ) ) . toEqual ( [
79+ [ 10 , 20 , 30 , 40 ] ,
80+ [ 20 , 30 ] ,
81+ ] ) ;
82+ } ) ;
83+
84+ it ( 'should preserve part ordering across mixed types' , ( ) => {
85+ const inner = new Blob ( [ 'D' ] ) ;
86+ MockBlobModule . createFromParts . mockClear ( ) ;
87+
88+ const blob = new Blob ( [ 'A' , Uint8Array . from ( [ 66 , 67 ] ) , inner ] ) ;
89+ expect ( blob . size ) . toBe ( 4 ) ;
90+
91+ const [ parts , binaryParts ] = MockBlobModule . createFromParts . mock . calls [ 0 ] ;
92+
93+ expect ( parts ) . toEqual ( [
94+ { type : 'string' , data : 'A' } ,
95+ { type : 'binaryPart' , data : 0 } ,
96+ { type : 'blob' , data : inner . data } ,
97+ ] ) ;
98+ expect ( binaryParts . map ( b => Array . from ( new Uint8Array ( b ) ) ) ) . toEqual ( [
99+ [ 66 , 67 ] ,
100+ ] ) ;
101+ } ) ;
102+
103+ it ( 'should handle empty array buffer parts' , ( ) => {
104+ expect ( new Blob ( [ new ArrayBuffer ( 0 ) ] ) . size ) . toBe ( 0 ) ;
105+ } ) ;
106+
107+ it ( 'should count Float64Array and DataView parts in bytes' , ( ) => {
108+ const f64 = new Float64Array ( [ 1.5 , 2.5 , 3.5 ] ) ;
109+ expect ( new Blob ( [ f64 ] ) . size ) . toBe ( 24 ) ;
110+ expect ( new Blob ( [ new DataView ( f64 . buffer , 8 , 8 ) ] ) . size ) . toBe ( 8 ) ;
111+ } ) ;
112+
113+ it ( 'should treat a detached ArrayBuffer as an empty blob part' , ( ) => {
114+ const ab = new ArrayBuffer ( 8 ) ;
115+ // $FlowFixMe[cannot-resolve-name] Node's structuredClone is not in RN's Flow libs.
116+ structuredClone ( ab , { transfer : [ ab ] } ) ;
117+ const blob = new Blob ( [ ab ] ) ;
118+ expect ( blob . size ) . toBe ( 0 ) ;
119+ } ) ;
120+
121+ it ( 'should treat a detached ArrayBufferView as an empty blob part' , ( ) => {
122+ const ab = new ArrayBuffer ( 8 ) ;
123+ const view = new Uint8Array ( ab , 2 , 4 ) ;
124+ // $FlowFixMe[cannot-resolve-name] Node's structuredClone is not in RN's Flow libs.
125+ structuredClone ( ab , { transfer : [ ab ] } ) ;
126+ const blob = new Blob ( [ view ] ) ;
127+ expect ( blob . size ) . toBe ( 0 ) ;
128+ } ) ;
129+
130+ it ( 'stringifies parts that are neither Blob nor BufferSource (W3C: USVString)' , ( ) => {
131+ // $FlowExpectedError[incompatible-type]
132+ expect ( new Blob ( [ 42 ] ) . size ) . toBe ( 2 ) ;
133+ // $FlowExpectedError[incompatible-type]
134+ expect ( new Blob ( [ null ] ) . size ) . toBe ( 4 ) ;
135+ // $FlowExpectedError[incompatible-type]
136+ expect ( new Blob ( [ undefined ] ) . size ) . toBe ( 9 ) ;
137+ // $FlowExpectedError[incompatible-type]
138+ expect ( new Blob ( [ { } ] ) . size ) . toBe ( 15 ) ;
139+ // $FlowExpectedError[incompatible-type]
140+ expect ( new Blob ( [ new String ( 'abc' ) ] ) . size ) . toBe ( 3 ) ; // eslint-disable-line no-new-wrappers
141+
142+ const [ parts ] = MockBlobModule . createFromParts . mock . calls [ 0 ] ;
143+ expect ( parts ) . toEqual ( [ { type : 'string' , data : '42' } ] ) ;
144+ } ) ;
145+
61146 it ( 'should slice a blob' , ( ) => {
62147 const blob = new Blob ( ) ;
63148
0 commit comments