|
1 | | -import { CryptoService, getModelToken } from '@douglasneuroinformatics/libnest'; |
2 | | -import type { Model } from '@douglasneuroinformatics/libnest'; |
| 1 | +import { CryptoService, getModelToken, PRISMA_CLIENT_TOKEN } from '@douglasneuroinformatics/libnest'; |
| 2 | +import type { ExtendedPrismaClient, Model } from '@douglasneuroinformatics/libnest'; |
3 | 3 | import { MockFactory } from '@douglasneuroinformatics/libnest/testing'; |
4 | 4 | import type { MockedInstance } from '@douglasneuroinformatics/libnest/testing'; |
5 | 5 | import { ConflictException, NotFoundException } from '@nestjs/common'; |
6 | 6 | import { Test } from '@nestjs/testing'; |
7 | 7 | import { pick } from 'lodash-es'; |
8 | | -import { beforeEach, describe, expect, it } from 'vitest'; |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
9 | 9 |
|
10 | 10 | import { SubjectsService } from '../subjects.service'; |
11 | 11 |
|
12 | 12 | describe('SubjectsService', () => { |
13 | 13 | let subjectsService: SubjectsService; |
14 | 14 | let subjectModel: MockedInstance<Model<'Subject'>>; |
| 15 | + let prismaClient: MockedInstance<ExtendedPrismaClient> & { |
| 16 | + [key: string]: any; |
| 17 | + }; |
15 | 18 |
|
16 | 19 | beforeEach(async () => { |
17 | 20 | const moduleRef = await Test.createTestingModule({ |
18 | 21 | providers: [ |
19 | 22 | MockFactory.createForService(CryptoService), |
20 | 23 | SubjectsService, |
21 | | - MockFactory.createForModelToken(getModelToken('Subject')) |
| 24 | + MockFactory.createForModelToken(getModelToken('Subject')), |
| 25 | + { |
| 26 | + provide: PRISMA_CLIENT_TOKEN, |
| 27 | + useValue: { |
| 28 | + $transaction: vi.fn(), |
| 29 | + instrumentRecord: { |
| 30 | + deleteMany: vi.fn() |
| 31 | + }, |
| 32 | + session: { |
| 33 | + deleteMany: vi.fn() |
| 34 | + }, |
| 35 | + subject: { |
| 36 | + deleteMany: vi.fn() |
| 37 | + } |
| 38 | + } |
| 39 | + } |
22 | 40 | ] |
23 | 41 | }).compile(); |
24 | 42 | subjectModel = moduleRef.get(getModelToken('Subject')); |
25 | 43 | subjectsService = moduleRef.get(SubjectsService); |
| 44 | + prismaClient = moduleRef.get(PRISMA_CLIENT_TOKEN); |
26 | 45 | }); |
27 | 46 |
|
28 | 47 | describe('create', () => { |
@@ -59,6 +78,28 @@ describe('SubjectsService', () => { |
59 | 78 | }); |
60 | 79 | }); |
61 | 80 |
|
| 81 | + describe('deleteById', () => { |
| 82 | + it('should delete the subject via the subject model and not call $transaction, if force is falsy', async () => { |
| 83 | + subjectModel.findFirst.mockResolvedValueOnce({ id: '123' }); |
| 84 | + await subjectsService.deleteById('123'); |
| 85 | + expect(subjectModel.delete).toHaveBeenCalledOnce(); |
| 86 | + expect(prismaClient.$transaction).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + it('should use $transaction if force is set to true', async () => { |
| 89 | + subjectModel.findFirst.mockResolvedValueOnce({ id: '123' }); |
| 90 | + await subjectsService.deleteById('123', { force: true }); |
| 91 | + expect(subjectModel.delete).not.toHaveBeenCalled(); |
| 92 | + expect(prismaClient.session.deleteMany).toHaveBeenCalled(); |
| 93 | + expect(prismaClient.instrumentRecord.deleteMany).toHaveBeenCalled(); |
| 94 | + expect(prismaClient.subject.deleteMany).toHaveBeenCalled(); |
| 95 | + expect(prismaClient.$transaction).toHaveBeenCalledOnce(); |
| 96 | + }); |
| 97 | + it('should throw NotFoundException when subject does not exist', async () => { |
| 98 | + subjectModel.findFirst.mockResolvedValueOnce(null); |
| 99 | + await expect(subjectsService.deleteById('123')).rejects.toBeInstanceOf(NotFoundException); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
62 | 103 | describe('findById', () => { |
63 | 104 | it('should throw a `NotFoundException` if there is no subject with the provided id', async () => { |
64 | 105 | subjectModel.findFirst.mockResolvedValueOnce(null); |
|
0 commit comments