@@ -15,6 +15,7 @@ describe('bmEquipmentController', () => {
1515 findOneAndUpdate : jest . fn ( ) ,
1616 create : jest . fn ( ) ,
1717 findByIdAndUpdate : jest . fn ( ) ,
18+ updateOne : jest . fn ( ) ,
1819 } ;
1920
2021 // Initialize controller with mock model
@@ -130,4 +131,148 @@ describe('bmEquipmentController', () => {
130131 expect ( mockRes . status ) . toHaveBeenCalledWith ( 201 ) ;
131132 } ) ;
132133 } ) ;
134+
135+ describe ( 'updateEquipmentById' , ( ) => {
136+ const validObjectId = '507f1f77bcf86cd799439011' ;
137+
138+ it ( 'should return 400 for invalid equipment ID' , async ( ) => {
139+ mockReq . params . equipmentId = 'invalid-id' ;
140+ mockReq . body = { purchaseStatus : 'Purchased' } ;
141+
142+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
143+
144+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 400 ) ;
145+ expect ( mockRes . send ) . toHaveBeenCalledWith ( { message : 'Invalid equipment ID.' } ) ;
146+ expect ( mockBuildingEquipment . updateOne ) . not . toHaveBeenCalled ( ) ;
147+ } ) ;
148+
149+ it ( 'should return 400 for invalid project ID when provided' , async ( ) => {
150+ mockReq . params . equipmentId = validObjectId ;
151+ mockReq . body = { projectId : 'not-valid' , purchaseStatus : 'Purchased' } ;
152+
153+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
154+
155+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 400 ) ;
156+ expect ( mockRes . send ) . toHaveBeenCalledWith ( { message : 'Invalid project ID.' } ) ;
157+ expect ( mockBuildingEquipment . updateOne ) . not . toHaveBeenCalled ( ) ;
158+ } ) ;
159+
160+ it ( 'should return 400 for invalid purchaseStatus enum' , async ( ) => {
161+ mockReq . params . equipmentId = validObjectId ;
162+ mockReq . body = { purchaseStatus : 'InvalidStatus' } ;
163+
164+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
165+
166+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 400 ) ;
167+ expect ( mockRes . send ) . toHaveBeenCalledWith (
168+ expect . objectContaining ( {
169+ message : expect . stringContaining ( 'Invalid purchaseStatus' ) ,
170+ } ) ,
171+ ) ;
172+ expect ( mockBuildingEquipment . updateOne ) . not . toHaveBeenCalled ( ) ;
173+ } ) ;
174+
175+ it ( 'should return 400 for invalid currentUsage enum' , async ( ) => {
176+ mockReq . params . equipmentId = validObjectId ;
177+ mockReq . body = { currentUsage : 'Broken' } ;
178+
179+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
180+
181+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 400 ) ;
182+ expect ( mockRes . send ) . toHaveBeenCalledWith (
183+ expect . objectContaining ( {
184+ message : expect . stringContaining ( 'Invalid currentUsage' ) ,
185+ } ) ,
186+ ) ;
187+ expect ( mockBuildingEquipment . updateOne ) . not . toHaveBeenCalled ( ) ;
188+ } ) ;
189+
190+ it ( 'should return 400 for invalid condition enum' , async ( ) => {
191+ mockReq . params . equipmentId = validObjectId ;
192+ mockReq . body = { condition : 'Unknown' } ;
193+
194+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
195+
196+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 400 ) ;
197+ expect ( mockRes . send ) . toHaveBeenCalledWith (
198+ expect . objectContaining ( {
199+ message : expect . stringContaining ( 'Invalid condition' ) ,
200+ } ) ,
201+ ) ;
202+ expect ( mockBuildingEquipment . updateOne ) . not . toHaveBeenCalled ( ) ;
203+ } ) ;
204+
205+ it ( 'should return 400 when no valid fields provided to update' , async ( ) => {
206+ mockReq . params . equipmentId = validObjectId ;
207+ mockReq . body = { unknownField : 'value' } ;
208+
209+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
210+
211+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 400 ) ;
212+ expect ( mockRes . send ) . toHaveBeenCalledWith ( {
213+ message : 'No valid fields provided to update.' ,
214+ } ) ;
215+ expect ( mockBuildingEquipment . updateOne ) . not . toHaveBeenCalled ( ) ;
216+ } ) ;
217+
218+ it ( 'should return 200 with updated equipment on success' , async ( ) => {
219+ const updatedEquipment = {
220+ _id : validObjectId ,
221+ purchaseStatus : 'Purchased' ,
222+ condition : 'Good' ,
223+ } ;
224+ mockReq . params . equipmentId = validObjectId ;
225+ mockReq . body = { purchaseStatus : 'Purchased' , condition : 'Good' } ;
226+
227+ mockBuildingEquipment . updateOne . mockResolvedValue ( { } ) ;
228+ const mockPopulate = jest . fn ( ) . mockReturnThis ( ) ;
229+ const mockExec = jest . fn ( ) . mockResolvedValue ( updatedEquipment ) ;
230+ mockBuildingEquipment . findById . mockReturnValue ( {
231+ populate : mockPopulate ,
232+ exec : mockExec ,
233+ } ) ;
234+
235+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
236+
237+ expect ( mockBuildingEquipment . updateOne ) . toHaveBeenCalledWith (
238+ { _id : validObjectId } ,
239+ { $set : expect . objectContaining ( { purchaseStatus : 'Purchased' , condition : 'Good' } ) } ,
240+ ) ;
241+ expect ( mockBuildingEquipment . findById ) . toHaveBeenCalledWith ( validObjectId ) ;
242+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 200 ) ;
243+ expect ( mockRes . send ) . toHaveBeenCalledWith ( updatedEquipment ) ;
244+ } ) ;
245+
246+ it ( 'should return 404 when equipment not found after update' , async ( ) => {
247+ mockReq . params . equipmentId = validObjectId ;
248+ mockReq . body = { condition : 'Good' } ;
249+
250+ mockBuildingEquipment . updateOne . mockResolvedValue ( { } ) ;
251+ const mockPopulate = jest . fn ( ) . mockReturnThis ( ) ;
252+ const mockExec = jest . fn ( ) . mockResolvedValue ( null ) ;
253+ mockBuildingEquipment . findById . mockReturnValue ( {
254+ populate : mockPopulate ,
255+ exec : mockExec ,
256+ } ) ;
257+
258+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
259+
260+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 404 ) ;
261+ expect ( mockRes . send ) . toHaveBeenCalledWith ( { message : 'Equipment not found.' } ) ;
262+ } ) ;
263+
264+ it ( 'should return 500 on updateOne error' , async ( ) => {
265+ mockReq . params . equipmentId = validObjectId ;
266+ mockReq . body = { condition : 'Good' } ;
267+
268+ mockBuildingEquipment . updateOne . mockRejectedValue ( new Error ( 'DB error' ) ) ;
269+
270+ await controller . updateEquipmentById ( mockReq , mockRes ) ;
271+
272+ expect ( mockRes . status ) . toHaveBeenCalledWith ( 500 ) ;
273+ expect ( mockRes . send ) . toHaveBeenCalledWith (
274+ expect . objectContaining ( { message : expect . any ( String ) } ) ,
275+ ) ;
276+ } ) ;
277+ } ) ;
133278} ) ;
0 commit comments