Skip to content

Commit 040b177

Browse files
authored
Merge pull request #1186 from david-roper/subject-service-tests
Subject service tests
2 parents 7e56083 + 57e7508 commit 040b177

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

apps/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"dependencies": {
1818
"@douglasneuroinformatics/libcrypto": "catalog:",
1919
"@douglasneuroinformatics/libjs": "catalog:",
20-
"@douglasneuroinformatics/libnest": "^7.3.2",
20+
"@douglasneuroinformatics/libnest": "^7.3.3",
2121
"@douglasneuroinformatics/libpasswd": "catalog:",
2222
"@douglasneuroinformatics/libstats": "catalog:",
2323
"@faker-js/faker": "^9.4.0",

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
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';
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 { beforeEach, describe, expect, it, vi } from 'vitest';
99

1010
import { SubjectsService } from '../subjects.service';
1111

1212
describe('SubjectsService', () => {
1313
let subjectsService: SubjectsService;
1414
let subjectModel: MockedInstance<Model<'Subject'>>;
15+
let prismaClient: MockedInstance<ExtendedPrismaClient> & {
16+
[key: string]: any;
17+
};
1518

1619
beforeEach(async () => {
1720
const moduleRef = await Test.createTestingModule({
1821
providers: [
1922
MockFactory.createForService(CryptoService),
2023
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+
}
2240
]
2341
}).compile();
2442
subjectModel = moduleRef.get(getModelToken('Subject'));
2543
subjectsService = moduleRef.get(SubjectsService);
44+
prismaClient = moduleRef.get(PRISMA_CLIENT_TOKEN);
2645
});
2746

2847
describe('create', () => {
@@ -59,6 +78,28 @@ describe('SubjectsService', () => {
5978
});
6079
});
6180

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

0 commit comments

Comments
 (0)