Skip to content

Commit 12d1d45

Browse files
committed
fix: classToFormData to use nested arrays in formdata and enable array assertions in tests
1 parent 300f4a3 commit 12d1d45

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/utils/ClassToFormData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const classToFormData = (obj: object, formPrefix?: string, formData?: For
5151
prefix += `[${index.toString()}]`;
5252
}
5353

54-
classToFormData(value, prefix, formData);
54+
classToFormData(value, prefix, newFormData);
5555
} else {
5656
// eslint-disable-next-line @typescript-eslint/no-base-to-string
5757
newFormData.append(name, String(value));

tests/formdata-convertations.spec.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
/* eslint-disable max-lines-per-function */
21
/* eslint-disable max-statements */
32

43
import("reflect-metadata"); // Polyfill
54
import {assert} from "chai";
65
import {describe, it} from "node:test";
7-
8-
// Import type {Friend as IFriend} from "./models/User";
6+
import type {Friend as IFriend} from "./models/User";
97

108

119
describe("FormData convertation", () => {
@@ -41,22 +39,17 @@ describe("FormData convertation", () => {
4139
assert.strictEqual(user.greeting, formData.get("greeting"), "greeting is not equal");
4240
assert.strictEqual(user.favoriteFruit, formData.get("favoriteFruit"), "favoriteFruit is not equal");
4341

44-
/*
45-
* Nodejs version of FormData don't support arrays of objects, but C# and browser version of FormData support him.
46-
* Because test run under nodejs env, this next part disabled. Check this code in future versions of nodejs.
47-
*
48-
* user.friends.forEach((friend: IFriend, index: number) => {
49-
* assert.strictEqual(
50-
* String(friend.id),
51-
* formData.get(`friends[${index.toString()}].id`),
52-
* `friend ${String(index)} id is not equal`
53-
* );
54-
* assert.strictEqual(
55-
* friend.name,
56-
* formData.get(`friends[${index.toString()}].name`),
57-
* `friend ${String(index)} name is not equal`
58-
* );
59-
* });
60-
*/
42+
user.friends.forEach((friend: IFriend, index: number) => {
43+
assert.strictEqual(
44+
String(friend.id),
45+
formData.get(`friends[${index.toString()}].id`),
46+
`friend ${String(index)} id is not equal`
47+
);
48+
assert.strictEqual(
49+
friend.name,
50+
formData.get(`friends[${index.toString()}].name`),
51+
`friend ${String(index)} name is not equal`
52+
);
53+
});
6154
});
6255
});

0 commit comments

Comments
 (0)