88 prismaAssembly1 ,
99 toolMaterial ,
1010 prismaManufacturer1 ,
11+ prismaMaterial1 ,
1112 prismaManufacturer2 ,
1213 prismaMaterial ,
1314 prismaMaterialType ,
@@ -29,7 +30,7 @@ import {
2930import { prismaWbsElement1 } from './test-data/wbs-element.test-data' ;
3031import WorkPackagesService from '../src/services/work-packages.services' ;
3132import { validateWBS , WbsNumber } from 'shared' ;
32- import { Material_Status , User } from '@prisma/client' ;
33+ import { Material , Material_Status , User } from '@prisma/client' ;
3334
3435vi . mock ( '../src/utils/projects.utils' ) ;
3536const mockGetHighestProjectNumber = getHighestProjectNumber as jest . Mock < Promise < number > > ;
@@ -725,6 +726,64 @@ describe('Projects', () => {
725726 } ) ;
726727 } ) ;
727728
729+ describe ( 'assigning material assemblies' , ( ) => {
730+ test ( 'assignment fails because of permissions' , async ( ) => {
731+ vi . spyOn ( prisma . material , 'findUnique' ) . mockResolvedValue ( prismaMaterial1 ) ;
732+ vi . spyOn ( prisma . assembly , 'findUnique' ) . mockResolvedValue ( prismaAssembly1 ) ;
733+ const project = { ...prismaProject1 , teams : [ { members : [ aquaman ] , leads : [ superman ] } ] } ;
734+ vi . spyOn ( prisma . project , 'findFirst' ) . mockResolvedValue ( project ) ;
735+
736+ await expect ( ProjectsService . assignMaterialAssembly ( theVisitor , 'mid' , 'aid' ) ) . rejects . toThrow (
737+ new AccessDeniedException (
738+ `Only leadership or above, or someone on the project's team can assign materials to assemblies`
739+ )
740+ ) ;
741+ } ) ;
742+
743+ test ( 'assignment fails because of invalid material id' , async ( ) => {
744+ vi . spyOn ( prisma . material , 'findUnique' ) . mockResolvedValue ( null ) ;
745+ await expect ( ProjectsService . assignMaterialAssembly ( superman , 'invalid-mid' , 'aid' ) ) . rejects . toThrow (
746+ new NotFoundException ( 'Material' , 'invalid-mid' )
747+ ) ;
748+ } ) ;
749+
750+ test ( 'assignment fails because of invalid assembly id' , async ( ) => {
751+ vi . spyOn ( prisma . material , 'findUnique' ) . mockResolvedValue ( prismaMaterial1 ) ;
752+ vi . spyOn ( prisma . assembly , 'findUnique' ) . mockResolvedValue ( null ) ;
753+ await expect ( ProjectsService . assignMaterialAssembly ( superman , 'mid' , 'invalid-aid' ) ) . rejects . toThrow (
754+ new NotFoundException ( 'Assembly' , 'invalid-aid' )
755+ ) ;
756+ } ) ;
757+
758+ test ( 'assignment fails because the wbsElements do not match' , async ( ) => {
759+ const material = { ...prismaMaterial1 , wbsElementId : 1 , wbsElement : prismaWbsElement1 } ;
760+ vi . spyOn ( prisma . material , 'findUnique' ) . mockResolvedValue ( material ) ;
761+ const assembly = {
762+ ...prismaAssembly1 ,
763+ wbsElementId : 2 ,
764+ wbsElement : { ...prismaWbsElement1 , wbsElementId : 2 , projectNumber : 1 }
765+ } ;
766+ vi . spyOn ( prisma . assembly , 'findUnique' ) . mockResolvedValue ( assembly ) ;
767+ await expect ( ProjectsService . assignMaterialAssembly ( superman , 'mid' , 'aid' ) ) . rejects . toThrow (
768+ new HttpException ( 400 , `The WBS element of the material (1.2.0) and assembly (1.1.0) do not match` )
769+ ) ;
770+ } ) ;
771+
772+ test ( 'assignment successful' , async ( ) => {
773+ vi . spyOn ( prisma . material , 'findUnique' ) . mockResolvedValue ( prismaMaterial1 ) ;
774+ vi . spyOn ( prisma . assembly , 'findUnique' ) . mockResolvedValue ( prismaAssembly1 ) ;
775+ vi . spyOn ( prisma . project , 'findFirst' ) . mockResolvedValue ( prismaProject1 ) ;
776+
777+ const expectedUpdatedToolMaterial : Material = {
778+ ...prismaMaterial1 ,
779+ assemblyId : 'updated-aid'
780+ } ;
781+ vi . spyOn ( prisma . material , 'update' ) . mockResolvedValue ( expectedUpdatedToolMaterial ) ;
782+
783+ const updatedMaterial = await ProjectsService . assignMaterialAssembly ( aquaman , 'mid' , 'updated-aid' ) ;
784+ expect ( updatedMaterial ) . toBe ( expectedUpdatedToolMaterial ) ;
785+ } ) ;
786+ } ) ;
728787 describe ( 'Delete Assembly' , ( ) => {
729788 test ( 'Deleteing assembly fails because user is not an admin or head' , async ( ) => {
730789 await expect ( ProjectsService . deleteAssembly ( 'New Assembly' , theVisitor ) ) . rejects . toThrow (
@@ -733,6 +792,8 @@ describe('Projects', () => {
733792 } ) ;
734793
735794 test ( 'Deleting assembly fails if assembly does not exist' , async ( ) => {
795+ vi . spyOn ( prisma . assembly , 'findUnique' ) . mockResolvedValue ( null ) ;
796+
736797 await expect ( ProjectsService . deleteAssembly ( 'New Assembly' , batman ) ) . rejects . toThrow (
737798 new NotFoundException ( 'Assembly' , 'New Assembly' )
738799 ) ;
0 commit comments