-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(formData): refactor filterFormData + tests
- Loading branch information
Showing
4 changed files
with
33 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { filterFormData } from "../filterFormData"; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
describe("filterFormData", () => { | ||
it("creates a object with data", () => { | ||
const formData = new FormData(); | ||
formData.append("a", "1"); | ||
formData.append("b", "2"); | ||
expect(filterFormData(formData)).toEqual({ a: "1", b: "2" }); | ||
}); | ||
|
||
it("filters out entries starting with underscore", () => { | ||
const formData = new FormData(); | ||
formData.append("a", "1"); | ||
formData.append("_b", "2"); | ||
expect(filterFormData(formData)).toEqual({ a: "1" }); | ||
}); | ||
|
||
it("filters out repeated entries", () => { | ||
const formData = new FormData(); | ||
formData.append("a", "1"); | ||
formData.append("a", "2"); | ||
expect(filterFormData(formData)).toEqual({ a: "2" }); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const filterFormData = (formData: FormData) => | ||
// Note: fromEntries() reduces same-named form fields to the last one | ||
Object.fromEntries( | ||
Array.from(formData.entries()).filter(([key]) => !key.startsWith("_")), | ||
); |
Nice tests 👍