Last active
November 3, 2021 17:34
-
-
Save Tsugami/c580e84d02348b227c693ff87d2254e3 to your computer and use it in GitHub Desktop.
follow mutation test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { makeID, mutate } from '../../../../../tests/app'; | |
import { clearDatabase, closeDatabase, connectDatabase } from '../../../../../tests/mongo'; | |
import ERRORS from '../../../../errors'; | |
import { createUserFixture, createUserFixtureAndToken } from '../../../../../tests/user.factory'; | |
import folllowModel from '../../folllow.model'; | |
beforeAll(async () => await connectDatabase()); | |
beforeEach(async () => { | |
await clearDatabase(); | |
}); | |
afterAll(async () => await closeDatabase()); | |
const SAVE_LIST_ITEM_MUTATION = ` | |
mutation FollowUserTest($input: FollowUserInput!) { | |
FollowUser(input: $input) { | |
follow { | |
followerID | |
followingID | |
} | |
success | |
error | |
} | |
} | |
`; | |
describe('SaveEntryMediaListItem Mutation', () => { | |
it('should throws an error when the access token not is provided', async () => { | |
const response = await mutate( | |
SAVE_LIST_ITEM_MUTATION, | |
{ input: { userID: makeID('User') } }, | |
null, | |
); | |
expect(response.data.FollowUser).toBeNull(); | |
expect(JSON.stringify(response)).toMatch(ERRORS.UNAUTHORIZED); | |
}); | |
it('should throws an error when the invalid userID is provided', async () => { | |
const { accessToken } = await createUserFixtureAndToken(); | |
const response = await mutate( | |
SAVE_LIST_ITEM_MUTATION, | |
{ input: { userID: makeID('NotUser') } }, | |
accessToken, | |
); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.FollowUser.success).toBeFalsy(); | |
expect(response.data.FollowUser.error).toBe(ERRORS.INVALID_USER_ID); | |
expect(response.data.FollowUser.follow).toBeNull(); | |
}); | |
it('should throws an error when user try follow yourself', async () => { | |
const { accessToken, userID } = await createUserFixtureAndToken(); | |
const response = await mutate(SAVE_LIST_ITEM_MUTATION, { input: { userID } }, accessToken); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.FollowUser.success).toBeFalsy(); | |
expect(response.data.FollowUser.error).toBe(ERRORS.CANNOT_FOLLOW_YOURSELF); | |
expect(response.data.FollowUser.follow).toBeNull(); | |
}); | |
it("should throws an error when the provided user doesn't exist", async () => { | |
const { accessToken } = await createUserFixtureAndToken(); | |
const response = await mutate( | |
SAVE_LIST_ITEM_MUTATION, | |
{ input: { userID: makeID('User') } }, | |
accessToken, | |
); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.FollowUser.success).toBeFalsy(); | |
expect(response.data.FollowUser.error).toBe(ERRORS.USER_NOT_FOUND); | |
expect(response.data.FollowUser.follow).toBeNull(); | |
}); | |
it('should follow user with success', async () => { | |
const { accessToken, userID } = await createUserFixtureAndToken(); | |
const user = await createUserFixture({ username: 'follow', email: '[email protected]' }); | |
const followingID = makeID('User', user._id); | |
const response = await mutate( | |
SAVE_LIST_ITEM_MUTATION, | |
{ input: { userID: followingID } }, | |
accessToken, | |
); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.FollowUser.success).toBeTruthy(); | |
expect(response.data.FollowUser.error).toBeNull(); | |
expect(response.data.FollowUser.follow.followerID).toBe(userID); | |
expect(response.data.FollowUser.follow.followingID).toBe(followingID); | |
}); | |
it('should return success when auth user already follow', async () => { | |
const { accessToken, user: auth, userID } = await createUserFixtureAndToken(); | |
const user = await createUserFixture({ username: 'follow', email: '[email protected]' }); | |
const followingID = makeID('User', user._id); | |
await folllowModel.create({ followingID: user._id, followerID: auth._id }); | |
const response = await mutate( | |
SAVE_LIST_ITEM_MUTATION, | |
{ input: { userID: followingID } }, | |
accessToken, | |
); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.FollowUser.error).toBeNull(); | |
expect(response.data.FollowUser.follow.followerID).toBe(userID); | |
expect(response.data.FollowUser.follow.followingID).toBe(followingID); | |
expect(response.data.FollowUser.success).toBeTruthy(); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { errorField, successField } from '@entria/graphql-mongo-helpers'; | |
import { GraphQLNonNull, GraphQLString } from 'graphql'; | |
import { fromGlobalId, mutationWithClientMutationId } from 'graphql-relay'; | |
import { authenticatedMutation } from '../../../auth'; | |
import ERRORS from '../../../errors'; | |
import userModel from '../../user/user.model'; | |
import folllowModel from '../folllow.model'; | |
import FollowType from '../follow.type'; | |
export default mutationWithClientMutationId({ | |
inputFields: { | |
userID: { | |
type: new GraphQLNonNull(GraphQLString), | |
}, | |
}, | |
name: 'FollowUser', | |
mutateAndGetPayload: authenticatedMutation(async ({ userID }, ctx) => { | |
const { id: followingID, type } = fromGlobalId(userID); | |
if (type !== 'User') return { error: ERRORS.INVALID_USER_ID }; | |
if (String(followingID) === String(ctx.user.id)) { | |
return { error: ERRORS.CANNOT_FOLLOW_YOURSELF }; | |
} | |
const following = await userModel.exists({ _id: followingID }); | |
if (!following) { | |
return { error: ERRORS.USER_NOT_FOUND }; | |
} | |
const hasFollowed = await folllowModel.findOne({ | |
followerID: ctx.user._id, | |
followingID: followingID, | |
}); | |
if (hasFollowed) { | |
return { success: true, follow: hasFollowed }; | |
} | |
const follow = await folllowModel.create({ | |
followerID: ctx.user._id, | |
followingID: followingID, | |
}); | |
return { success: true, follow }; | |
}), | |
outputFields: { | |
follow: { | |
type: FollowType, | |
resolve: ({ follow }) => follow, | |
}, | |
...successField, | |
...errorField, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment