Skip to content

Commit a5e687e

Browse files
committed
test: add tests for action throwValidationErrors and throwServerError utils
1 parent 2e6aaf9 commit a5e687e

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

packages/next-safe-action/src/__tests__/server-error.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ test("known error occurred in middleware function is unmasked", async () => {
9393
assert.deepStrictEqual(actualResult, expectedResult);
9494
});
9595

96+
test("error occurred with `throwServerError` set to true at the action level throws", async () => {
97+
const action = ac1.action(
98+
async () => {
99+
throw new Error("Something bad happened");
100+
},
101+
{ throwServerError: true }
102+
);
103+
104+
assert.rejects(async () => await action());
105+
});
106+
96107
// Server error is an object with a 'message' property.
97108
const ac2 = createSafeActionClient({
98109
validationAdapter: zodAdapter(),

packages/next-safe-action/src/__tests__/validation-errors.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,25 @@ test("action with errors set via `returnValidationErrors` gives back an object w
541541

542542
// `throwValidationErrors` tests.
543543

544+
// test without `throwValidationErrors` set at the instance level, just set at the action level.
545+
test("action with validation errors and `throwValidationErrors` option set to true at the action level throws", async () => {
546+
const schema = z.object({
547+
username: z.string().min(3),
548+
password: z.string().min(3),
549+
});
550+
551+
const action = dac.schema(schema).action(
552+
async () => {
553+
return {
554+
ok: true,
555+
};
556+
},
557+
{ throwValidationErrors: true }
558+
);
559+
560+
assert.rejects(async () => await action({ username: "12", password: "34" }));
561+
});
562+
544563
const tveac = createSafeActionClient({
545564
validationAdapter: zodAdapter(),
546565
throwValidationErrors: true,
@@ -580,3 +599,78 @@ test("action with server validation errors and `throwValidationErrors` option se
580599

581600
assert.rejects(async () => await action({ username: "1234", password: "5678" }));
582601
});
602+
603+
test("action with validation errors and `throwValidationErrors` option set to true both in client and action throws", async () => {
604+
const schema = z.object({
605+
username: z.string().min(3),
606+
password: z.string().min(3),
607+
});
608+
609+
const action = tveac.schema(schema).action(
610+
async () => {
611+
return {
612+
ok: true,
613+
};
614+
},
615+
{ throwValidationErrors: true }
616+
);
617+
618+
assert.rejects(async () => await action({ username: "12", password: "34" }));
619+
});
620+
621+
test("action with validation errors and overridden `throwValidationErrors` set to false at the action level doesn't throw", async () => {
622+
const schema = z.object({
623+
user: z.object({
624+
id: z.string().min(36).uuid(),
625+
}),
626+
store: z.object({
627+
id: z.string().min(36).uuid(),
628+
product: z.object({
629+
id: z.string().min(36).uuid(),
630+
}),
631+
}),
632+
});
633+
634+
const action = tveac.schema(schema).action(
635+
async () => {
636+
return {
637+
ok: true,
638+
};
639+
},
640+
{ throwValidationErrors: false }
641+
);
642+
643+
const actualResult = await action({
644+
user: {
645+
id: "invalid_uuid",
646+
},
647+
store: {
648+
id: "invalid_uuid",
649+
product: {
650+
id: "invalid_uuid",
651+
},
652+
},
653+
});
654+
655+
const expectedResult = {
656+
validationErrors: {
657+
user: {
658+
id: {
659+
_errors: ["String must contain at least 36 character(s)", "Invalid uuid"],
660+
},
661+
},
662+
store: {
663+
id: {
664+
_errors: ["String must contain at least 36 character(s)", "Invalid uuid"],
665+
},
666+
product: {
667+
id: {
668+
_errors: ["String must contain at least 36 character(s)", "Invalid uuid"],
669+
},
670+
},
671+
},
672+
},
673+
};
674+
675+
assert.deepStrictEqual(actualResult, expectedResult);
676+
});

0 commit comments

Comments
 (0)