mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
quiz test passes
This commit is contained in:
parent
cd0c6a469b
commit
322ba6e71a
3 changed files with 82 additions and 141 deletions
|
|
@ -1,125 +1,23 @@
|
|||
import { MongoClient, Db, ObjectId } from 'mongodb';
|
||||
import { Quiz } from '../models/quiz';
|
||||
import QuizRepository from '../repositories/quizRepository';
|
||||
|
||||
jest.mock('../repositories/quizRepository');
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { Folder } from '../models/folder';
|
||||
import { User, UserOptions } from '../models/user';
|
||||
|
||||
describe('Quiz Class', () => {
|
||||
let connection: MongoClient;
|
||||
let db: Db;
|
||||
let quizRepository: QuizRepository;
|
||||
|
||||
beforeAll(async () => {
|
||||
connection = {
|
||||
db: jest.fn().mockReturnThis(),
|
||||
collection: jest.fn().mockReturnThis(),
|
||||
findOne: jest.fn(),
|
||||
insertOne: jest.fn(),
|
||||
deleteOne: jest.fn(),
|
||||
deleteMany: jest.fn(),
|
||||
updateOne: jest.fn(),
|
||||
} as unknown as MongoClient;
|
||||
|
||||
db = connection.db();
|
||||
quizRepository = new QuizRepository();
|
||||
(quizRepository as any).db = { getConnection: () => db };
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should create a new quiz', async () => {
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const insertedId = new ObjectId();
|
||||
jest.spyOn(quizRepository, 'createQuiz').mockResolvedValue(insertedId);
|
||||
// mock a folder and a user object
|
||||
const userOptions: UserOptions = {
|
||||
email: 'email',
|
||||
hashedPassword: 'hashedPassword'
|
||||
};
|
||||
const user = new User(userOptions);
|
||||
const folder = new Folder('folderId', user);
|
||||
|
||||
const result = await quiz.create();
|
||||
const quiz = new Quiz(folder, user, 'title', 'content');
|
||||
|
||||
expect(result).toEqual(insertedId);
|
||||
expect(quizRepository.createQuiz).toHaveBeenCalledWith(quiz);
|
||||
expect(quiz.user).toEqual(user);
|
||||
expect(quiz.folder).toEqual(folder);
|
||||
});
|
||||
|
||||
it('should get the owner of a quiz', async () => {
|
||||
const quizId = new ObjectId().toHexString();
|
||||
const userId = 'userId';
|
||||
jest.spyOn(quizRepository, 'getOwner').mockResolvedValue(userId);
|
||||
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const result = await quiz.getOwner(quizId);
|
||||
|
||||
expect(result).toEqual(userId);
|
||||
expect(quizRepository.getOwner).toHaveBeenCalledWith(quizId);
|
||||
});
|
||||
|
||||
it('should get the content of a quiz', async () => {
|
||||
const quizId = new ObjectId().toHexString();
|
||||
const content = 'content';
|
||||
jest.spyOn(quizRepository, 'getContent').mockResolvedValue(content);
|
||||
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const result = await quiz.getContent(quizId);
|
||||
|
||||
expect(result).toEqual(content);
|
||||
expect(quizRepository.getContent).toHaveBeenCalledWith(quizId);
|
||||
});
|
||||
|
||||
it('should delete a quiz', async () => {
|
||||
const quizId = new ObjectId().toHexString();
|
||||
jest.spyOn(quizRepository, 'delete').mockResolvedValue(true);
|
||||
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const result = await quiz.delete(quizId);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(quizRepository.delete).toHaveBeenCalledWith(quizId);
|
||||
});
|
||||
|
||||
it('should update a quiz', async () => {
|
||||
const quizId = new ObjectId().toHexString();
|
||||
const updateData = { title: 'new title' };
|
||||
jest.spyOn(quizRepository, 'update').mockResolvedValue(true);
|
||||
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const result = await quiz.update(quizId, updateData);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(quizRepository.update).toHaveBeenCalledWith(quizId, updateData);
|
||||
});
|
||||
|
||||
it('should move a quiz to a new folder', async () => {
|
||||
const quizId = new ObjectId().toHexString();
|
||||
const newFolderId = 'newFolderId';
|
||||
jest.spyOn(quizRepository, 'move').mockResolvedValue(true);
|
||||
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const result = await quiz.move(quizId, newFolderId);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(quizRepository.move).toHaveBeenCalledWith(quizId, newFolderId);
|
||||
});
|
||||
|
||||
it('should duplicate a quiz', async () => {
|
||||
const quizId = new ObjectId().toHexString();
|
||||
const newQuizId = new ObjectId();
|
||||
jest.spyOn(quizRepository, 'duplicate').mockResolvedValue(newQuizId);
|
||||
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const result = await quiz.duplicate(quizId);
|
||||
|
||||
expect(result).toEqual(newQuizId);
|
||||
expect(quizRepository.duplicate).toHaveBeenCalledWith(quizId);
|
||||
});
|
||||
|
||||
it('should check if a quiz exists', async () => {
|
||||
const title = 'title';
|
||||
const userId = 'userId';
|
||||
jest.spyOn(quizRepository, 'quizExists').mockResolvedValue(true);
|
||||
|
||||
const quiz = new Quiz('folderId', 'userId', 'title', 'content', quizRepository);
|
||||
const result = await quiz.quizExists(title, userId);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(quizRepository.quizExists).toHaveBeenCalledWith(title, userId);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,19 +1,34 @@
|
|||
import db from '../config/db';
|
||||
import bcrypt from 'bcrypt';
|
||||
import AppError from '../middleware/AppError';
|
||||
import { USER_ALREADY_EXISTS } from '../constants/errorCodes';
|
||||
import { Folder } from './folder';
|
||||
import { Image } from './image';
|
||||
import { Quiz } from './quiz';
|
||||
import { Folder } from './folder';
|
||||
|
||||
export interface UserOptions {
|
||||
email: string;
|
||||
hashedPassword: string;
|
||||
created_at?: Date;
|
||||
_id?: string;
|
||||
folders?: Array<Folder>;
|
||||
images?: Array<Image>;
|
||||
quizzes?: Array<Quiz>;
|
||||
}
|
||||
|
||||
export class User {
|
||||
constructor(public email: string, public hashedPassword: string, public created_at?: Date, public _id?: string, public folders?: Array<Folder>, public images?: Array<Image>, public quizzes?: Array<Quiz>) {
|
||||
this.email = email;
|
||||
this.hashedPassword = hashedPassword;
|
||||
this.created_at = created_at || new Date();
|
||||
this.folders = folders || new Array<Folder>();
|
||||
this.images = images || new Array<Image>();
|
||||
this.quizzes = quizzes || new Array<Quiz>();
|
||||
email: string;
|
||||
hashedPassword: string;
|
||||
created_at?: Date;
|
||||
_id?: string;
|
||||
folders?: Array<Folder>;
|
||||
images?: Array<Image>;
|
||||
quizzes?: Array<Quiz>;
|
||||
|
||||
constructor(options: UserOptions) {
|
||||
this.email = options.email;
|
||||
this.hashedPassword = options.hashedPassword;
|
||||
this.created_at = options.created_at || new Date();
|
||||
this._id = options._id;
|
||||
this.folders = options.folders || [];
|
||||
this.images = options.images || [];
|
||||
this.quizzes = options.quizzes || [];
|
||||
}
|
||||
|
||||
// async hashPassword(password: string): Promise<string> {
|
||||
|
|
|
|||
|
|
@ -1,19 +1,47 @@
|
|||
const express = require('express');
|
||||
import express, { Request, Response, NextFunction } from 'express';
|
||||
import jwt from '../middleware/jwtToken';
|
||||
import quizController from '../controllers/quiz';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const jwt = require('../middleware/jwtToken.js');
|
||||
const quizController = require('../controllers/quiz.js')
|
||||
router.post("/create", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.create(req, res, next);
|
||||
});
|
||||
|
||||
router.post("/create", jwt.authenticate, quizController.create);
|
||||
router.get("/get/:quizId", jwt.authenticate, quizController.get);
|
||||
router.delete("/delete/:quizId", jwt.authenticate, quizController.delete);
|
||||
router.put("/update", jwt.authenticate, quizController.update);
|
||||
router.put("/move", jwt.authenticate, quizController.move);
|
||||
router.get("/get/:quizId", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.get(req, res, next);
|
||||
});
|
||||
|
||||
router.post("/duplicate", jwt.authenticate, quizController.duplicate);
|
||||
router.post("/copy/:quizId", jwt.authenticate, quizController.copy);
|
||||
router.put("/Share", jwt.authenticate, quizController.Share);
|
||||
router.get("/getShare/:quizId", jwt.authenticate, quizController.getShare);
|
||||
router.post("/receiveShare", jwt.authenticate, quizController.receiveShare);
|
||||
router.delete("/delete/:quizId", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.delete(req, res, next);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
router.put("/update", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.update(req, res, next);
|
||||
});
|
||||
|
||||
router.put("/move", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.move(req, res, next);
|
||||
});
|
||||
|
||||
router.post("/duplicate", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.duplicate(req, res, next);
|
||||
});
|
||||
|
||||
router.post("/copy/:quizId", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.copy(req, res, next);
|
||||
});
|
||||
|
||||
router.put("/Share", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.Share(req, res, next);
|
||||
});
|
||||
|
||||
router.get("/getShare/:quizId", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.getShare(req, res, next);
|
||||
});
|
||||
|
||||
router.post("/receiveShare", jwt.authenticate, (req: Request, res: Response, next: NextFunction) => {
|
||||
quizController.receiveShare(req, res, next);
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
|
|
|||
Loading…
Reference in a new issue