|
1 | 1 | import { CryptoService, getModelToken } from '@douglasneuroinformatics/libnest'; |
2 | | -import type { Model } 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 { PRISMA_CLIENT_TOKEN } from 'node_modules/@douglasneuroinformatics/libnest/dist/modules/prisma/prisma.config'; |
| 9 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
9 | 10 |
|
10 | 11 | import { SubjectsService } from '../subjects.service'; |
11 | 12 |
|
12 | 13 | describe('SubjectsService', () => { |
13 | 14 | let subjectsService: SubjectsService; |
14 | 15 | let subjectModel: MockedInstance<Model<'Subject'>>; |
| 16 | + let prismaClient: MockedInstance<ExtendedPrismaClient> & { |
| 17 | + [key: string]: any; |
| 18 | + }; |
15 | 19 |
|
16 | 20 | beforeEach(async () => { |
17 | 21 | const moduleRef = await Test.createTestingModule({ |
18 | 22 | providers: [ |
19 | 23 | MockFactory.createForService(CryptoService), |
20 | 24 | SubjectsService, |
21 | | - MockFactory.createForModelToken(getModelToken('Subject')) |
| 25 | + MockFactory.createForModelToken(getModelToken('Subject')), |
| 26 | + { |
| 27 | + provide: PRISMA_CLIENT_TOKEN, |
| 28 | + useValue: { |
| 29 | + $transaction: vi.fn() |
| 30 | + } |
| 31 | + } |
22 | 32 | ] |
23 | 33 | }).compile(); |
24 | 34 | subjectModel = moduleRef.get(getModelToken('Subject')); |
25 | 35 | subjectsService = moduleRef.get(SubjectsService); |
| 36 | + prismaClient = moduleRef.get(PRISMA_CLIENT_TOKEN); |
26 | 37 | }); |
27 | 38 |
|
28 | 39 | describe('create', () => { |
@@ -59,6 +70,20 @@ describe('SubjectsService', () => { |
59 | 70 | }); |
60 | 71 | }); |
61 | 72 |
|
| 73 | + describe('deleteById', () => { |
| 74 | + it('should delete the subject via the subject model and not call $transaction, if force is falsy', async () => { |
| 75 | + subjectModel.findFirst.mockResolvedValueOnce({ id: '123' }); |
| 76 | + await subjectsService.deleteById('123'); |
| 77 | + expect(subjectModel.delete).toHaveBeenCalledOnce(); |
| 78 | + expect(prismaClient.$transaction).not.toHaveBeenCalled(); |
| 79 | + }); |
| 80 | + it('should use $transaction if force is set to true', async () => { |
| 81 | + subjectModel.findFirst.mockResolvedValueOnce({ id: '123' }); |
| 82 | + await subjectsService.deleteById('123', { force: true }); |
| 83 | + expect(subjectModel.delete).not.toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
62 | 87 | describe('findById', () => { |
63 | 88 | it('should throw a `NotFoundException` if there is no subject with the provided id', async () => { |
64 | 89 | subjectModel.findFirst.mockResolvedValueOnce(null); |
|
0 commit comments