mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Added axios.formToJSON method; (#4735)
* Draft * Added `formDataToJSON` helper; Added `axios.formToJSON` method; Added client tests; Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
var formDataToJSON = require('../../../lib/helpers/formDataToJSON');
|
||||
|
||||
describe('formDataToJSON', function () {
|
||||
it('should convert a FormData Object to JSON Object', function () {
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('foo[bar][baz]', '123');
|
||||
|
||||
expect(formDataToJSON(formData)).toEqual({
|
||||
foo: {
|
||||
bar: {
|
||||
baz: '123'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should convert repeatable values as an array', function () {
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('foo', '1');
|
||||
formData.append('foo', '2');
|
||||
|
||||
expect(formDataToJSON(formData)).toEqual({
|
||||
foo: ['1', '2']
|
||||
});
|
||||
});
|
||||
|
||||
it('should convert props with empty brackets to arrays', function () {
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('foo[]', '1');
|
||||
formData.append('foo[]', '2');
|
||||
|
||||
expect(formDataToJSON(formData)).toEqual({
|
||||
foo: ['1', '2']
|
||||
});
|
||||
});
|
||||
|
||||
it('should supported indexed arrays', function () {
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('foo[0]', '1');
|
||||
formData.append('foo[1]', '2');
|
||||
|
||||
expect(formDataToJSON(formData)).toEqual({
|
||||
foo: ['1', '2']
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,8 @@ describe('instance', function () {
|
||||
'isAxiosError',
|
||||
'VERSION',
|
||||
'default',
|
||||
'toFormData'
|
||||
'toFormData',
|
||||
'formToJSON'
|
||||
].indexOf(prop) > -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user