generated from UoaWDCC/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9333bf0
commit bc785b9
Showing
3 changed files
with
820 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import mongoose from "mongoose"; | ||
import { MongoMemoryServer } from "mongodb-memory-server"; | ||
import User from "../User"; | ||
import { afterAll, beforeAll, beforeEach, expect, it } from "vitest"; | ||
import { describe } from "node:test"; | ||
|
||
let mongod: MongoMemoryServer; | ||
|
||
const user1 = { | ||
name: "Harsheel", | ||
email: "hsin212", | ||
password: "password", | ||
}; | ||
|
||
const user2 = { | ||
name: "Adi", | ||
email: "ashe194", | ||
password: "Password", | ||
}; | ||
|
||
const user3 = { | ||
name: "Adi", | ||
email: "slie", | ||
password: "Password", | ||
}; | ||
|
||
const users = [user1, user2, user3]; | ||
|
||
/** | ||
* Before all tests, create an in-memory MongoDB instance so we don't have to test on a real database, | ||
* then establish a mongoose connection to it. | ||
*/ | ||
beforeAll(async () => { | ||
mongod = await MongoMemoryServer.create(); | ||
const uri = mongod.getUri(); | ||
await mongoose.connect(uri); | ||
}); | ||
|
||
/** | ||
* Before each test, intialize the database with some data | ||
*/ | ||
beforeEach(async () => { | ||
await mongoose.connection.db.dropDatabase(); | ||
const usersCollection = mongoose.connection.db.collection("users"); | ||
await usersCollection.insertMany(users); | ||
}); | ||
|
||
/** | ||
* After all tests, gracefully terminate the in-memory MongoDB instance and mongoose connection. | ||
*/ | ||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
await mongod.stop(); | ||
}); | ||
|
||
describe("User Model", () => { | ||
it("find all the users", async () => { | ||
const users = await User.find(); | ||
expect(users.length).toBe(3); | ||
}); | ||
|
||
it("find a user by name", async () => { | ||
const foundUser = await User.findOne({ name: "Harsheel" }); | ||
expect(foundUser?.name).toBe("Harsheel"); | ||
}); | ||
|
||
it("does not find user by invalid name", async () => { | ||
const user = await User.findOne({ name: "invalid" }); | ||
expect(user).toBeNull(); | ||
}); | ||
|
||
it("find a user by email", async () => { | ||
const foundUser = await User.findOne({ email: "hsin212" }); | ||
expect(foundUser?.email).toBe("hsin212"); | ||
}); | ||
|
||
it("does not find user by invalid email", async () => { | ||
const user = await User.findOne({ email: "[email protected]" }); | ||
expect(user).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.