@@ -2,14 +2,15 @@ import prisma from '../src/prisma/prisma';
22import { getHighestProjectNumber } from '../src/utils/projects.utils' ;
33import * as changeRequestUtils from '../src/utils/change-requests.utils' ;
44import { aquaman , batman , wonderwoman } from './test-data/users.test-data' ;
5- import { prismaManufacturer1 , prismaProject1 , sharedProject1 } from './test-data/projects.test-data' ;
5+ import { prismaManufacturer1 , prismaProject1 , sharedProject1 , toolMaterial } from './test-data/projects.test-data' ;
66import { prismaChangeRequest1 } from './test-data/change-requests.test-data' ;
77import { prismaTeam1 } from './test-data/teams.test-data' ;
88import * as projectTransformer from '../src/transformers/projects.transformer' ;
99import ProjectsService from '../src/services/projects.services' ;
1010import {
1111 AccessDeniedAdminOnlyException ,
1212 AccessDeniedGuestException ,
13+ AccessDeniedException ,
1314 DeletedException ,
1415 HttpException ,
1516 NotFoundException
@@ -281,4 +282,29 @@ describe('Projects', () => {
281282 expect ( manufacturer . creatorId ) . toBe ( prismaManufacturer1 . creatorId ) ;
282283 } ) ;
283284 } ) ;
285+
286+ describe ( 'materialType' , ( ) => {
287+ test ( 'Create material type fails if user is not leader' , async ( ) => {
288+ await expect ( ProjectsService . createMaterialType ( 'Tools' , wonderwoman ) ) . rejects . toThrow (
289+ new AccessDeniedException ( 'Only leadership or above can create a material type' )
290+ ) ;
291+ } ) ;
292+
293+ test ( 'Create material type fails if the material type with the given name already exists' , async ( ) => {
294+ vi . spyOn ( prisma . material_Type , 'findUnique' ) . mockResolvedValue ( toolMaterial ) ;
295+
296+ await expect ( ProjectsService . createMaterialType ( 'NERSoftwareTools' , batman ) ) . rejects . toThrow (
297+ new HttpException ( 400 , 'The following material type already exists: NERSoftwareTools' )
298+ ) ;
299+ } ) ;
300+
301+ test ( 'Create material type works' , async ( ) => {
302+ vi . spyOn ( prisma . material_Type , 'findUnique' ) . mockResolvedValue ( null ) ;
303+ vi . spyOn ( prisma . material_Type , 'create' ) . mockResolvedValue ( toolMaterial ) ;
304+
305+ const materialType = await ProjectsService . createMaterialType ( 'NERSoftwareTools' , batman ) ;
306+ expect ( materialType . name ) . toBe ( 'NERSoftwareTools' ) ;
307+ expect ( prisma . material_Type . create ) . toBeCalledTimes ( 1 ) ;
308+ } ) ;
309+ } ) ;
284310} ) ;
0 commit comments