Skip to content

Commit 5a87b62

Browse files
committed
test withRouteSpec correctly validates formData
1 parent 48859c3 commit 5a87b62

2 files changed

Lines changed: 42 additions & 33 deletions

File tree

apps/example-todo-app/tests/api/todo/form-add.test.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import test from "ava"
22
import { TODO_ID } from "tests/fixtures"
33
import getTestServer from "tests/fixtures/get-test-server"
44
import { v4 as uuidv4 } from "uuid"
5-
import { formData } from "pages/api/todo/form-add"
65
import { withRouteSpec } from "lib/middlewares"
7-
import * as z from "zod"
6+
import { z } from "zod"
87

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

29-
test("Workspace supports an optional object of formData", async (t) => {
30-
const { axios } = await getTestServer(t)
31-
32-
axios.defaults.headers.common.Authorization = `Bearer auth_token`
28+
test("Valid formData object passes validation", (t) => {
29+
const validFormData = {
30+
clear_sandbox_state: "clear_sandbox_state",
31+
};
3332

3433
withRouteSpec({
3534
auth: "support",
3635
methods: ["GET", "POST"],
3736
queryParams: z.object({
3837
workspace_id: z.string(),
3938
}),
40-
formData: z
41-
.object({
42-
clear_sandbox_state: z.literal("clear_sandbox_state"),
43-
})
44-
.optional(),
45-
} as const)
39+
formData: z.object({
40+
clear_sandbox_state: z.literal("clear_sandbox_state"),
41+
}),
42+
} as const);
4643

47-
const successfulRes = await axios({
48-
method: "POST",
49-
url: "/todo/form-add",
50-
data: formData,
51-
headers: {
52-
"Content-Type": "application/x-www-form-urlencoded",
53-
},
54-
}).catch((err) => err)
44+
const validationResult = z
45+
.object({
46+
clear_sandbox_state: z.literal("clear_sandbox_state"),
47+
})
48+
.safeParse(validFormData);
5549

56-
t.is(successfulRes.status, 200)
57-
})
50+
t.true(validationResult.success)
51+
});

yarn.lock

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -683,15 +683,10 @@
683683
dependencies:
684684
glob "7.1.7"
685685

686-
"@next/swc-linux-x64-gnu@12.2.0":
686+
"@next/swc-darwin-arm64@12.2.0":
687687
version "12.2.0"
688-
resolved "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-12.2.0.tgz"
689-
integrity sha512-MyhHbAKVjpn065WzRbqpLu2krj4kHLi6RITQdD1ee+uxq9r2yg5Qe02l24NxKW+1/lkmpusl4Y5Lks7rBiJn4w==
690-
691-
"@next/swc-linux-x64-musl@12.2.0":
692-
version "12.2.0"
693-
resolved "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-12.2.0.tgz"
694-
integrity sha512-Tz1tJZ5egE0S/UqCd5V6ZPJsdSzv/8aa7FkwFmIJ9neLS8/00za+OY5pq470iZQbPrkTwpKzmfTTIPRVD5iqDg==
688+
resolved "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-12.2.0.tgz"
689+
integrity sha512-x5U5gJd7ZvrEtTFnBld9O2bUlX8opu7mIQUqRzj7KeWzBwPhrIzTTsQXAiNqsaMuaRPvyHBVW/5d/6g6+89Y8g==
695690

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

1152+
"@swc/core-darwin-arm64@1.3.93":
1153+
version "1.3.93"
1154+
resolved "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.3.93.tgz"
1155+
integrity sha512-gEKgk7FVIgltnIfDO6GntyuQBBlAYg5imHpRgLxB1zSI27ijVVkksc6QwISzFZAhKYaBWIsFSVeL9AYSziAF7A==
1156+
11571157
"@swc/core@*", "@swc/core@^1", "@swc/core@^1.3.42":
11581158
version "1.3.93"
11591159
resolved "https://registry.npmjs.org/@swc/core/-/core-1.3.93.tgz"
@@ -2974,6 +2974,11 @@ es-to-primitive@^1.2.1:
29742974
is-date-object "^1.0.1"
29752975
is-symbol "^1.0.2"
29762976

2977+
esbuild-darwin-arm64@0.14.54:
2978+
version "0.14.54"
2979+
resolved "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.14.54.tgz"
2980+
integrity sha512-OPafJHD2oUPyvJMrsCvDGkRrVCar5aVyHfWGQzY1dWnzErjrDuSETxwA2HSsyg2jORLY8yBfzc1MIpUkXlctmw==
2981+
29772982
esbuild-register@^3.3.3:
29782983
version "3.5.0"
29792984
resolved "https://registry.npmjs.org/esbuild-register/-/esbuild-register-3.5.0.tgz"
@@ -3172,7 +3177,7 @@ eslint-plugin-jsx-a11y@^6.5.1:
31723177
object.fromentries "^2.0.6"
31733178
semver "^6.3.0"
31743179

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

3482-
"example-todo-app@file:/workspaces/nextlove/apps/example-todo-app":
3487+
"example-todo-app@file:/Users/pawelstepniak/Desktop/programming.nosync/seam/nextlove/apps/example-todo-app":
34833488
version "0.1.0"
34843489
resolved "file:apps/example-todo-app"
34853490
dependencies:
@@ -3734,6 +3739,11 @@ fs.realpath@^1.0.0:
37343739
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
37353740
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==
37363741

3742+
fsevents@^2.3.2, fsevents@~2.3.2:
3743+
version "2.3.3"
3744+
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
3745+
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==
3746+
37373747
function-bind@^1.1.1:
37383748
version "1.1.2"
37393749
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"
@@ -5956,7 +5966,7 @@ nextjs-server-modules@^2.1.0:
59565966
serve-handler "^6.1.3"
59575967
yargs "^17.3.1"
59585968

5959-
nextlove@*, "nextlove@file:/workspaces/nextlove/packages/nextlove":
5969+
nextlove@*, "nextlove@file:/Users/pawelstepniak/Desktop/programming.nosync/seam/nextlove/packages/nextlove":
59605970
version "2.8.2"
59615971
resolved "file:packages/nextlove"
59625972
dependencies:
@@ -8021,6 +8031,11 @@ tsutils@^3.21.0:
80218031
dependencies:
80228032
tslib "^1.8.1"
80238033

8034+
turbo-darwin-arm64@1.10.15:
8035+
version "1.10.15"
8036+
resolved "https://registry.npmjs.org/turbo-darwin-arm64/-/turbo-darwin-arm64-1.10.15.tgz"
8037+
integrity sha512-xwqyFDYUcl2xwXyGPmHkmgnNm4Cy0oNzMpMOBGRr5x64SErS7QQLR4VHb0ubiR+VAb8M+ECPklU6vD1Gm+wekg==
8038+
80248039
turbo@^1.3.1, turbo@latest:
80258040
version "1.10.15"
80268041
resolved "https://registry.npmjs.org/turbo/-/turbo-1.10.15.tgz"

0 commit comments

Comments
 (0)