Skip to content

Commit

Permalink
test: add throwValidationErrors tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jun 20, 2024
1 parent a9bfa41 commit 75e92f5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,44 @@ test("typeschema - action with errors set via `returnValidationErrors` gives bac

assert.deepStrictEqual(actualResult, expectedResult);
});

// `throwValidationErrors` tests.

const tveac = createSafeActionClient({
throwValidationErrors: true,
});

test("typeschema - action with validation errors and `throwValidationErrors` option set to true in client throws", async () => {
const schema = z.object({
username: z.string().min(3),
password: z.string().min(3),
});

const action = tveac.schema(schema).action(async () => {
return {
ok: true,
};
});

assert.rejects(async () => await action({ username: "12", password: "34" }));
});

test("typeschema - action with server validation errors and `throwValidationErrors` option set to true in client throws", async () => {
const schema = z.object({
username: z.string().min(3),
password: z.string().min(3),
});

const action = tveac.schema(schema).action(async () => {
returnValidationErrors(schema, {
username: {
_errors: ["user_suspended"],
},
});
return {
ok: true,
};
});

assert.rejects(async () => await action({ username: "1234", password: "5678" }));
});
41 changes: 41 additions & 0 deletions packages/next-safe-action/src/__tests__/validation-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,44 @@ test("action with errors set via `returnValidationErrors` gives back an object w

assert.deepStrictEqual(actualResult, expectedResult);
});

// `throwValidationErrors` tests.

const tveac = createSafeActionClient({
throwValidationErrors: true,
});

test("action with validation errors and `throwValidationErrors` option set to true in client throws", async () => {
const schema = z.object({
username: z.string().min(3),
password: z.string().min(3),
});

const action = tveac.schema(schema).action(async () => {
return {
ok: true,
};
});

assert.rejects(async () => await action({ username: "12", password: "34" }));
});

test("action with server validation errors and `throwValidationErrors` option set to true in client throws", async () => {
const schema = z.object({
username: z.string().min(3),
password: z.string().min(3),
});

const action = tveac.schema(schema).action(async () => {
returnValidationErrors(schema, {
username: {
_errors: ["user_suspended"],
},
});
return {
ok: true,
};
});

assert.rejects(async () => await action({ username: "1234", password: "5678" }));
});

0 comments on commit 75e92f5

Please sign in to comment.