Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evadecker committed Dec 2, 2024
1 parent 690973a commit 6f7427b
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 10 deletions.
4 changes: 4 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import type {
} from "convex/server";
import type * as auth from "../auth.js";
import type * as constants from "../constants.js";
import type * as faqs from "../faqs.js";
import type * as forms from "../forms.js";
import type * as helpers from "../helpers.js";
import type * as http from "../http.js";
import type * as passwordReset from "../passwordReset.js";
import type * as questFields from "../questFields.js";
import type * as quests from "../quests.js";
import type * as seed from "../seed.js";
import type * as topics from "../topics.js";
import type * as userEncryptedData from "../userEncryptedData.js";
import type * as userQuests from "../userQuests.js";
import type * as userSettings from "../userSettings.js";
Expand All @@ -39,13 +41,15 @@ import type * as validators from "../validators.js";
declare const fullApi: ApiFromModules<{
auth: typeof auth;
constants: typeof constants;
faqs: typeof faqs;
forms: typeof forms;
helpers: typeof helpers;
http: typeof http;
passwordReset: typeof passwordReset;
questFields: typeof questFields;
quests: typeof quests;
seed: typeof seed;
topics: typeof topics;
userEncryptedData: typeof userEncryptedData;
userQuests: typeof userQuests;
userSettings: typeof userSettings;
Expand Down
131 changes: 131 additions & 0 deletions convex/faqs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { convexTest } from "convex-test";
import { describe, expect, it } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";
import { modules } from "./test.setup";

describe("faqs", () => {
it("creates a FAQ", async () => {
const t = convexTest(schema, modules);

// Create a topic first
const topicId = await t.mutation(api.topics.createTopic, {
topic: "Costs",
});

// Create a FAQ
const faqId = await t.mutation(api.faqs.createFAQ, {
question: "How much does the process cost?",
answer: "It varies.",
topics: [topicId],
});

// Verify FAQ creation
const faqs = await t.query(api.faqs.getAllFAQs);
const createdFAQ = faqs.find((f) => f._id === faqId);
expect(createdFAQ).toBeTruthy();
expect(createdFAQ?.question).toBe("How much does the process cost?");
expect(createdFAQ?.answer).toBe("It varies.");
expect(createdFAQ?.topics).toContain(topicId);
});

it("updates a FAQ", async () => {
const t = convexTest(schema, modules);

// Create a topic first
const topicId = await t.mutation(api.topics.createTopic, {
topic: "Costs",
});

// Create a FAQ
const faqId = await t.mutation(api.faqs.createFAQ, {
question: "How much does the process cost?",
answer: "It varies.",
topics: [topicId],
});

// Update the FAQ
await t.mutation(api.faqs.updateFAQ, {
faqId,
faq: {
question: "How much does the process cost, exactly?",
answer: "It varies a lot.",
topics: [topicId],
},
});

// Verify FAQ update
const faqs = await t.query(api.faqs.getAllFAQs);
const updatedFAQ = faqs.find((f) => f._id === faqId);
expect(updatedFAQ).toBeTruthy();
expect(updatedFAQ?.question).toBe(
"How much does the process cost, exactly?",
);
expect(updatedFAQ?.answer).toBe("It varies a lot.");
});

it("gets all FAQs", async () => {
const t = convexTest(schema, modules);

// Create a topic first
const topicId = await t.mutation(api.topics.createTopic, {
topic: "Costs",
});

// Mock FAQs
await t.run(async (ctx) => {
await ctx.db.insert("faqs", {
question: "How much does the process cost?",
answer: "It varies.",
topics: [topicId],
});
await ctx.db.insert("faqs", {
question: "What documents do I need?",
answer: "You'll need passport, visa, and other supporting documents.",
topics: [topicId],
});
});

// Get all FAQs
const faqs = await t.query(api.faqs.getAllFAQs);

// Verify FAQs
expect(faqs.length).toBe(2);
expect(faqs[0].question).toBe("How much does the process cost?");
expect(faqs[1].question).toBe("What documents do I need?");
});

it("deletes a FAQ", async () => {
const t = convexTest(schema, modules);

// Create a topic first
const topicId = await t.mutation(api.topics.createTopic, {
topic: "Costs",
});

// Create a FAQ
const faqId = await t.mutation(api.faqs.createFAQ, {
question: "How much does the process cost?",
answer: "It varies.",
topics: [topicId],
});

// Verify FAQ creation
const faqs = await t.query(api.faqs.getAllFAQs);
const createdFAQ = faqs.find((f) => f._id === faqId);
expect(createdFAQ).toBeTruthy();
expect(createdFAQ?.question).toBe("How much does the process cost?");
expect(createdFAQ?.answer).toBe("It varies.");
expect(createdFAQ?.topics).toContain(topicId);

// Delete the FAQ
await t.mutation(api.faqs.deleteFAQ, {
faqId,
});

// Verify FAQ deletion
const faqsAfter = await t.query(api.faqs.getAllFAQs);
const deletedFAQ = faqsAfter.find((f) => f._id === faqId);
expect(deletedFAQ).toBeUndefined();
});
});
4 changes: 3 additions & 1 deletion convex/faqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export const createFAQ = mutation({
question: v.string(),
answer: v.string(),
topics: v.array(v.id("topics")),
relatedQuests: v.optional(v.array(v.id("quests"))),
},
handler: async (ctx, { question, answer, topics }) => {
handler: async (ctx, { question, answer, topics, relatedQuests }) => {
return await ctx.db.insert("faqs", {
question,
answer,
topics,
relatedQuests,
});
},
});
Expand Down
15 changes: 7 additions & 8 deletions convex/quests.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { convexTest } from "convex-test";
import { expect, test } from "vitest";
import { describe, expect, it } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";

// TODO: Remove this import and this file;
// shouldn't need to use it since everything is in the default location
import { modules } from "./test.setup";

test("get all quests", async () => {
const t = convexTest(schema, modules);
const quests = await t.query(api.quests.getAllQuests);
expect(quests).toMatchObject([]);
describe("quests", () => {
it("gets all quests", async () => {
const t = convexTest(schema, modules);
const quests = await t.query(api.quests.getAllQuests);
expect(quests).toMatchObject([]);
});
});
85 changes: 85 additions & 0 deletions convex/topics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { convexTest } from "convex-test";
import { describe, expect, it } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";
import { modules } from "./test.setup";

describe("topics", () => {
it("creates a topic", async () => {
const t = convexTest(schema, modules);
// Create a topic
const topicId = await t.mutation(api.topics.createTopic, {
topic: "Immigration",
});

// Verify topic creation
const topics = await t.query(api.topics.getAllTopics);
const createdTopic = topics.find((t) => t._id === topicId);
expect(createdTopic).toBeTruthy();
expect(createdTopic?.topic).toBe("Immigration");
});

it("updates a topic", async () => {
const t = convexTest(schema, modules);
// Create a topic
const topicId = await t.mutation(api.topics.createTopic, {
topic: "Immigration",
});

// Update the topic
await t.mutation(api.topics.updateTopic, {
topicId,
topic: "Adoption",
});

// Verify topic update
const topics = await t.query(api.topics.getAllTopics);
const updatedTopic = topics.find((t) => t._id === topicId);
expect(updatedTopic).toBeTruthy();
expect(updatedTopic?.topic).toBe("Adoption");
});

it("deletes a topic", async () => {
const t = convexTest(schema, modules);
// Create a topic
const topicId = await t.mutation(api.topics.createTopic, {
topic: "Immigration",
});

// Verify topic creation
const topics = await t.query(api.topics.getAllTopics);
const createdTopic = topics.find((t) => t._id === topicId);
expect(createdTopic).toBeTruthy();
expect(createdTopic?.topic).toBe("Immigration");

// Delete the topic
await t.mutation(api.topics.deleteTopic, {
topicId,
});

// Verify topic deletion
const topicsAfter = await t.query(api.topics.getAllTopics);
const deletedTopic = topicsAfter.find((t) => t._id === topicId);
expect(deletedTopic).toBeUndefined();
});

it("gets all topics", async () => {
const t = convexTest(schema, modules);

// Mock topics
await t.run(async (ctx) => {
await ctx.db.insert("topics", { topic: "Immigration" });
await ctx.db.insert("topics", { topic: "Adoption" });
await ctx.db.insert("topics", { topic: "Costs" });
});

// Get all topics
const topics = await t.query(api.topics.getAllTopics);

// Verify topics
expect(topics.length).toBe(3);
expect(topics[0].topic).toBe("Immigration");
expect(topics[1].topic).toBe("Adoption");
expect(topics[2].topic).toBe("Costs");
});
});
5 changes: 4 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export default defineConfig({
["convex/**", "edge-runtime"],
["**", "jsdom"],
],
include: ["src/**/*.{test,spec}.{ts,tsx}"],
include: [
"src/**/*.{test,spec}.{ts,tsx}",
"convex/**/*.{test,spec}.{ts,tsx}",
],
exclude: [...configDefaults.exclude, "e2e/**"],
coverage: {
reporter: ["text", "json-summary", "json"],
Expand Down

0 comments on commit 6f7427b

Please sign in to comment.