Add tests, validations for too many categories

- Co-authored-by: Joseph Ngugi <jngugi88@gmail.com>
This commit is contained in:
Matt Rider 2019-08-19 17:31:55 +02:00
parent 3b09c31cbb
commit 555c5254bc
2 changed files with 53 additions and 38 deletions

View File

@ -40,10 +40,12 @@ const validateUpdateComment = async (resolve, root, args, context, info) => {
return resolve(root, args, context, info) return resolve(root, args, context, info)
} }
const validateCreatePost = async (resolve, root, args, context, info) => { const validatePost = async (resolve, root, args, context, info) => {
const { categoryIds } = args const { categoryIds } = args
if (!Array.isArray(categoryIds) || !categoryIds.length) { if (!Array.isArray(categoryIds) || !categoryIds.length || categoryIds.length > 3) {
throw new UserInputError('You cannot create a post without at least one category') throw new UserInputError(
'You cannot save a post without at least one category or more than three',
)
} }
return resolve(root, args, context, info) return resolve(root, args, context, info)
} }
@ -52,6 +54,7 @@ export default {
Mutation: { Mutation: {
CreateComment: validateCommentCreation, CreateComment: validateCommentCreation,
UpdateComment: validateUpdateComment, UpdateComment: validateUpdateComment,
CreatePost: validateCreatePost, CreatePost: validatePost,
UpdatePost: validatePost,
}, },
} }

View File

@ -19,6 +19,7 @@ const oldTitle = 'Old title'
const oldContent = 'Old content' const oldContent = 'Old content'
const newTitle = 'New title' const newTitle = 'New title'
const newContent = 'New content' const newContent = 'New content'
const postSaveError = 'You cannot save a post without at least one category or more than three'
const categoryIds = ['cat9', 'cat4', 'cat15'] const categoryIds = ['cat9', 'cat4', 'cat15']
let createPostVariables let createPostVariables
@ -88,6 +89,11 @@ beforeEach(async () => {
name: 'Consumption & Sustainability', name: 'Consumption & Sustainability',
icon: 'shopping-cart', icon: 'shopping-cart',
}), }),
factory.create('Category', {
id: 'cat27',
name: 'Animal Protection',
icon: 'paw',
}),
]) ])
createPostVariables = { createPostVariables = {
id: 'p3589', id: 'p3589',
@ -187,14 +193,21 @@ describe('CreatePost', () => {
it('throws an error if categoryIds is not an array', async () => { it('throws an error if categoryIds is not an array', async () => {
createPostVariables.categoryIds = null createPostVariables.categoryIds = null
await expect(client.request(createPostMutation, createPostVariables)).rejects.toThrow( await expect(client.request(createPostMutation, createPostVariables)).rejects.toThrow(
'You cannot create a post without at least one category', postSaveError,
) )
}) })
it('requires at least one category for successful creation', async () => { it('requires at least one category for successful creation', async () => {
createPostVariables.categoryIds = [] createPostVariables.categoryIds = []
await expect(client.request(createPostMutation, createPostVariables)).rejects.toThrow( await expect(client.request(createPostMutation, createPostVariables)).rejects.toThrow(
'You cannot create a post without at least one category', postSaveError,
)
})
it('allows a maximum of three category for successful update', async () => {
createPostVariables.categoryIds = ['cat9', 'cat27', 'cat15', 'cat4']
await expect(client.request(createPostMutation, createPostVariables)).rejects.toThrow(
postSaveError,
) )
}) })
@ -219,31 +232,16 @@ describe('CreatePost', () => {
}) })
describe('UpdatePost', () => { describe('UpdatePost', () => {
let updatePostMutation
let updatePostVariables let updatePostVariables
const updatePostMutation = gql`
mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) {
UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) {
id
content
}
}
`
beforeEach(async () => { beforeEach(async () => {
await Promise.all([
factory.create('Category', {
id: 'cat9',
name: 'Democracy & Politics',
icon: 'university',
}),
factory.create('Category', {
id: 'cat4',
name: 'Environment & Nature',
icon: 'tree',
}),
factory.create('Category', {
id: 'cat15',
name: 'Consumption & Sustainability',
icon: 'shopping-cart',
}),
factory.create('Category', {
id: 'cat27',
name: 'Animal Protection',
icon: 'paw',
}),
])
const asAuthor = Factory() const asAuthor = Factory()
await asAuthor.create('User', authorParams) await asAuthor.create('User', authorParams)
await asAuthor.authenticateAs(authorParams) await asAuthor.authenticateAs(authorParams)
@ -253,14 +251,6 @@ describe('UpdatePost', () => {
content: oldContent, content: oldContent,
categoryIds, categoryIds,
}) })
updatePostMutation = gql`
mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) {
UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) {
id
content
}
}
`
updatePostVariables = { updatePostVariables = {
id: 'p1', id: 'p1',
@ -301,6 +291,7 @@ describe('UpdatePost', () => {
}) })
it('updates a post', async () => { it('updates a post', async () => {
updatePostVariables.categoryIds = ['cat9']
const expected = { UpdatePost: { id: 'p1', content: newContent } } const expected = { UpdatePost: { id: 'p1', content: newContent } }
await expect(client.request(updatePostMutation, updatePostVariables)).resolves.toEqual( await expect(client.request(updatePostMutation, updatePostVariables)).resolves.toEqual(
expected, expected,
@ -328,6 +319,27 @@ describe('UpdatePost', () => {
client.request(postQueryWithCategories, postQueryWithCategoriesVariables), client.request(postQueryWithCategories, postQueryWithCategoriesVariables),
).resolves.toEqual({ Post: [{ categories: expect.arrayContaining(expected) }] }) ).resolves.toEqual({ Post: [{ categories: expect.arrayContaining(expected) }] })
}) })
it('throws an error if categoryIds is not an array', async () => {
updatePostVariables.categoryIds = null
await expect(client.request(updatePostMutation, updatePostVariables)).rejects.toThrow(
postSaveError,
)
})
it('requires at least one category for successful update', async () => {
updatePostVariables.categoryIds = []
await expect(client.request(updatePostMutation, updatePostVariables)).rejects.toThrow(
postSaveError,
)
})
it('allows a maximum of three category for a successful update', async () => {
updatePostVariables.categoryIds = ['cat9', 'cat27', 'cat15', 'cat4']
await expect(client.request(updatePostMutation, updatePostVariables)).rejects.toThrow(
postSaveError,
)
})
}) })
}) })
}) })
@ -400,7 +412,7 @@ describe('emotions', () => {
postQueryAction, postQueryAction,
postToEmote, postToEmote,
postToEmoteNode postToEmoteNode
const PostsEmotionsCountQuery = ` const PostsEmotionsCountQuery = gql`
query($id: ID!) { query($id: ID!) {
Post(id: $id) { Post(id: $id) {
emotionsCount emotionsCount