Skip to content

Commit

Permalink
Test file for updatePluginStatus.ts (#1682)
Browse files Browse the repository at this point in the history
* Improved code coverage

* Revert "Improved code coverage"

This reverts commit 1ac5f89.

* Tests for updatePluginStatus.ts file
  • Loading branch information
git-init-priyanshu authored Jan 16, 2024
1 parent 325c687 commit ad3e16e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export const ORGANIZATION_IMAGE_NOT_FOUND_ERROR = {
MESSAGE: "organizationImage.notFound",
PARAM: "organizationImage",
};
export const PLUGIN_NOT_FOUND = {
DESC: "Plugin not found",
CODE: "plugin.notFound",
MESSAGE: "plugin.notFound",
PARAM: "plugin",
};
export const POST_NOT_FOUND_ERROR = {
DESC: "Post not found",
CODE: "post.notFound",
Expand Down
11 changes: 8 additions & 3 deletions src/resolvers/Mutation/updatePluginStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { Plugin } from "../../models";
import mongoose from "mongoose";
import { errors, requestContext } from "../../libraries";
import { PLUGIN_NOT_FOUND } from "../../constants";

/**
* This function enables to update plugin install status.
Expand All @@ -20,8 +22,11 @@ export const updatePluginStatus: MutationResolvers["updatePluginStatus"] =
const plugin = await Plugin.findById(uid);

if (!plugin) {
console.log("Document not found");
return;
throw new errors.NotFoundError(
requestContext.translate(PLUGIN_NOT_FOUND.MESSAGE),
PLUGIN_NOT_FOUND.CODE,
PLUGIN_NOT_FOUND.PARAM
);
}

let uninstalledOrgsList = plugin.uninstalledOrgs;
Expand Down Expand Up @@ -51,7 +56,7 @@ export const updatePluginStatus: MutationResolvers["updatePluginStatus"] =

// calls subscription
context.pubsub.publish("TALAWA_PLUGIN_UPDATED", {
Plugin: res,
onPluginUpdate: res,
});
return res;
};
133 changes: 133 additions & 0 deletions tests/resolvers/Mutation/updatePluginStatus.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import type mongoose from "mongoose";
import { Types } from "mongoose";
import { nanoid } from "nanoid";
import {
beforeAll,
afterAll,
afterEach,
describe,
it,
expect,
vi,
} from "vitest";

import { PLUGIN_NOT_FOUND } from "../../../src/constants";
import { pubsub } from "../../../src/index";
import { createTestUserAndOrganization } from "../../helpers/userAndOrg";
import type { TestOrganizationType } from "../../helpers/userAndOrg";
import { connect, disconnect } from "../../helpers/db";
import type { MutationUpdatePluginStatusArgs } from "../../../src/types/generatedGraphQLTypes";
import { updatePluginStatus } from "../../../src/resolvers/Mutation/updatePluginStatus";
import { Plugin } from "../../../src/models";
import type { InterfacePlugin } from "../../../src/models";

let MONGOOSE_INSTANCE: typeof mongoose;
let testOrganization: TestOrganizationType;

beforeAll(async () => {
MONGOOSE_INSTANCE = await connect();
const temp = await createTestUserAndOrganization();
testOrganization = temp[1];
});

afterAll(async () => {
await disconnect(MONGOOSE_INSTANCE);
});

afterEach(() => {
vi.doUnmock("../../../src/constants");
vi.resetModules();
});

afterEach(() => {
vi.doUnmock("../../../src/constants");
vi.resetModules();
});

describe("resolvers -> Mutation -> updatePluginStatus", () => {
it(`throws NotFoundError if no plugin exists with _id === args.id`, async () => {
const { requestContext } = await import("../../../src/libraries");

const spy = vi
.spyOn(requestContext, "translate")
.mockImplementation((message) => `Translated ${message}`);

try {
const args: MutationUpdatePluginStatusArgs = {
id: Types.ObjectId().toString(),
orgId: Types.ObjectId().toString(),
};

const context = async () => {
pubsub;
};

const { updatePluginStatus: updatePluginStatusResolver } = await import(
"../../../src/resolvers/Mutation/updatePluginStatus"
);

await updatePluginStatusResolver?.({}, args, context);
} catch (error: any) {
expect(spy).toHaveBeenCalledWith(PLUGIN_NOT_FOUND.MESSAGE);
expect(error.message).toEqual(`Translated ${PLUGIN_NOT_FOUND.MESSAGE}`);
}
});

it(`updates the plugin with _id === args.id and returns the updated plugin when orgId does no exists in uninstalled orgs`, async () => {
const testPlugin: InterfacePlugin = await Plugin.create({
pluginName: `${nanoid()}`,
pluginCreatedBy: `${nanoid()}`,
pluginDesc: `${nanoid()}`,
uninstalledOrgs: [],
});
const args: MutationUpdatePluginStatusArgs = {
id: testPlugin._id.toString(),
orgId: testOrganization?._id.toString(),
};

const context = {
pubsub,
};

const updatePluginStatusPayload = await updatePluginStatus?.(
{},
args,
context
);

const testUpdatePluginStatusPayload = await Plugin.findOne({
_id: testPlugin._id,
}).lean();

expect(updatePluginStatusPayload).toEqual(testUpdatePluginStatusPayload);
});

it(`updates the plugin with _id === args.id and returns the updated plugin when orgId exists in uninstalled orgs`, async () => {
const testPlugin: InterfacePlugin = await Plugin.create({
pluginName: `${nanoid()}`,
pluginCreatedBy: `${nanoid()}`,
pluginDesc: `${nanoid()}`,
uninstalledOrgs: [testOrganization?._id],
});
const args: MutationUpdatePluginStatusArgs = {
id: testPlugin._id.toString(),
orgId: testOrganization?._id.toString(),
};

const context = {
pubsub,
};

const updatePluginStatusPayload = await updatePluginStatus?.(
{},
args,
context
);

const testUpdatePluginStatusPayload = await Plugin.findOne({
_id: testPlugin._id,
}).lean();

expect(updatePluginStatusPayload).toEqual(testUpdatePluginStatusPayload);
});
});

0 comments on commit ad3e16e

Please sign in to comment.