mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Add tests, validations for too many categories
- Co-authored-by: Joseph Ngugi <jngugi88@gmail.com>
This commit is contained in:
parent
3b09c31cbb
commit
555c5254bc
@ -40,10 +40,12 @@ const validateUpdateComment = async (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
|
||||
if (!Array.isArray(categoryIds) || !categoryIds.length) {
|
||||
throw new UserInputError('You cannot create a post without at least one category')
|
||||
if (!Array.isArray(categoryIds) || !categoryIds.length || categoryIds.length > 3) {
|
||||
throw new UserInputError(
|
||||
'You cannot save a post without at least one category or more than three',
|
||||
)
|
||||
}
|
||||
return resolve(root, args, context, info)
|
||||
}
|
||||
@ -52,6 +54,7 @@ export default {
|
||||
Mutation: {
|
||||
CreateComment: validateCommentCreation,
|
||||
UpdateComment: validateUpdateComment,
|
||||
CreatePost: validateCreatePost,
|
||||
CreatePost: validatePost,
|
||||
UpdatePost: validatePost,
|
||||
},
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ const oldTitle = 'Old title'
|
||||
const oldContent = 'Old content'
|
||||
const newTitle = 'New title'
|
||||
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']
|
||||
let createPostVariables
|
||||
|
||||
@ -88,6 +89,11 @@ beforeEach(async () => {
|
||||
name: 'Consumption & Sustainability',
|
||||
icon: 'shopping-cart',
|
||||
}),
|
||||
factory.create('Category', {
|
||||
id: 'cat27',
|
||||
name: 'Animal Protection',
|
||||
icon: 'paw',
|
||||
}),
|
||||
])
|
||||
createPostVariables = {
|
||||
id: 'p3589',
|
||||
@ -187,14 +193,21 @@ describe('CreatePost', () => {
|
||||
it('throws an error if categoryIds is not an array', async () => {
|
||||
createPostVariables.categoryIds = null
|
||||
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 () => {
|
||||
createPostVariables.categoryIds = []
|
||||
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', () => {
|
||||
let updatePostMutation
|
||||
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 () => {
|
||||
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()
|
||||
await asAuthor.create('User', authorParams)
|
||||
await asAuthor.authenticateAs(authorParams)
|
||||
@ -253,14 +251,6 @@ describe('UpdatePost', () => {
|
||||
content: oldContent,
|
||||
categoryIds,
|
||||
})
|
||||
updatePostMutation = gql`
|
||||
mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) {
|
||||
UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) {
|
||||
id
|
||||
content
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
updatePostVariables = {
|
||||
id: 'p1',
|
||||
@ -301,6 +291,7 @@ describe('UpdatePost', () => {
|
||||
})
|
||||
|
||||
it('updates a post', async () => {
|
||||
updatePostVariables.categoryIds = ['cat9']
|
||||
const expected = { UpdatePost: { id: 'p1', content: newContent } }
|
||||
await expect(client.request(updatePostMutation, updatePostVariables)).resolves.toEqual(
|
||||
expected,
|
||||
@ -328,6 +319,27 @@ describe('UpdatePost', () => {
|
||||
client.request(postQueryWithCategories, postQueryWithCategoriesVariables),
|
||||
).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,
|
||||
postToEmote,
|
||||
postToEmoteNode
|
||||
const PostsEmotionsCountQuery = `
|
||||
const PostsEmotionsCountQuery = gql`
|
||||
query($id: ID!) {
|
||||
Post(id: $id) {
|
||||
emotionsCount
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user