|
1 |
| -import { createFormData, mergeErrors, parseFormData } from "./index"; |
| 1 | +import { |
| 2 | + createFormData, |
| 3 | + generateFormData, |
| 4 | + mergeErrors, |
| 5 | + parseFormData, |
| 6 | +} from "./index"; |
2 | 7 |
|
3 | 8 | describe("createFormData", () => {
|
4 | 9 | it("should create a FormData object with the provided data", () => {
|
@@ -55,13 +60,37 @@ describe("parseFormData", () => {
|
55 | 60 | expect(parsedData).toEqual(data);
|
56 | 61 | });
|
57 | 62 |
|
58 |
| - it("should throw an error if no data is found for the specified key", async () => { |
| 63 | + it("should return an empty object if no formData exists", async () => { |
59 | 64 | const request = new Request("http://localhost:3000");
|
60 | 65 | const requestFormDataSpy = vi.spyOn(request, "formData");
|
61 | 66 | requestFormDataSpy.mockResolvedValueOnce(createFormData({}));
|
62 |
| - await expect(parseFormData(request, "randomKey")).rejects.toThrow( |
63 |
| - "No data found" |
64 |
| - ); |
| 67 | + const parsedData = await parseFormData(request); |
| 68 | + expect(parsedData).toEqual({}); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should return formData if NO js was used and formData was passed as is", async () => { |
| 72 | + const formData = new FormData(); |
| 73 | + formData.append("name", "John Doe"); |
| 74 | + formData.append("age", "30"); |
| 75 | + formData.append("hobbies.0", "Reading"); |
| 76 | + formData.append("hobbies.1", "Writing"); |
| 77 | + formData.append("hobbies.2", "Coding"); |
| 78 | + formData.append("other.skills.0", "testing"); |
| 79 | + formData.append("other.skills.1", "testing"); |
| 80 | + formData.append("other.something", "else"); |
| 81 | + const request = new Request("http://localhost:3000"); |
| 82 | + const requestFormDataSpy = vi.spyOn(request, "formData"); |
| 83 | + requestFormDataSpy.mockResolvedValueOnce(formData); |
| 84 | + const parsedData = await parseFormData(request); |
| 85 | + expect(parsedData).toEqual({ |
| 86 | + name: "John Doe", |
| 87 | + age: "30", |
| 88 | + hobbies: ["Reading", "Writing", "Coding"], |
| 89 | + other: { |
| 90 | + skills: ["testing", "testing"], |
| 91 | + something: "else", |
| 92 | + }, |
| 93 | + }); |
65 | 94 | });
|
66 | 95 |
|
67 | 96 | it("should throw an error if the retrieved data is not a string (but a file instead)", async () => {
|
@@ -132,3 +161,52 @@ describe("mergeErrors", () => {
|
132 | 161 | expect(mergedErrors).toEqual(expectedErrors);
|
133 | 162 | });
|
134 | 163 | });
|
| 164 | + |
| 165 | +describe("generateFormData", () => { |
| 166 | + it("should generate an output object for flat form data", () => { |
| 167 | + const formData = new FormData(); |
| 168 | + formData.append("name", "John Doe"); |
| 169 | + formData.append("email", "[email protected]"); |
| 170 | + |
| 171 | + const expectedOutput = { |
| 172 | + name: "John Doe", |
| 173 | + |
| 174 | + }; |
| 175 | + |
| 176 | + expect(generateFormData(formData)).toEqual(expectedOutput); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should generate an output object for nested form data", () => { |
| 180 | + const formData = new FormData(); |
| 181 | + formData.append("user.name.first", "John"); |
| 182 | + formData.append("user.name.last", "Doe"); |
| 183 | + formData.append("user.email", "[email protected]"); |
| 184 | + |
| 185 | + const expectedOutput = { |
| 186 | + user: { |
| 187 | + name: { |
| 188 | + first: "John", |
| 189 | + last: "Doe", |
| 190 | + }, |
| 191 | + |
| 192 | + }, |
| 193 | + }; |
| 194 | + |
| 195 | + expect(generateFormData(formData)).toEqual(expectedOutput); |
| 196 | + }); |
| 197 | + |
| 198 | + it("should generate an output object with arrays for integer indexes", () => { |
| 199 | + const formData = new FormData(); |
| 200 | + formData.append("user.roles.0", "admin"); |
| 201 | + formData.append("user.roles.1", "editor"); |
| 202 | + formData.append("user.roles.2", "contributor"); |
| 203 | + |
| 204 | + const expectedOutput = { |
| 205 | + user: { |
| 206 | + roles: ["admin", "editor", "contributor"], |
| 207 | + }, |
| 208 | + }; |
| 209 | + |
| 210 | + expect(generateFormData(formData)).toEqual(expectedOutput); |
| 211 | + }); |
| 212 | +}); |
0 commit comments