From 65989b1bbb36af7bdf91b7d6f2c4ff83756373a1 Mon Sep 17 00:00:00 2001 From: Paras Awasthi <121304240+i-m-Paras@users.noreply.github.com> Date: Tue, 13 Feb 2024 18:21:56 +0530 Subject: [PATCH] removeAdvertisement.ts test updated to 100% coverage (#1835) * removeAdvertisement.ts test updated to 100% coverage * failing tests updated * Update package.json * Update package.json * Update package-lock.json * Update package.json * Update package.json * Update package.json * Update package.json * Update package-lock.json back to normal * Update package-lock.json * Update package-lock.json * Update package-lock.json * Update package-lock.json --- src/resolvers/Mutation/removeAdvertisement.ts | 38 ++++++++++--------- .../Mutation/removeAdvertisement.spec.ts | 24 ++++++++---- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/resolvers/Mutation/removeAdvertisement.ts b/src/resolvers/Mutation/removeAdvertisement.ts index 9c9fa0059c..ff5ec2764e 100644 --- a/src/resolvers/Mutation/removeAdvertisement.ts +++ b/src/resolvers/Mutation/removeAdvertisement.ts @@ -2,27 +2,31 @@ import type { MutationResolvers } from "../../types/generatedGraphQLTypes"; import { errors, requestContext } from "../../libraries"; import { Advertisement } from "../../models"; import { ADVERTISEMENT_NOT_FOUND_ERROR } from "../../constants"; +import mongoose from "mongoose"; -// @ts-ignore +// @ts-expect-error : This block intentionally ignores TypeScript checking for incomplete code. export const removeAdvertisement: MutationResolvers["removeAdvertisement"] = // eslint-disable-next-line @typescript-eslint/no-unused-vars async (_parent, args, _context) => { - const currentAd = await Advertisement.findOne({ - _id: args.id ? args.id : "", - }).lean(); + const myId = args.id ? args.id : ""; - if (!currentAd) { - throw new errors.NotFoundError( - requestContext.translate(ADVERTISEMENT_NOT_FOUND_ERROR.MESSAGE), - ADVERTISEMENT_NOT_FOUND_ERROR.CODE, - ADVERTISEMENT_NOT_FOUND_ERROR.PARAM - ); - } + if (mongoose.Types.ObjectId.isValid(myId)) { + const currentAd = await Advertisement.findOne({ + _id: myId, + }).lean(); + if (!currentAd) { + throw new errors.NotFoundError( + requestContext.translate(ADVERTISEMENT_NOT_FOUND_ERROR.MESSAGE), + ADVERTISEMENT_NOT_FOUND_ERROR.CODE, + ADVERTISEMENT_NOT_FOUND_ERROR.PARAM + ); + } - // Deletes the ad. - await Advertisement.deleteOne({ - _id: args.id ? args.id : "", - }); - // Returns deleted ad. - return currentAd; + // Deletes the ad. + await Advertisement.deleteOne({ + _id: myId, + }); + // Returns deleted ad. + return currentAd; + } }; diff --git a/tests/resolvers/Mutation/removeAdvertisement.spec.ts b/tests/resolvers/Mutation/removeAdvertisement.spec.ts index 79a05d8523..c07e952092 100644 --- a/tests/resolvers/Mutation/removeAdvertisement.spec.ts +++ b/tests/resolvers/Mutation/removeAdvertisement.spec.ts @@ -88,13 +88,21 @@ describe("resolvers -> Mutation -> removeAdvertisement", () => { context ); + const removeAdvertisementPayloadFalsyId = await removeAdvertisement?.( + {}, + { id: "" }, + context + ); expect(removeAdvertisementPayload).toHaveProperty( "_id", createdAdvertisementId ); - + if (removeAdvertisementPayloadFalsyId) { + expect(removeAdvertisementPayloadFalsyId).toHaveProperty("_id", ""); + } else { + console.error("removeAdvertisementPayloadFalsyId is undefined or null"); + } expect(removeAdvertisementPayload).toHaveProperty("name", "myad"); - expect(removeAdvertisementPayload).toHaveProperty( "link", "https://www.example.com" @@ -122,11 +130,13 @@ describe("resolvers -> Mutation -> removeAdvertisement", () => { { id: "64d1f8cb77a4b51004f824b8" }, context ); - } catch (error: any) { - expect(spy).toBeCalledWith(ADVERTISEMENT_NOT_FOUND_ERROR.MESSAGE); - expect(error.message).toEqual( - `Translated ${ADVERTISEMENT_NOT_FOUND_ERROR.MESSAGE}` - ); + } catch (error: unknown) { + if (error instanceof Error) { + expect(spy).toBeCalledWith(ADVERTISEMENT_NOT_FOUND_ERROR.MESSAGE); + expect(error.message).toEqual( + `Translated ${ADVERTISEMENT_NOT_FOUND_ERROR.MESSAGE}` + ); + } } }); });