Skip to content

Commit

Permalink
handles edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-0311 committed Jan 26, 2023
1 parent ba69d8b commit b6aafd5
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/resolvers/Mutation/updateUserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const updateUserProfile: MutationResolvers["updateUserProfile"] = async (
"email"
);
}
}// Upload file
} // Upload file
let uploadImageObj;
if (args.file) {
uploadImageObj = await uploadImage(args.file, null);
Expand All @@ -57,11 +57,18 @@ export const updateUserProfile: MutationResolvers["updateUserProfile"] = async (
{
_id: context.userId,
},
{ $set: {
{
$set: {
email: args.data?.email ? args.data.email : currentUser?.email,
firstName: args.data?.firstName ? args.data.firstName : currentUser?.firstName,
lastName: args.data?.lastName ? args.data.lastName : currentUser?.lastName,
image: uploadImageObj.imageAlreadyInDbPath ? uploadImageObj.imageAlreadyInDbPath : uploadImageObj.newImagePath,
firstName: args.data?.firstName
? args.data.firstName
: currentUser?.firstName,
lastName: args.data?.lastName
? args.data.lastName
: currentUser?.lastName,
image: uploadImageObj.imageAlreadyInDbPath
? uploadImageObj.imageAlreadyInDbPath
: uploadImageObj.newImagePath,
},
},
{
Expand All @@ -75,15 +82,13 @@ export const updateUserProfile: MutationResolvers["updateUserProfile"] = async (
},
{
$set: {

email: args.data?.email ? args.data.email : currentUser?.email,
firstName: args.data?.firstName
? args.data.firstName
: currentUser?.firstName,
lastName: args.data?.lastName
? args.data.lastName
: currentUser?.lastName,

},
},
{
Expand Down
176 changes: 176 additions & 0 deletions tests/resolvers/Mutation/updateUserProfile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,180 @@ describe("resolvers -> Mutation -> updateUserProfile", () => {
image: "imageAlreadyInDbPath",
});
});

it(`When Image is given, updates current user's user object and returns the object when only email is given`, async () => {
const uploadImage = await import("../../../src/utilities");

const spy = vi
.spyOn(uploadImage, "uploadImage")
.mockImplementationOnce(async () => {
return {
newImagePath: "newImagePath",
imageAlreadyInDbPath: "imageAlreadyInDbPath",
};
});

const args: MutationUpdateUserProfileArgs = {
data: {
email: `email${nanoid().toLowerCase()}@gmail.com`,
},
file: "newImageFile.png",
};

const context = {
userId: testUser._id,
};

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

const updateUserProfilePayload = await updateUserProfileResolverWImage?.(
{},
args,
context
);

expect(spy).toHaveBeenCalledTimes(1);
expect(updateUserProfilePayload).toEqual({
...testUser.toObject(),
email: args.data?.email,
firstName: "newFirstName",
lastName: "newLastName",
image: "imageAlreadyInDbPath",
});
});

it(`When Image is given, updates current user's user object and returns the object when only firstName is given`, async () => {
const uploadImage = await import("../../../src/utilities");

const spy = vi
.spyOn(uploadImage, "uploadImage")
.mockImplementationOnce(async () => {
return {
newImagePath: "newImagePath",
imageAlreadyInDbPath: "imageAlreadyInDbPath",
};
});

const args: MutationUpdateUserProfileArgs = {
data: {
firstName: "newFirstName1",
},
file: "newImageFile.png",
};

const context = {
userId: testUser._id,
};

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

const updateUserProfilePayload = await updateUserProfileResolverWImage?.(
{},
args,
context
);

const testUserobj = await User.findById({ _id: testUser.id });

expect(spy).toHaveBeenCalledTimes(1);
expect(updateUserProfilePayload).toEqual({
...testUser.toObject(),
email: testUserobj?.email,
firstName: "newFirstName1",
lastName: testUserobj?.lastName,
image: "imageAlreadyInDbPath",
});
});

it(`When Image is given, updates current user's user object and returns the object when only lastName is given`, async () => {
const uploadImage = await import("../../../src/utilities");

const spy = vi
.spyOn(uploadImage, "uploadImage")
.mockImplementationOnce(async () => {
return {
newImagePath: "newImagePath",
imageAlreadyInDbPath: "imageAlreadyInDbPath",
};
});

const args: MutationUpdateUserProfileArgs = {
data: {
lastName: "newLastName1",
},
file: "newImageFile.png",
};

const context = {
userId: testUser._id,
};

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

const updateUserProfilePayload = await updateUserProfileResolverWImage?.(
{},
args,
context
);

const testUserobj = await User.findById({ _id: testUser.id });

expect(spy).toHaveBeenCalledTimes(1);
expect(updateUserProfilePayload).toEqual({
...testUser.toObject(),
email: testUserobj?.email,
firstName: testUserobj?.firstName,
lastName: "newLastName1",
image: "imageAlreadyInDbPath",
});
});

it(`When Image is given, updates current user's user object and returns the object when only Image is not in DB path`, async () => {
const uploadImage = await import("../../../src/utilities");

const spy = vi
.spyOn(uploadImage, "uploadImage")
.mockImplementationOnce(async () => {
return {
newImagePath: "newImagePath",
imageAlreadyInDbPath: undefined,
};
});

const args: MutationUpdateUserProfileArgs = {
data: {},
file: "newImageFile.png",
};

const context = {
userId: testUser._id,
};

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

const updateUserProfilePayload = await updateUserProfileResolverWImage?.(
{},
args,
context
);

const testUserobj = await User.findById({ _id: testUser.id });

expect(spy).toHaveBeenCalledTimes(1);
expect(updateUserProfilePayload).toEqual({
...testUser.toObject(),
email: testUserobj?.email,
firstName: testUserobj?.firstName,
lastName: "newLastName1",
image: "newImagePath",
});
});
});

0 comments on commit b6aafd5

Please sign in to comment.