@@ -3,6 +3,19 @@ import toFormData from '../../../lib/helpers/toFormData.js';
33import FormData from 'form-data' ;
44
55describe ( 'helpers::toFormData' , function ( ) {
6+ function createRNFormDataSpy ( ) {
7+ const calls = [ ] ;
8+ return {
9+ calls,
10+ append ( key , value ) {
11+ calls . push ( [ key , value ] ) ;
12+ } ,
13+ getParts ( ) {
14+ return [ ] ;
15+ }
16+ } ;
17+ }
18+
619 it ( 'should convert a flat object to FormData' , function ( ) {
720 const data = {
821 foo : 'bar' ,
@@ -50,4 +63,70 @@ describe('helpers::toFormData', function () {
5063 const formData = toFormData ( data , new FormData ( ) ) ;
5164 assert . ok ( formData instanceof FormData ) ;
5265 } ) ;
66+
67+ it ( 'should append root-level React Native blob without recursion' , function ( ) {
68+ const formData = createRNFormDataSpy ( ) ;
69+
70+ const blob = {
71+ uri : 'file://test.png' ,
72+ type : 'image/png' ,
73+ name : 'test.png'
74+ } ;
75+
76+ toFormData ( { file : blob } , formData ) ;
77+
78+ assert . strictEqual ( formData . calls . length , 1 ) ;
79+ assert . strictEqual ( formData . calls [ 0 ] [ 0 ] , 'file' ) ;
80+ assert . strictEqual ( formData . calls [ 0 ] [ 1 ] , blob ) ;
81+ } ) ;
82+
83+ it ( 'should append nested React Native blob without recursion' , function ( ) {
84+ const formData = createRNFormDataSpy ( ) ;
85+
86+ const blob = {
87+ uri : 'file://nested.png' ,
88+ type : 'image/png' ,
89+ name : 'nested.png'
90+ } ;
91+
92+ toFormData ( { nested : { file : blob } } , formData ) ;
93+
94+ assert . strictEqual ( formData . calls . length , 1 ) ;
95+ assert . strictEqual ( formData . calls [ 0 ] [ 0 ] , 'nested[file]' ) ;
96+ assert . strictEqual ( formData . calls [ 0 ] [ 1 ] , blob ) ;
97+ } ) ;
98+
99+ it ( 'should append deeply nested React Native blob without recursion' , function ( ) {
100+ const formData = createRNFormDataSpy ( ) ;
101+
102+ const blob = {
103+ uri : 'file://deep.png' ,
104+ name : 'deep.png'
105+ } ;
106+
107+ toFormData ( { a : { b : { c : blob } } } , formData ) ;
108+
109+ assert . strictEqual ( formData . calls . length , 1 ) ;
110+ assert . strictEqual ( formData . calls [ 0 ] [ 0 ] , 'a[b][c]' ) ;
111+ assert . strictEqual ( formData . calls [ 0 ] [ 1 ] , blob ) ;
112+ } ) ;
113+
114+ it ( 'should NOT recurse into React Native blob properties' , function ( ) {
115+ const formData = createRNFormDataSpy ( ) ;
116+
117+ const blob = {
118+ uri : 'file://nope.png' ,
119+ type : 'image/png' ,
120+ name : 'nope.png'
121+ } ;
122+
123+ toFormData ( { file : blob } , formData ) ;
124+
125+ const keys = formData . calls . map ( call => call [ 0 ] ) ;
126+
127+ assert . deepStrictEqual ( keys , [ 'file' ] ) ;
128+ assert . ok ( ! keys . some ( k => k . includes ( 'uri' ) ) ) ;
129+ assert . ok ( ! keys . some ( k => k . includes ( 'type' ) ) ) ;
130+ assert . ok ( ! keys . some ( k => k . includes ( 'name' ) ) ) ;
131+ } ) ;
53132} ) ;
0 commit comments