Files
react-native/Libraries/Network/__tests__/FormData-test.js
T
matinzd d05a5d1551 feat: add getAll function to formdata (#32444)
Summary:
The getAll() method of the [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) interface returns all the values associated with a given key from within a FormData object.

## Changelog

[General] [Added] - Add getAll function to FormData class for getting all parts containing that key. This is also available in web API.

Pull Request resolved: https://github.com/facebook/react-native/pull/32444

Test Plan: New test added in FormData-test.js

Reviewed By: lunaleaps

Differential Revision: D31798633

Pulled By: cortinico

fbshipit-source-id: ef29bb54e930532a671adbe707be8d1a64ff0d82
2022-03-31 07:59:01 -07:00

112 lines
2.5 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
'use strict';
const FormData = require('../FormData');
describe('FormData', function () {
var formData;
beforeEach(() => {
formData = new FormData();
});
afterEach(() => {
formData = null;
});
it('should return non blob null', function () {
formData.append('null', null);
const expectedPart = {
string: 'null',
headers: {
'content-disposition': 'form-data; name="null"',
},
fieldName: 'null',
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});
it('should return blob', function () {
formData.append('photo', {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo.jpg',
});
const expectedPart = {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo.jpg',
headers: {
'content-disposition': 'form-data; name="photo"; filename="photo.jpg"',
'content-type': 'image/jpeg',
},
fieldName: 'photo',
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});
it('should return non blob array', function () {
formData.append('array', [
true,
false,
undefined,
null,
{},
[],
'string',
0,
]);
const expectedPart = {
string: 'true,false,,,[object Object],,string,0',
headers: {
'content-disposition': 'form-data; name="array"',
},
fieldName: 'array',
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});
it('should return values based on the given key', function () {
formData.append('username', 'Chris');
formData.append('username', 'Bob');
expect(formData.getAll('username').length).toBe(2);
expect(formData.getAll('username')).toMatchObject(['Chris', 'Bob']);
formData.append('photo', {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo3.jpg',
});
formData.append('photo', {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo2.jpg',
});
const expectedPart = {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo2.jpg',
};
expect(formData.getAll('photo')[1]).toMatchObject(expectedPart);
expect(formData.getAll('file').length).toBe(0);
});
});