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
17 changes: 17 additions & 0 deletions apps/example-todo-app/pages/api/todo/form-add-with-default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { withRouteSpec, checkRouteSpec } from "lib/middlewares"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"
import { formData } from "./form-add"

export const route_spec = checkRouteSpec({
methods: ["POST"],
auth: "auth_token",
formData,
jsonResponse: z.object({
ok: z.boolean(),
}),
})

export default withRouteSpec(route_spec)(async (req, res) => {
return res.status(200).json({ ok: true })
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from "ava"
import getTestServer from "tests/fixtures/get-test-server"

test("POST /todo/form-add-with-default with optional form data", async (t) => {
const { axios } = await getTestServer(t)

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

const bodyFormData = {
id: "test-id",
completed: true,
}

const resWithFormData = await axios({
method: "POST",
url: "/todo/form-add-with-default",
data: bodyFormData,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
}).catch((err) => err)

t.is(resWithFormData.status, 200)

const resWithoutData = await axios({
method: "POST",
url: "/todo/form-add-with-default",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
}).catch((err) => err)

t.is(resWithoutData.status, 200)
})
3 changes: 2 additions & 1 deletion 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,8 @@ 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 { formData } from "pages/api/todo/form-add"
import { z } from "zod"
import qs from "qs"

test("POST /todo/form-add", async (t) => {
const { axios } = await getTestServer(t)
Expand Down
Loading
Loading