|
| 1 | +// Mock and import CheckIn model |
| 2 | +jest.mock('../models/checkIn.model'); |
| 3 | +const { CheckIn } = require('../models'); |
| 4 | + |
| 5 | +// Import the check-ins router |
| 6 | +const express = require('express'); |
| 7 | +const supertest = require('supertest'); |
| 8 | +const checkInsRouter = require('./checkIns.router'); |
| 9 | + |
| 10 | +// Create a new Express application for testing |
| 11 | +const testapp = express(); |
| 12 | +// Allows for body parsing |
| 13 | +testapp.use(express.json()); |
| 14 | +testapp.use('/api/checkins', checkInsRouter); |
| 15 | +const request = supertest(testapp); |
| 16 | + |
| 17 | +describe('Unit tests for checkIns router', () => { |
| 18 | + // Mock data for check-ins |
| 19 | + const mockCheckIns = [ |
| 20 | + { id: 1, eventId: 'event1', userId: 'user1', checkedIn: true, createdDate: String(new Date()) }, |
| 21 | + { id: 2, eventId: 'event2', userId: 'user2', checkedIn: true, createdDate: String(new Date()) }, |
| 22 | + ]; |
| 23 | + |
| 24 | + // Clear mocks after each test |
| 25 | + afterEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('READ', () => { |
| 30 | + it('should return a list of check-ins with GET /api/checkins', async (done) => { |
| 31 | + // Mock Mongoose method |
| 32 | + CheckIn.find.mockResolvedValue(mockCheckIns); |
| 33 | + |
| 34 | + const response = await request.get('/api/checkins'); |
| 35 | + |
| 36 | + // Tests |
| 37 | + expect(CheckIn.find).toHaveBeenCalled(); |
| 38 | + expect(response.status).toBe(200); |
| 39 | + expect(response.body).toEqual(mockCheckIns); |
| 40 | + |
| 41 | + // Marks completion of test |
| 42 | + done(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return a single check-in by id with GET /api/checkins/:id', async (done) => { |
| 46 | + // Mock Mongoose method |
| 47 | + CheckIn.findById.mockResolvedValue(mockCheckIns[0]); |
| 48 | + |
| 49 | + const response = await request.get('/api/checkins/1'); |
| 50 | + |
| 51 | + // Tests |
| 52 | + expect(CheckIn.findById).toHaveBeenCalledWith('1'); |
| 53 | + expect(response.status).toBe(200); |
| 54 | + expect(response.body).toEqual(mockCheckIns[0]); |
| 55 | + |
| 56 | + // Marks completion of test |
| 57 | + done(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return a list of users who have checked into a specific event with GET /api/checkins/findEvent/:id', async (done) => { |
| 61 | + // Mock specific checkIn |
| 62 | + const mockCheckIn = mockCheckIns[1]; |
| 63 | + const { eventId } = mockCheckIn; |
| 64 | + |
| 65 | + // Mock Mongoose methods |
| 66 | + CheckIn.find.mockReturnValue({ |
| 67 | + populate: jest.fn().mockResolvedValue(mockCheckIn), |
| 68 | + }); |
| 69 | + |
| 70 | + const response = await request.get(`/api/checkins/findEvent/${eventId}`); |
| 71 | + |
| 72 | + // Tests |
| 73 | + expect(CheckIn.find).toHaveBeenCalledWith({ |
| 74 | + eventId: eventId, |
| 75 | + userId: { $ne: 'undefined' }, |
| 76 | + }); |
| 77 | + expect(CheckIn.find().populate).toHaveBeenCalledWith({ path: 'userId', model: 'User' }); |
| 78 | + expect(response.status).toBe(200); |
| 79 | + expect(response.body).toEqual(mockCheckIn); |
| 80 | + |
| 81 | + // Marks completion of test |
| 82 | + done(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('CREATE', () => { |
| 87 | + it('should create a new check-in with POST /api/checkins', async (done) => { |
| 88 | + // Mock new check-in data |
| 89 | + const newCheckIn = { |
| 90 | + id: 3, |
| 91 | + eventId: 'event3', |
| 92 | + userId: 'user3', |
| 93 | + checkedIn: true, |
| 94 | + createdDate: String(new Date()), |
| 95 | + }; |
| 96 | + |
| 97 | + // Mock create method |
| 98 | + CheckIn.create.mockResolvedValue(newCheckIn); |
| 99 | + |
| 100 | + const response = await request.post('/api/checkins').send(newCheckIn); |
| 101 | + |
| 102 | + // Tests |
| 103 | + expect(CheckIn.create).toHaveBeenCalledWith(newCheckIn); |
| 104 | + expect(response.status).toBe(201); |
| 105 | + |
| 106 | + // Marks completion of test |
| 107 | + done(); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments