Skip to content

Commit 35d6ea8

Browse files
committed
test: add prisma client token to subject test
1 parent 7e56083 commit 35d6ea8

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

apps/api/src/subjects/__tests__/subjects.service.spec.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
import { CryptoService, getModelToken } from '@douglasneuroinformatics/libnest';
2-
import type { Model } from '@douglasneuroinformatics/libnest';
2+
import type { ExtendedPrismaClient, Model } from '@douglasneuroinformatics/libnest';
33
import { MockFactory } from '@douglasneuroinformatics/libnest/testing';
44
import type { MockedInstance } from '@douglasneuroinformatics/libnest/testing';
55
import { ConflictException, NotFoundException } from '@nestjs/common';
66
import { Test } from '@nestjs/testing';
77
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';
910

1011
import { SubjectsService } from '../subjects.service';
1112

1213
describe('SubjectsService', () => {
1314
let subjectsService: SubjectsService;
1415
let subjectModel: MockedInstance<Model<'Subject'>>;
16+
let prismaClient: MockedInstance<ExtendedPrismaClient> & {
17+
[key: string]: any;
18+
};
1519

1620
beforeEach(async () => {
1721
const moduleRef = await Test.createTestingModule({
1822
providers: [
1923
MockFactory.createForService(CryptoService),
2024
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+
}
2232
]
2333
}).compile();
2434
subjectModel = moduleRef.get(getModelToken('Subject'));
2535
subjectsService = moduleRef.get(SubjectsService);
36+
prismaClient = moduleRef.get(PRISMA_CLIENT_TOKEN);
2637
});
2738

2839
describe('create', () => {
@@ -59,6 +70,20 @@ describe('SubjectsService', () => {
5970
});
6071
});
6172

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+
6287
describe('findById', () => {
6388
it('should throw a `NotFoundException` if there is no subject with the provided id', async () => {
6489
subjectModel.findFirst.mockResolvedValueOnce(null);

0 commit comments

Comments
 (0)