Skip to content

Commit

Permalink
removeAdvertisement.ts test updated to 100% coverage (PalisadoesFound…
Browse files Browse the repository at this point in the history
…ation#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
  • Loading branch information
i-m-Paras authored Feb 13, 2024
1 parent cc21e65 commit 65989b1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
38 changes: 21 additions & 17 deletions src/resolvers/Mutation/removeAdvertisement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
24 changes: 17 additions & 7 deletions tests/resolvers/Mutation/removeAdvertisement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}`
);
}
}
});
});

0 comments on commit 65989b1

Please sign in to comment.