diff --git a/src/tests/controllers.tests/activities.test.ts b/src/tests/controllers.tests/activities.test.ts new file mode 100644 index 0000000..81f7af8 --- /dev/null +++ b/src/tests/controllers.tests/activities.test.ts @@ -0,0 +1,17 @@ +import { dbConnect, dbDisconnect } from "../dbHandler"; +import { ActivitiesType } from "../../models/activities.model"; +import { activityDummyData1 } from "./dummyData"; +import { createActivity, getAllActivities } from "../../controllers/activities.controller"; +import { describe, test, expect, beforeEach, afterEach } from "vitest"; + +describe("Activities controller tests", () => { + beforeEach(async () => dbConnect()); + afterEach(async () => dbDisconnect()); + + test("Adds and retrieves an activity", async () => { + await createActivity(activityDummyData1 as ActivitiesType); + const returnedActivities = await getAllActivities(activityDummyData1.user); + expect(returnedActivities.length).to.equal(1); + expect(returnedActivities[0]).toMatchObject(activityDummyData1); + }) +}) \ No newline at end of file diff --git a/src/tests/controllers.tests/dummyData.ts b/src/tests/controllers.tests/dummyData.ts new file mode 100644 index 0000000..ef8aff8 --- /dev/null +++ b/src/tests/controllers.tests/dummyData.ts @@ -0,0 +1,9 @@ +export const activityDummyData1 = { + user: "test", + itemName: "activitesItem1", + bullets: ["example bullet"], + title: "title", + subtitle: "subtitle", + year: "year", + location: "location", +}; diff --git a/src/tests/dbHandler.ts b/src/tests/dbHandler.ts new file mode 100644 index 0000000..ff40f59 --- /dev/null +++ b/src/tests/dbHandler.ts @@ -0,0 +1,16 @@ +import mongoose from "mongoose"; +import { MongoMemoryServer } from 'mongodb-memory-server'; + +let mongoServer: MongoMemoryServer; + +export const dbConnect = async () => { + mongoServer = await MongoMemoryServer.create(); + const uri = mongoServer.getUri(); + await mongoose.connect(uri); +}; + +export const dbDisconnect = async () => { + await mongoose.connection.dropDatabase(); + await mongoose.connection.close(); + await mongoServer.stop(); +}; \ No newline at end of file