Skip to content

Commit

Permalink
adds test for activities controller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgarritano committed Feb 25, 2024
1 parent 6b42b5c commit c2fd3d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/tests/controllers.tests/activities.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Check failure on line 4 in src/tests/controllers.tests/activities.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'"../../controllers/activities.controller"' has no exported member named 'createActivity'. Did you mean 'createActivities'?

Check failure on line 4 in src/tests/controllers.tests/activities.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Module '"../../controllers/activities.controller"' has no exported member 'getAllActivities'.
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);
})
})
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/dummyData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const activityDummyData1 = {
user: "test",
itemName: "activitesItem1",
bullets: ["example bullet"],
title: "title",
subtitle: "subtitle",
year: "year",
location: "location",
};
16 changes: 16 additions & 0 deletions src/tests/dbHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose from "mongoose";
import { MongoMemoryServer } from 'mongodb-memory-server';

Check failure on line 2 in src/tests/dbHandler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'mongodb-memory-server' or its corresponding type declarations.

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();
};

0 comments on commit c2fd3d0

Please sign in to comment.