mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-23 02:45:21 -05:00
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
import { z } from "zod";
|
|
import { logger } from "@formbricks/logger";
|
|
import { ValidationError } from "@formbricks/types/errors";
|
|
import { validateInputs } from "./validate";
|
|
|
|
vi.mock("@formbricks/logger", () => ({
|
|
logger: {
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("validateInputs", () => {
|
|
test("validates inputs successfully", () => {
|
|
const schema = z.string();
|
|
const result = validateInputs(["valid", schema]);
|
|
|
|
expect(result).toEqual(["valid"]);
|
|
});
|
|
|
|
test("throws ValidationError for invalid inputs", () => {
|
|
const schema = z.string();
|
|
|
|
expect(() => validateInputs([123, schema])).toThrow(ValidationError);
|
|
expect(logger.error).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
error: expect.any(z.ZodError),
|
|
issues: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
message: "Invalid input: expected string, received number",
|
|
}),
|
|
]),
|
|
valuePreview: "123",
|
|
}),
|
|
"Input validation failed"
|
|
);
|
|
});
|
|
|
|
test("validates multiple inputs successfully", () => {
|
|
const stringSchema = z.string();
|
|
const numberSchema = z.number();
|
|
|
|
const result = validateInputs(["valid", stringSchema], [42, numberSchema]);
|
|
|
|
expect(result).toEqual(["valid", 42]);
|
|
});
|
|
|
|
test("throws ValidationError for one of multiple invalid inputs", () => {
|
|
const stringSchema = z.string();
|
|
const numberSchema = z.number();
|
|
|
|
expect(() => validateInputs(["valid", stringSchema], ["invalid", numberSchema])).toThrow(ValidationError);
|
|
expect(logger.error).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
error: expect.any(z.ZodError),
|
|
issues: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
message: "Invalid input: expected number, received string",
|
|
}),
|
|
]),
|
|
valuePreview: '"invalid"',
|
|
}),
|
|
"Input validation failed"
|
|
);
|
|
});
|
|
});
|