Validate comments length, presence/test

- Co-authored-by: Wolfgang Huss <wolle.huss@pjannto.com>
- Co-authored-by: Mike Aono <aonomike@gmail.com>
This commit is contained in:
Matt Rider 2019-04-24 09:11:33 -03:00
parent 23b0bbfbbe
commit 0078b743fe
2 changed files with 28 additions and 1 deletions

View File

@ -1,5 +1,7 @@
import { neo4jgraphql } from 'neo4j-graphql-js'
import { UserInputError } from 'apollo-server'
const COMMENT_MIN_LENGTH = 3
export default {
Query: {
CommentByPost: async (object, params, context, resolveInfo) => {
@ -23,6 +25,11 @@ export default {
},
Mutation: {
CreateComment: async (object, params, context, resolveInfo) => {
const content = params.content.replace(/<(?:.|\n)*?>/gm, '').trim()
if (!params.content || content.length < COMMENT_MIN_LENGTH) {
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} characters long!`)
}
const { postId } = params
delete params.postId
const comment = await neo4jgraphql(object, params, context, resolveInfo, false)

View File

@ -44,7 +44,7 @@ describe('CreateComment', () => {
client = new GraphQLClient(host, { headers })
})
it('creates a post', async () => {
it('creates a comment', async () => {
variables = {
postId: 'p1',
content: 'I\'m authorised to comment'
@ -57,5 +57,25 @@ describe('CreateComment', () => {
await expect(client.request(mutation, variables)).resolves.toMatchObject(expected)
})
it('throw an error if an empty string is sent as content', async () => {
variables = {
postId: 'p1',
content: '<p></p>'
}
await expect(client.request(mutation, variables))
.rejects.toThrow('Comment must be at least 3 characters long!')
})
it('throws an error if a comment is less than 3 characters', async () => {
variables = {
postId: 'p1',
content: '<p>ab</p>'
}
await expect(client.request(mutation, variables))
.rejects.toThrow('Comment must be at least 3 characters long!')
})
})
})