|
| 1 | +import type { Model } from '@douglasneuroinformatics/libnest'; |
| 2 | +import { getModelToken } from '@douglasneuroinformatics/libnest'; |
| 3 | +import { MockFactory } from '@douglasneuroinformatics/libnest/testing'; |
| 4 | +import type { MockedInstance } from '@douglasneuroinformatics/libnest/testing'; |
| 5 | +import { NotFoundException } from '@nestjs/common'; |
| 6 | +import { Test } from '@nestjs/testing'; |
| 7 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 8 | + |
| 9 | +import { GroupsService } from '../../groups/groups.service'; |
| 10 | +import { InstrumentsService } from '../../instruments/instruments.service'; |
| 11 | +import { SessionsService } from '../../sessions/sessions.service'; |
| 12 | +import { SubjectsService } from '../../subjects/subjects.service'; |
| 13 | +import { InstrumentMeasuresService } from '../instrument-measures.service'; |
| 14 | +import { InstrumentRecordsService } from '../instrument-records.service'; |
| 15 | + |
| 16 | +describe('InstrumentRecordsService', () => { |
| 17 | + let instrumentRecordsService: InstrumentRecordsService; |
| 18 | + let instrumentRecordModel: MockedInstance<Model<'InstrumentRecord'>>; |
| 19 | + let _groupsService: MockedInstance<GroupsService>; |
| 20 | + let _instrumentMeasuresService: MockedInstance<InstrumentMeasuresService>; |
| 21 | + let _instrumentsService: MockedInstance<InstrumentsService>; |
| 22 | + let _sessionsService: MockedInstance<SessionsService>; |
| 23 | + let _subjectsService: MockedInstance<SubjectsService>; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + const moduleRef = await Test.createTestingModule({ |
| 27 | + providers: [ |
| 28 | + InstrumentRecordsService, |
| 29 | + MockFactory.createForModelToken(getModelToken('InstrumentRecord')), |
| 30 | + MockFactory.createForService(GroupsService), |
| 31 | + MockFactory.createForService(InstrumentMeasuresService), |
| 32 | + MockFactory.createForService(InstrumentsService), |
| 33 | + MockFactory.createForService(SessionsService), |
| 34 | + MockFactory.createForService(SubjectsService) |
| 35 | + ] |
| 36 | + }).compile(); |
| 37 | + |
| 38 | + instrumentRecordModel = moduleRef.get(getModelToken('InstrumentRecord')); |
| 39 | + instrumentRecordsService = moduleRef.get(InstrumentRecordsService); |
| 40 | + _groupsService = moduleRef.get(GroupsService); |
| 41 | + _instrumentMeasuresService = moduleRef.get(InstrumentMeasuresService); |
| 42 | + _instrumentsService = moduleRef.get(InstrumentsService); |
| 43 | + _sessionsService = moduleRef.get(SessionsService); |
| 44 | + _subjectsService = moduleRef.get(SubjectsService); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('findById', () => { |
| 48 | + it('should throw a NotFoundException if no record is found', async () => { |
| 49 | + instrumentRecordModel.findFirst.mockResolvedValueOnce(null); |
| 50 | + await expect(instrumentRecordsService.findById('nonexistent-id')).rejects.toBeInstanceOf(NotFoundException); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return the instrument record with the correct shape', async () => { |
| 54 | + const mockRecord = { |
| 55 | + data: { test: 'data' }, |
| 56 | + date: new Date(), |
| 57 | + id: 'test-record-id', |
| 58 | + instrumentId: 'test-instrument-id', |
| 59 | + sessionId: 'test-session-id', |
| 60 | + subjectId: 'test-subject-id' |
| 61 | + }; |
| 62 | + |
| 63 | + instrumentRecordModel.findFirst.mockResolvedValueOnce(mockRecord); |
| 64 | + |
| 65 | + const result = await instrumentRecordsService.findById('test-record-id'); |
| 66 | + |
| 67 | + expect(result).toMatchObject({ |
| 68 | + data: { test: 'data' }, |
| 69 | + id: 'test-record-id', |
| 70 | + instrumentId: 'test-instrument-id', |
| 71 | + sessionId: 'test-session-id', |
| 72 | + subjectId: 'test-subject-id' |
| 73 | + }); |
| 74 | + expect(result.date).toBeInstanceOf(Date); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments