Skip to content

Commit

Permalink
test withRouteSpec correctly validates formData
Browse files Browse the repository at this point in the history
  • Loading branch information
kainpets committed Nov 13, 2023
1 parent 48859c3 commit 5a87b62
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
38 changes: 16 additions & 22 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,9 +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 { withRouteSpec } from "lib/middlewares"
import * as z from "zod"
import { z } from "zod"

test("POST /todo/form-add", async (t) => {
const { axios } = await getTestServer(t)
Expand All @@ -26,32 +25,27 @@ test("POST /todo/form-add", async (t) => {
t.is(successfulRes.status, 200)
})

test("Workspace supports an optional object of formData", async (t) => {
const { axios } = await getTestServer(t)

axios.defaults.headers.common.Authorization = `Bearer auth_token`
test("Valid formData object passes validation", (t) => {
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"),
})
.optional(),
} as const)
formData: z.object({
clear_sandbox_state: z.literal("clear_sandbox_state"),
}),
} as const);

const successfulRes = await axios({
method: "POST",
url: "/todo/form-add",
data: formData,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
}).catch((err) => err)
const validationResult = z
.object({
clear_sandbox_state: z.literal("clear_sandbox_state"),
})
.safeParse(validFormData);

t.is(successfulRes.status, 200)
})
t.true(validationResult.success)
});
37 changes: 26 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -683,15 +683,10 @@
dependencies:
glob "7.1.7"

"@next/swc-linux-x64-gnu@12.2.0":
"@next/swc-darwin-arm64@12.2.0":
version "12.2.0"
resolved "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-12.2.0.tgz"
integrity sha512-MyhHbAKVjpn065WzRbqpLu2krj4kHLi6RITQdD1ee+uxq9r2yg5Qe02l24NxKW+1/lkmpusl4Y5Lks7rBiJn4w==

"@next/swc-linux-x64-musl@12.2.0":
version "12.2.0"
resolved "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-12.2.0.tgz"
integrity sha512-Tz1tJZ5egE0S/UqCd5V6ZPJsdSzv/8aa7FkwFmIJ9neLS8/00za+OY5pq470iZQbPrkTwpKzmfTTIPRVD5iqDg==
resolved "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-12.2.0.tgz"
integrity sha512-x5U5gJd7ZvrEtTFnBld9O2bUlX8opu7mIQUqRzj7KeWzBwPhrIzTTsQXAiNqsaMuaRPvyHBVW/5d/6g6+89Y8g==

"@nodelib/fs.scandir@2.1.5":
version "2.1.5"
Expand Down Expand Up @@ -1154,6 +1149,11 @@
resolved "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.2.tgz"
integrity sha512-sXXKG+uL9IrKqViTtao2Ws6dy0znu9sOaP1di/jKGW1M6VssO8vlpXCQcpZ+jisQ1tTFAC5Jo/EOzFbggBagFQ==

"@swc/core-darwin-arm64@1.3.93":
version "1.3.93"
resolved "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.3.93.tgz"
integrity sha512-gEKgk7FVIgltnIfDO6GntyuQBBlAYg5imHpRgLxB1zSI27ijVVkksc6QwISzFZAhKYaBWIsFSVeL9AYSziAF7A==

"@swc/core@*", "@swc/core@^1", "@swc/core@^1.3.42":
version "1.3.93"
resolved "https://registry.npmjs.org/@swc/core/-/core-1.3.93.tgz"
Expand Down Expand Up @@ -2974,6 +2974,11 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

esbuild-darwin-arm64@0.14.54:
version "0.14.54"
resolved "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.14.54.tgz"
integrity sha512-OPafJHD2oUPyvJMrsCvDGkRrVCar5aVyHfWGQzY1dWnzErjrDuSETxwA2HSsyg2jORLY8yBfzc1MIpUkXlctmw==

esbuild-register@^3.3.3:
version "3.5.0"
resolved "https://registry.npmjs.org/esbuild-register/-/esbuild-register-3.5.0.tgz"
Expand Down Expand Up @@ -3172,7 +3177,7 @@ eslint-plugin-jsx-a11y@^6.5.1:
object.fromentries "^2.0.6"
semver "^6.3.0"

eslint-plugin-nextlove@*, "eslint-plugin-nextlove@file:/workspaces/nextlove/packages/eslint-plugin":
eslint-plugin-nextlove@*, "eslint-plugin-nextlove@file:/Users/pawelstepniak/Desktop/programming.nosync/seam/nextlove/packages/eslint-plugin":
version "0.0.1"
resolved "file:packages/eslint-plugin"
dependencies:
Expand Down Expand Up @@ -3479,7 +3484,7 @@ esutils@^2.0.2, esutils@^2.0.3:
resolved "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

"example-todo-app@file:/workspaces/nextlove/apps/example-todo-app":
"example-todo-app@file:/Users/pawelstepniak/Desktop/programming.nosync/seam/nextlove/apps/example-todo-app":
version "0.1.0"
resolved "file:apps/example-todo-app"
dependencies:
Expand Down Expand Up @@ -3734,6 +3739,11 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==

fsevents@^2.3.2, fsevents@~2.3.2:
version "2.3.3"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

function-bind@^1.1.1:
version "1.1.2"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"
Expand Down Expand Up @@ -5956,7 +5966,7 @@ nextjs-server-modules@^2.1.0:
serve-handler "^6.1.3"
yargs "^17.3.1"

nextlove@*, "nextlove@file:/workspaces/nextlove/packages/nextlove":
nextlove@*, "nextlove@file:/Users/pawelstepniak/Desktop/programming.nosync/seam/nextlove/packages/nextlove":
version "2.8.2"
resolved "file:packages/nextlove"
dependencies:
Expand Down Expand Up @@ -8021,6 +8031,11 @@ tsutils@^3.21.0:
dependencies:
tslib "^1.8.1"

turbo-darwin-arm64@1.10.15:
version "1.10.15"
resolved "https://registry.npmjs.org/turbo-darwin-arm64/-/turbo-darwin-arm64-1.10.15.tgz"
integrity sha512-xwqyFDYUcl2xwXyGPmHkmgnNm4Cy0oNzMpMOBGRr5x64SErS7QQLR4VHb0ubiR+VAb8M+ECPklU6vD1Gm+wekg==

turbo@^1.3.1, turbo@latest:
version "1.10.15"
resolved "https://registry.npmjs.org/turbo/-/turbo-1.10.15.tgz"
Expand Down

0 comments on commit 5a87b62

Please sign in to comment.