From 126a406d166031e6b7db9d55155f92f00d5849b9 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Sat, 12 Apr 2025 06:32:06 +0100 Subject: [PATCH] fix(api): update unit tests --- .../repositories/block.repository.spec.ts | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/api/src/chat/repositories/block.repository.spec.ts b/api/src/chat/repositories/block.repository.spec.ts index 7a799faf..6006f3a4 100644 --- a/api/src/chat/repositories/block.repository.spec.ts +++ b/api/src/chat/repositories/block.repository.spec.ts @@ -54,6 +54,7 @@ describe('BlockRepository', () => { validCategory = '64def5678abc123490fedcba'; category = (await categoryRepository.findOne({ label: 'default' }))!; + hasPreviousBlocks = (await blockRepository.findOne({ name: 'hasPreviousBlocks', }))!; @@ -171,42 +172,38 @@ describe('BlockRepository', () => { describe('prepareBlocksInCategoryUpdateScope', () => { it('should update blocks within the scope based on category and ids', async () => { - jest.spyOn(blockRepository, 'findOne').mockResolvedValue({ - id: blockValidIds[0], - category: 'oldCategory', - nextBlocks: [blockValidIds[1]], - attachedBlock: blockValidIds[1], - } as Block); - - const mockUpdateOne = jest.spyOn(blockRepository, 'updateOne'); + jest.spyOn(blockRepository, 'find').mockResolvedValue([ + { + id: blockValidIds[0], + category: category.id, + nextBlocks: [blockValidIds[1]], + attachedBlock: blockValidIds[2], + }, + ] as Block[]); + jest.spyOn(blockRepository, 'updateOne'); await blockRepository.prepareBlocksInCategoryUpdateScope( validCategory, blockValidIds, ); - expect(mockUpdateOne).toHaveBeenCalledWith(blockValidIds[0], { + expect(blockRepository.updateOne).toHaveBeenCalledWith(blockValidIds[0], { nextBlocks: [blockValidIds[1]], - attachedBlock: blockValidIds[1], + attachedBlock: blockValidIds[2], }); }); it('should not update blocks if the category already matches', async () => { - jest.spyOn(blockRepository, 'findOne').mockResolvedValue({ - id: validIds[0], - category: validCategory, - nextBlocks: [], - attachedBlock: null, - } as unknown as Block); - - const mockUpdateOne = jest.spyOn(blockRepository, 'updateOne'); + jest.spyOn(blockRepository, 'find').mockResolvedValue([]); + jest.spyOn(blockRepository, 'updateOne'); await blockRepository.prepareBlocksInCategoryUpdateScope( - validCategory, - validIds, + category.id, + blockValidIds, ); - expect(mockUpdateOne).not.toHaveBeenCalled(); + expect(blockRepository.find).toHaveBeenCalled(); + expect(blockRepository.updateOne).not.toHaveBeenCalled(); }); });