Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for support for optional formData #123

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
31 changes: 18 additions & 13 deletions apps/example-todo-app/tests/api/todo/form-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import test from "ava"
import { TODO_ID } from "tests/fixtures"
import getTestServer from "tests/fixtures/get-test-server"
import { v4 as uuidv4 } from "uuid"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"

test("POST /todo/form-add", async (t) => {
Expand All @@ -25,27 +24,33 @@ test("POST /todo/form-add", async (t) => {
t.is(successfulRes.status, 200)
})

test("Valid formData object passes validation", (t) => {
test("Valid formData object passes validation and returns successful response", async (t) => {
const { axios } = await getTestServer(t)

axios.defaults.headers.common.Authorization = `Bearer auth_token`

const bodyFormData = new URLSearchParams()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const bodyFormData = new URLSearchParams()
const bodyFormData = new FormData()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like FormData isn't supported. Seve created an issue to implement support for that: #127

bodyFormData.append("title", "clear_sandbox_state")

const successfulRes = await axios({
method: "POST",
url: "/todo/form-add",
data: bodyFormData,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
}).catch((err) => err)
kainpets marked this conversation as resolved.
Show resolved Hide resolved

const validFormData = {
clear_sandbox_state: "clear_sandbox_state",
}

withRouteSpec({
auth: "support",
methods: ["GET", "POST"],
queryParams: z.object({
workspace_id: z.string(),
}),
formData: z.object({
clear_sandbox_state: z.literal("clear_sandbox_state"),
}),
} as const)

const validationResult = z
.object({
clear_sandbox_state: z.literal("clear_sandbox_state"),
})
.safeParse(validFormData)

t.true(validationResult.success)
t.is(successfulRes.status, 200)
})
Loading