@@ -117,6 +117,9 @@ describe('MessagePushingTaskService', () => {
117
117
expect ( result ) . toEqual ( tasks ) ;
118
118
expect ( repository . find ) . toHaveBeenCalledWith ( {
119
119
where : {
120
+ isDeleted : {
121
+ $ne : true ,
122
+ } ,
120
123
ownerId : mockOwnerId ,
121
124
surveys : { $all : [ surveyId ] } ,
122
125
triggerHook : hook ,
@@ -144,9 +147,19 @@ describe('MessagePushingTaskService', () => {
144
147
where : {
145
148
ownerId : mockOwnerId ,
146
149
_id : new ObjectId ( taskId ) ,
150
+ isDeleted : {
151
+ $ne : true ,
152
+ } ,
147
153
} ,
148
154
} ) ;
149
155
} ) ;
156
+ it ( 'should throw an error when message pushing task is not found' , async ( ) => {
157
+ const taskId = '65afc62904d5db18534c0f78' ;
158
+ jest . spyOn ( repository , 'findOne' ) . mockResolvedValue ( null ) ; // 模拟未找到任务
159
+ const mockOwnerId = '66028642292c50f8b71a9eee' ;
160
+
161
+ await expect ( service . findOne ( { id : taskId , ownerId : mockOwnerId } ) ) ;
162
+ } ) ;
150
163
} ) ;
151
164
152
165
describe ( 'update' , ( ) => {
@@ -183,6 +196,20 @@ describe('MessagePushingTaskService', () => {
183
196
} ) ;
184
197
expect ( repository . save ) . toHaveBeenCalledWith ( updatedTask ) ;
185
198
} ) ;
199
+ it ( 'should throw an error if the task to be updated is not found' , async ( ) => {
200
+ const taskId = '65afc62904d5db18534c0f78' ;
201
+ const updateDto : UpdateMessagePushingTaskDto = { name : 'Updated Task' } ;
202
+ jest . spyOn ( repository , 'findOne' ) . mockResolvedValue ( null ) ; // 模拟任务未找到
203
+ const mockOwnerId = '66028642292c50f8b71a9eee' ;
204
+
205
+ await expect (
206
+ service . update ( {
207
+ ownerId : mockOwnerId ,
208
+ id : taskId ,
209
+ updateData : updateDto ,
210
+ } ) ,
211
+ ) . rejects . toThrow ( `Message pushing task with id ${ taskId } not found` ) ;
212
+ } ) ;
186
213
} ) ;
187
214
188
215
describe ( 'remove' , ( ) => {
@@ -204,16 +231,40 @@ describe('MessagePushingTaskService', () => {
204
231
expect ( result ) . toEqual ( updateResult ) ;
205
232
expect ( repository . updateOne ) . toHaveBeenCalledWith (
206
233
{
207
- ownerId : mockOperatorId ,
208
234
_id : new ObjectId ( taskId ) ,
209
235
} ,
210
236
{
211
237
$set : {
212
238
isDeleted : true ,
239
+ operatorId : mockOperatorId ,
240
+ operator : mockOperator ,
241
+ deletedAt : expect . any ( Date ) ,
213
242
} ,
214
243
} ,
215
244
) ;
216
245
} ) ;
246
+ it ( 'should throw an error if the task to be removed is not found' , async ( ) => {
247
+ const taskId = '65afc62904d5db18534c0f78' ;
248
+ jest
249
+ . spyOn ( repository , 'updateOne' )
250
+ . mockResolvedValue ( { modifiedCount : 0 } ) ; // 模拟删除失败
251
+ const mockOperatorId = '66028642292c50f8b71a9eee' ;
252
+ const mockOperator = 'mockOperator' ;
253
+
254
+ const result = await service . remove ( {
255
+ id : taskId ,
256
+ operatorId : mockOperatorId ,
257
+ operator : mockOperator ,
258
+ } ) ;
259
+
260
+ expect ( result . modifiedCount ) . toBe ( 0 ) ;
261
+ expect ( repository . updateOne ) . toHaveBeenCalledWith (
262
+ {
263
+ _id : new ObjectId ( taskId ) ,
264
+ } ,
265
+ expect . any ( Object ) ,
266
+ ) ;
267
+ } ) ;
217
268
} ) ;
218
269
219
270
describe ( 'surveyAuthorizeTask' , ( ) => {
@@ -243,7 +294,34 @@ describe('MessagePushingTaskService', () => {
243
294
$push : {
244
295
surveys : surveyId ,
245
296
} ,
297
+ $set : {
298
+ updatedAt : expect . any ( Date ) ,
299
+ } ,
300
+ } ,
301
+ ) ;
302
+ } ) ;
303
+ it ( 'should not add the surveyId if it already exists in the task' , async ( ) => {
304
+ const taskId = '65afc62904d5db18534c0f78' ;
305
+ const surveyId = '65af380475b64545e5277dd9' ;
306
+ const mockOwnerId = '66028642292c50f8b71a9eee' ;
307
+
308
+ jest
309
+ . spyOn ( repository , 'updateOne' )
310
+ . mockResolvedValue ( { modifiedCount : 0 } ) ; // 模拟重复添加
311
+ const result = await service . surveyAuthorizeTask ( {
312
+ taskId,
313
+ surveyId,
314
+ ownerId : mockOwnerId ,
315
+ } ) ;
316
+
317
+ expect ( result . modifiedCount ) . toBe ( 0 ) ;
318
+ expect ( repository . updateOne ) . toHaveBeenCalledWith (
319
+ {
320
+ _id : new ObjectId ( taskId ) ,
321
+ surveys : { $nin : [ surveyId ] } , // 确保只有不包含时才插入
322
+ ownerId : mockOwnerId ,
246
323
} ,
324
+ expect . any ( Object ) ,
247
325
) ;
248
326
} ) ;
249
327
} ) ;
0 commit comments