mirror of
https://github.com/axios/axios.git
synced 2026-04-11 02:11:50 +08:00
* chore: port karma tests * chore: port karma tests * chore: port karma tests * chore: tests * chore: tests * chore: tests * chore: fix issues with port collisions * refactor: utils tests * refactor: utils tests * refactor: utils tests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: ci * chore: install pw deps * chore: fixx ai feedback * chore: wip compatability tests * chore: wip compatability tests * chore: wip compatability tests * refactor: wip smoke * chore: smoke test run * chore: update unzip * chore: update testing * chore: update testing * chore: update testing * chore: update testing * chore: update testing * chore: skip tests that cannot run on node 16 and lower * chore: fix 16x under tests * chore: rest of tests * fix: functions and runs * feat: added tests for esm smoke * feat: added smoke * chore: ignore ai gen plans * chore: ci fixes * chore: fix small p2s
122 lines
3.4 KiB
JavaScript
122 lines
3.4 KiB
JavaScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import toFormData from '../../lib/helpers/toFormData.js';
|
|
|
|
describe('helpers::toFormData (vitest browser)', () => {
|
|
it('converts nested data object to FormData with dots option enabled', () => {
|
|
const data = {
|
|
val: 123,
|
|
nested: {
|
|
arr: ['hello', 'world'],
|
|
},
|
|
};
|
|
|
|
const form = toFormData(data, null, { dots: true });
|
|
|
|
expect(form).toBeInstanceOf(FormData);
|
|
expect(Array.from(form.keys())).toHaveLength(3);
|
|
expect(form.get('val')).toBe('123');
|
|
expect(form.get('nested.arr.0')).toBe('hello');
|
|
});
|
|
|
|
it('respects metaTokens option', () => {
|
|
const data = {
|
|
'obj{}': { x: 1, y: 2 },
|
|
};
|
|
const serialized = JSON.stringify(data['obj{}']);
|
|
|
|
const form = toFormData(data, null, { metaTokens: false });
|
|
|
|
expect(Array.from(form.keys())).toHaveLength(1);
|
|
expect(form.getAll('obj')).toEqual([serialized]);
|
|
});
|
|
|
|
describe('flat arrays serialization', () => {
|
|
it('includes full indexes when indexes option is true', () => {
|
|
const data = {
|
|
arr: [1, 2, 3],
|
|
arr2: [1, [2], 3],
|
|
};
|
|
|
|
const form = toFormData(data, null, { indexes: true });
|
|
|
|
expect(Array.from(form.keys())).toHaveLength(6);
|
|
expect(form.get('arr[0]')).toBe('1');
|
|
expect(form.get('arr[1]')).toBe('2');
|
|
expect(form.get('arr[2]')).toBe('3');
|
|
expect(form.get('arr2[0]')).toBe('1');
|
|
expect(form.get('arr2[1][0]')).toBe('2');
|
|
expect(form.get('arr2[2]')).toBe('3');
|
|
});
|
|
|
|
it('includes brackets only when indexes option is false', () => {
|
|
const data = {
|
|
arr: [1, 2, 3],
|
|
arr2: [1, [2], 3],
|
|
};
|
|
|
|
const form = toFormData(data, null, { indexes: false });
|
|
|
|
expect(Array.from(form.keys())).toHaveLength(6);
|
|
expect(form.getAll('arr[]')).toEqual(['1', '2', '3']);
|
|
expect(form.get('arr2[0]')).toBe('1');
|
|
expect(form.get('arr2[1][0]')).toBe('2');
|
|
expect(form.get('arr2[2]')).toBe('3');
|
|
});
|
|
|
|
it('omits brackets when indexes option is null', () => {
|
|
const data = {
|
|
arr: [1, 2, 3],
|
|
arr2: [1, [2], 3],
|
|
};
|
|
|
|
const form = toFormData(data, null, { indexes: null });
|
|
|
|
expect(Array.from(form.keys())).toHaveLength(6);
|
|
expect(form.getAll('arr')).toEqual(['1', '2', '3']);
|
|
expect(form.get('arr2[0]')).toBe('1');
|
|
expect(form.get('arr2[1][0]')).toBe('2');
|
|
expect(form.get('arr2[2]')).toBe('3');
|
|
});
|
|
});
|
|
|
|
it('converts nested data object to FormData', () => {
|
|
const data = {
|
|
val: 123,
|
|
nested: {
|
|
arr: ['hello', 'world'],
|
|
},
|
|
};
|
|
|
|
const form = toFormData(data);
|
|
|
|
expect(form).toBeInstanceOf(FormData);
|
|
expect(Array.from(form.keys())).toHaveLength(3);
|
|
expect(form.get('val')).toBe('123');
|
|
expect(form.get('nested[arr][0]')).toBe('hello');
|
|
});
|
|
|
|
it('appends value whose key ends with [] as separate values with the same key', () => {
|
|
const data = {
|
|
'arr[]': [1, 2, 3],
|
|
};
|
|
|
|
const form = toFormData(data);
|
|
|
|
expect(Array.from(form.keys())).toHaveLength(3);
|
|
expect(form.getAll('arr[]')).toEqual(['1', '2', '3']);
|
|
});
|
|
|
|
it('appends value whose key ends with {} as a JSON string', () => {
|
|
const data = {
|
|
'obj{}': { x: 1, y: 2 },
|
|
};
|
|
const serialized = JSON.stringify(data['obj{}']);
|
|
|
|
const form = toFormData(data);
|
|
|
|
expect(Array.from(form.keys())).toHaveLength(1);
|
|
expect(form.getAll('obj{}')).toEqual([serialized]);
|
|
});
|
|
});
|