File size: 4,450 Bytes
3b492d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Test, TestingModule } from '@nestjs/testing';
import { CourseSectionsController } from '../controllers/course-sections.controller';
import { CourseSectionsService } from '../services/course-sections.service';
import { CreateSectionDto, UpdateSectionDto } from '../dtos';

describe('CourseSectionsController', () => {
  let controller: CourseSectionsController;
  let service: CourseSectionsService;

  const mockSection = {
    id: 1,
    courseId: 1,
    semesterId: 1,
    sectionNumber: '01',
    maxCapacity: 40,
    currentEnrollment: 25,
    location: 'Building A, Room 101',
    status: 'open',
    createdAt: new Date(),
    updatedAt: new Date(),
    course: { id: 1, name: 'Data Structures', code: 'CS201' },
    semester: { id: 1, name: 'Fall 2025' },
    schedules: [],
  };

  const mockSectionsService = {
    findByCourseId: jest.fn(),
    findById: jest.fn(),
    create: jest.fn(),
    update: jest.fn(),
    updateEnrollment: jest.fn(),
  };

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [CourseSectionsController],
      providers: [
        {
          provide: CourseSectionsService,
          useValue: mockSectionsService,
        },
      ],
    }).compile();

    controller = module.get<CourseSectionsController>(CourseSectionsController);
    service = module.get<CourseSectionsService>(CourseSectionsService);
  });

  afterEach(() => {
    jest.clearAllMocks();
  });

  describe('findByCourseId', () => {
    it('should return sections for a course', async () => {
      mockSectionsService.findByCourseId.mockResolvedValue([mockSection]);

      const result = await controller.findByCourseId(1);

      expect(result).toHaveLength(1);
      expect(result[0].courseId).toBe(1);
      expect(service.findByCourseId).toHaveBeenCalledWith(1, undefined);
    });

    it('should filter by semester', async () => {
      mockSectionsService.findByCourseId.mockResolvedValue([mockSection]);

      const result = await controller.findByCourseId(1, 1);

      expect(service.findByCourseId).toHaveBeenCalledWith(1, 1);
    });
  });

  describe('findById', () => {
    it('should return a section by id', async () => {
      mockSectionsService.findById.mockResolvedValue(mockSection);

      const result = await controller.findById(1);

      expect(result).toBeDefined();
      expect(result.id).toBe(1);
      expect(service.findById).toHaveBeenCalledWith(1);
    });
  });

  describe('create', () => {
    it('should create a new section', async () => {
      const createDto: CreateSectionDto = {
        courseId: 1,
        semesterId: 1,
        sectionNumber: 1,
        maxCapacity: 40,
        currentEnrollment: 0,
        location: 'Building A, Room 101',
      };

      mockSectionsService.create.mockResolvedValue(mockSection);

      const result = await controller.create(createDto);

      expect(result).toBeDefined();
      expect(result.courseId).toBe(1);
      expect(service.create).toHaveBeenCalledWith(createDto);
    });
  });

  describe('update', () => {
    it('should update section details', async () => {
      const updateDto: UpdateSectionDto = {
        maxCapacity: 50,
        currentEnrollment: 30,
        location: 'Building B, Room 201',
      };

      const updated = { ...mockSection, ...updateDto };
      mockSectionsService.update.mockResolvedValue(updated);

      const result = await controller.update(1, updateDto);

      expect(result.maxCapacity).toBe(50);
      expect(result.location).toBe('Building B, Room 201');
      expect(service.update).toHaveBeenCalledWith(1, updateDto);
    });
  });

  describe('updateEnrollment', () => {
    it('should update section enrollment', async () => {
      const updated = { ...mockSection, currentEnrollment: 35 };
      mockSectionsService.updateEnrollment.mockResolvedValue(undefined);
      mockSectionsService.findById.mockResolvedValue(updated);

      const result = await controller.updateEnrollment(1, 35);

      expect(result.currentEnrollment).toBe(35);
      expect(service.updateEnrollment).toHaveBeenCalledWith(1, 35);
    });

    it('should not allow enrollment to exceed capacity', async () => {
      mockSectionsService.updateEnrollment.mockRejectedValue(
        new Error('Section is full'),
      );

      await expect(
        controller.updateEnrollment(1, 50),
      ).rejects.toThrow('Section is full');
    });
  });
});