From 1f2aaf528a0a151bb15b2b382e6286b3cdf43907 Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Tue, 5 Dec 2023 09:43:11 +0530 Subject: [PATCH] chore: added new testcases for updateUserCollection method in user-collection module --- .../user-collection.service.spec.ts | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts b/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts index dfd8215d7..ceb8f3b45 100644 --- a/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts +++ b/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts @@ -11,6 +11,7 @@ import { USER_COLL_SHORT_TITLE, USER_COLL_ALREADY_ROOT, USER_NOT_OWNER, + USER_COLL_DATA_INVALID, } from 'src/errors'; import { PrismaService } from 'src/prisma/prisma.service'; import { PubSubService } from 'src/pubsub/pubsub.service'; @@ -1728,3 +1729,107 @@ describe('updateUserCollectionOrder', () => { ); }); }); + +describe('updateUserCollection', () => { + test('should throw USER_COLL_DATA_INVALID is collection data is invalid', async () => { + const result = await userCollectionService.updateUserCollection( + rootRESTUserCollection.title, + '{', + rootRESTUserCollection.id, + rootRESTUserCollection.userUid, + ); + expect(result).toEqualLeft(USER_COLL_DATA_INVALID); + }); + + test('should throw USER_COLL_SHORT_TITLE if title is invalid', async () => { + const result = await userCollectionService.updateUserCollection( + '', + JSON.stringify(rootRESTUserCollection.data), + rootRESTUserCollection.id, + rootRESTUserCollection.userUid, + ); + + expect(result).toEqualLeft(USER_COLL_SHORT_TITLE); + }); + + test('should throw USER_NOT_OWNER is user is not owner of collection', async () => { + // isOwnerCheck + mockPrisma.userCollection.findFirstOrThrow.mockRejectedValueOnce( + 'NotFoundError', + ); + + const result = await userCollectionService.updateUserCollection( + rootRESTUserCollection.title, + JSON.stringify(rootRESTUserCollection.data), + rootRESTUserCollection.id, + rootRESTUserCollection.userUid, + ); + + expect(result).toEqualLeft(USER_NOT_OWNER); + }); + + test('should throw USER_COLL_NOT_FOUND is collectionID is invalid', async () => { + // isOwnerCheck + mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce({ + ...rootRESTUserCollection, + }); + mockPrisma.userCollection.update.mockRejectedValueOnce('RecordNotFound'); + + const result = await userCollectionService.updateUserCollection( + rootRESTUserCollection.title, + JSON.stringify(rootRESTUserCollection.data), + 'invalid_id', + rootRESTUserCollection.userUid, + ); + expect(result).toEqualLeft(USER_COLL_NOT_FOUND); + }); + + test('should successfully update a collection', async () => { + // isOwnerCheck + mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce({ + ...rootRESTUserCollection, + }); + mockPrisma.userCollection.update.mockResolvedValueOnce( + rootRESTUserCollection, + ); + + const result = await userCollectionService.updateUserCollection( + 'new_title', + JSON.stringify({ foo: 'bar' }), + rootRESTUserCollection.id, + rootRESTUserCollection.userUid, + ); + + expect(result).toEqualRight({ + data: JSON.stringify({ foo: 'bar' }), + title: 'new_title', + ...rootRESTUserCollectionCasted, + }); + }); + + test('should send pubsub message to "user_coll//updated" when UserCollection is updated successfully', async () => { + // isOwnerCheck + mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce({ + ...rootRESTUserCollection, + }); + mockPrisma.userCollection.update.mockResolvedValueOnce( + rootRESTUserCollection, + ); + + const result = await userCollectionService.updateUserCollection( + 'new_title', + JSON.stringify({ foo: 'bar' }), + rootRESTUserCollection.id, + rootRESTUserCollection.userUid, + ); + + expect(mockPubSub.publish).toHaveBeenCalledWith( + `user_coll/${rootRESTUserCollectionCasted.userID}/updated`, + { + data: JSON.stringify({ foo: 'bar' }), + title: 'new_title', + ...rootRESTUserCollectionCasted, + }, + ); + }); +});