feat: adds unit tests

This commit is contained in:
Piyush Gupta
2024-09-13 11:52:40 +05:30
parent 15aa9b2731
commit c7e0b02595
2 changed files with 293 additions and 0 deletions
@@ -290,3 +290,166 @@ export const mockTransformedSurveyOutput = {
export const mockTransformedSyncSurveyOutput = {
...mockSyncSurveyOutput,
};
export const mockSurveyWithLogic: TSurvey = {
...mockSyncSurveyOutput,
...baseSurveyProperties,
displayPercentage: null,
segment: null,
type: "link",
endings: [],
questions: [
{
id: "q1",
type: TSurveyQuestionTypeEnum.OpenText,
inputType: "text",
headline: { default: "What is your favorite color?" },
required: true,
logic: [
{
id: "cdu9vgtmmd9b24l35pp9bodk",
conditions: {
id: "d21qg6x5fk65pf592jys5rcz",
connector: "and",
conditions: [
{
id: "swlje0bsnh6lkyk8vqs13oyr",
leftOperand: { type: "question", value: "q1" },
operator: "equals",
rightOperand: { type: "static", value: "blue" },
},
],
},
actions: [],
},
],
},
{
id: "q2",
type: TSurveyQuestionTypeEnum.OpenText,
inputType: "text",
headline: { default: "What is your favorite food?" },
required: true,
logic: [
{
id: "uwlm6kazj5pbt6licpa1hw5c",
conditions: {
id: "cvqxpbjydwktz4f9mvit2i11",
connector: "and",
conditions: [
{
id: "n74oght3ozqgwm9rifp2fxrr",
leftOperand: { type: "question", value: "q1" },
operator: "equals",
rightOperand: { type: "static", value: "blue" },
},
{
id: "fg4c9dwt9qjy8aba7zxbfdqd",
leftOperand: { type: "question", value: "q2" },
operator: "equals",
rightOperand: { type: "static", value: "pizza" },
},
],
},
actions: [],
},
],
},
{
id: "q3",
type: TSurveyQuestionTypeEnum.OpenText,
inputType: "text",
headline: { default: "What is your favorite movie?" },
required: true,
logic: [
{
id: "dpi3zipezuo1idplztb1abes",
conditions: {
id: "h3tp53lf8lri4pjcqc1xz3d8",
connector: "or",
conditions: [
{
id: "tmj7p9d3kpz1v4mcgpguqytw",
leftOperand: { type: "question", value: "q2" },
operator: "equals",
rightOperand: { type: "static", value: "pizza" },
},
{
id: "rs7v5mmoetff7x8lo1gdsgpr",
leftOperand: { type: "question", value: "q3" },
operator: "equals",
rightOperand: { type: "static", value: "Inception" },
},
],
},
actions: [],
},
],
},
{
id: "q4",
type: TSurveyQuestionTypeEnum.MultipleChoiceSingle,
headline: { default: "Select a number:" },
choices: [
{ id: "mvedaklp0gxxycprpyhhwen7", label: { default: "lol" } },
{ id: "i7ws8uqyj66q5x086vbqtm8n", label: { default: "lmao" } },
{ id: "cy8hbbr9e2q6ywbfjbzwdsqn", label: { default: "XD" } },
{ id: "sojc5wwxc5gxrnuib30w7t6s", label: { default: "hehe" } },
],
required: true,
logic: [
{
id: "fbim31ttxe1s7qkrjzkj1mtc",
conditions: {
id: "db44yagvr140wahafu0n11x6",
connector: "and",
conditions: [
{
id: "ddhaccfqy7rr3d5jdswl8yl8",
leftOperand: { type: "variable", value: "siog1dabtpo3l0a3xoxw2922" },
operator: "equals",
rightOperand: { type: "question", value: "q4" },
},
],
},
actions: [],
},
],
},
{
id: "q5",
type: TSurveyQuestionTypeEnum.OpenText,
inputType: "number",
headline: { default: "Select your age group:" },
required: true,
logic: [
{
id: "o6n73uq9rysih9mpcbzlehfs",
conditions: {
id: "szdkmtz17j9008n4i2d1t040",
connector: "and",
conditions: [
{
id: "rb223vmzuuzo3ag1bp2m3i69",
leftOperand: { type: "variable", value: "km1srr55owtn2r7lkoh5ny1u" },
operator: "isGreaterThan",
rightOperand: { type: "static", value: 30 },
},
{
id: "ot894j7nwna24i6jo2zpk59o",
leftOperand: { type: "variable", value: "km1srr55owtn2r7lkoh5ny1u" },
operator: "isLessThan",
rightOperand: { type: "question", value: "q5" },
},
],
},
actions: [],
},
],
},
],
variables: [
{ id: "siog1dabtpo3l0a3xoxw2922", type: "text", name: "var1", value: "lmao" },
{ id: "km1srr55owtn2r7lkoh5ny1u", type: "number", name: "var2", value: 32 },
],
};
+130
View File
@@ -1,5 +1,6 @@
import { prisma } from "../../__mocks__/database";
import { Prisma } from "@prisma/client";
import { evaluateAdvancedLogic } from "utils/evaluateLogic";
import { beforeEach, describe, expect, it } from "vitest";
import { testInputValidation } from "vitestSetup";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/errors";
@@ -25,6 +26,7 @@ import {
mockPrismaPerson,
mockProduct,
mockSurveyOutput,
mockSurveyWithLogic,
mockSyncSurveyOutput,
mockTransformedSurveyOutput,
mockTransformedSyncSurveyOutput,
@@ -36,6 +38,134 @@ beforeEach(() => {
prisma.survey.count.mockResolvedValue(1);
});
describe("evaluateAdvancedLogic with mockSurveyWithLogic", () => {
it("should return true when q1 answer is blue", () => {
const data = { q1: "blue" };
const variablesData = {};
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[0].logic![0].conditions,
"default"
);
expect(result).toBe(true);
});
it("should return false when q1 answer is not blue", () => {
const data = { q1: "red" };
const variablesData = {};
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[0].logic![0].conditions,
"default"
);
expect(result).toBe(false);
});
it("should return true when q1 is blue and q2 is pizza", () => {
const data = { q1: "blue", q2: "pizza" };
const variablesData = {};
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[1].logic![0].conditions,
"default"
);
expect(result).toBe(true);
});
it("should return false when q1 is blue but q2 is not pizza", () => {
const data = { q1: "blue", q2: "burger" };
const variablesData = {};
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[1].logic![0].conditions,
"default"
);
expect(result).toBe(false);
});
it("should return true when q2 is pizza or q3 is Inception", () => {
const data = { q2: "pizza", q3: "Inception" };
const variablesData = {};
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[2].logic![0].conditions,
"default"
);
expect(result).toBe(true);
});
it("should return true when var1 is equal to single select question value", () => {
const data = { q4: "lmao" };
const variablesData = { siog1dabtpo3l0a3xoxw2922: "lmao" };
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[3].logic![0].conditions,
"default"
);
expect(result).toBe(true);
});
it("should return false when var1 is not equal to single select question value", () => {
const data = { q4: "lol" };
const variablesData = { siog1dabtpo3l0a3xoxw2922: "damn" };
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[3].logic![0].conditions,
"default"
);
expect(result).toBe(false);
});
it("should return true when var2 is greater than 30 and less than open text number value", () => {
const data = { q5: "40" };
const variablesData = { km1srr55owtn2r7lkoh5ny1u: 35 };
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[4].logic![0].conditions,
"default"
);
expect(result).toBe(true);
});
it("should return false when var2 is not greater than 30 or greater than open text number value", () => {
const data = { q5: "40" };
const variablesData = { km1srr55owtn2r7lkoh5ny1u: 25 };
const result = evaluateAdvancedLogic(
mockSurveyWithLogic,
data,
variablesData,
mockSurveyWithLogic.questions[4].logic![0].conditions,
"default"
);
expect(result).toBe(false);
});
});
describe("Tests for getSurvey", () => {
describe("Happy Path", () => {
it("Returns a survey", async () => {