forked from forge-42/remix-hook-form
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cb2a31
commit 616fad3
Showing
5 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
export * from "./utilities"; | ||
export { | ||
parseFormData, | ||
createFormData, | ||
getValidatedFormData, | ||
validateFormData, | ||
} from "./utilities"; | ||
export * from "./hook"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { createFormData, mergeErrors, parseFormData } from "./index"; | ||
import { | ||
createFormData, | ||
generateFormData, | ||
mergeErrors, | ||
parseFormData, | ||
} from "./index"; | ||
|
||
describe("createFormData", () => { | ||
it("should create a FormData object with the provided data", () => { | ||
|
@@ -55,13 +60,37 @@ describe("parseFormData", () => { | |
expect(parsedData).toEqual(data); | ||
}); | ||
|
||
it("should throw an error if no data is found for the specified key", async () => { | ||
it("should return an empty object if no formData exists", async () => { | ||
const request = new Request("http://localhost:3000"); | ||
const requestFormDataSpy = vi.spyOn(request, "formData"); | ||
requestFormDataSpy.mockResolvedValueOnce(createFormData({})); | ||
await expect(parseFormData(request, "randomKey")).rejects.toThrow( | ||
"No data found" | ||
); | ||
const parsedData = await parseFormData(request); | ||
expect(parsedData).toEqual({}); | ||
}); | ||
|
||
it("should return formData if NO js was used and formData was passed as is", async () => { | ||
const formData = new FormData(); | ||
formData.append("name", "John Doe"); | ||
formData.append("age", "30"); | ||
formData.append("hobbies.0", "Reading"); | ||
formData.append("hobbies.1", "Writing"); | ||
formData.append("hobbies.2", "Coding"); | ||
formData.append("other.skills.0", "testing"); | ||
formData.append("other.skills.1", "testing"); | ||
formData.append("other.something", "else"); | ||
const request = new Request("http://localhost:3000"); | ||
const requestFormDataSpy = vi.spyOn(request, "formData"); | ||
requestFormDataSpy.mockResolvedValueOnce(formData); | ||
const parsedData = await parseFormData(request); | ||
expect(parsedData).toEqual({ | ||
name: "John Doe", | ||
age: "30", | ||
hobbies: ["Reading", "Writing", "Coding"], | ||
other: { | ||
skills: ["testing", "testing"], | ||
something: "else", | ||
}, | ||
}); | ||
}); | ||
|
||
it("should throw an error if the retrieved data is not a string (but a file instead)", async () => { | ||
|
@@ -132,3 +161,52 @@ describe("mergeErrors", () => { | |
expect(mergedErrors).toEqual(expectedErrors); | ||
}); | ||
}); | ||
|
||
describe("generateFormData", () => { | ||
it("should generate an output object for flat form data", () => { | ||
const formData = new FormData(); | ||
formData.append("name", "John Doe"); | ||
formData.append("email", "[email protected]"); | ||
|
||
const expectedOutput = { | ||
name: "John Doe", | ||
email: "[email protected]", | ||
}; | ||
|
||
expect(generateFormData(formData)).toEqual(expectedOutput); | ||
}); | ||
|
||
it("should generate an output object for nested form data", () => { | ||
const formData = new FormData(); | ||
formData.append("user.name.first", "John"); | ||
formData.append("user.name.last", "Doe"); | ||
formData.append("user.email", "[email protected]"); | ||
|
||
const expectedOutput = { | ||
user: { | ||
name: { | ||
first: "John", | ||
last: "Doe", | ||
}, | ||
email: "[email protected]", | ||
}, | ||
}; | ||
|
||
expect(generateFormData(formData)).toEqual(expectedOutput); | ||
}); | ||
|
||
it("should generate an output object with arrays for integer indexes", () => { | ||
const formData = new FormData(); | ||
formData.append("user.roles.0", "admin"); | ||
formData.append("user.roles.1", "editor"); | ||
formData.append("user.roles.2", "contributor"); | ||
|
||
const expectedOutput = { | ||
user: { | ||
roles: ["admin", "editor", "contributor"], | ||
}, | ||
}; | ||
|
||
expect(generateFormData(formData)).toEqual(expectedOutput); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters