Written custom mutation and Jest tests for DeleteComment

This commit is contained in:
Wolfgang Huß 2019-05-31 17:42:04 +02:00
parent 5bec0f1d72
commit e63e4ad890
7 changed files with 289 additions and 254 deletions

View File

@ -56,7 +56,7 @@ export default {
return comment
},
DeleteComment: async (object, params, context, resolveInfo) => {
const socialMedia = await neo4jgraphql(object, params, context, resolveInfo, false)
const comment = await neo4jgraphql(object, params, context, resolveInfo, false)
return comment
},

View File

@ -95,13 +95,15 @@ describe('CreateComment', () => {
it('assigns the authenticated user as author', async () => {
await client.request(createCommentMutation, createCommentVariables)
const { User } = await client.request(`{
const { User } = await client.request(gql`
{
User(email: "test@example.org") {
comments {
content
}
}
}`)
}
`)
expect(User).toEqual([
{
@ -210,195 +212,83 @@ describe('CreateComment', () => {
})
})
// describe('DeleteComment', () => {
// const createCommentMutation = gql `
// mutation($postId: ID, $content: String!) {
// CreateComment(postId: $postId, content: $content) {
// id
// content
// }
// }
// `
// const deleteCommentMutation = gql `
// mutation($id: ID!) {
// DeleteComment(id: $id) {
// id
// content
// }
// }
// `
// const createPostMutation = gql `
// mutation($id: ID!, $title: String!, $content: String!) {
// CreatePost(id: $id, title: $title, content: $content) {
// id
// }
// }
// `
// const commentQueryForPostId = gql `
// query($content: String) {
// Comment(content: $content) {
// postId
// }
// }
// `
// describe('unauthenticated', () => {
// it('throws authorization error', async () => {
// deleteCommentVariables = {
// id: 'c1',
// }
// client = new GraphQLClient(host)
// await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow(
// 'Not Authorised',
// )
// })
// })
describe('DeleteComment', () => {
const createCommentMutation = gql`
mutation($postId: ID, $content: String!) {
CreateComment(postId: $postId, content: $content) {
id
content
}
}
`
const deleteCommentMutation = gql`
mutation($id: ID!) {
DeleteComment(id: $id) {
id
}
}
`
const createPostMutation = gql`
mutation($id: ID!, $title: String!, $content: String!) {
CreatePost(id: $id, title: $title, content: $content) {
id
}
}
`
describe('unauthenticated', () => {
it('throws authorization error', async () => {
deleteCommentVariables = {
id: 'c1',
}
client = new GraphQLClient(host)
await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow(
'Not Authorised',
)
})
})
// // describe('authenticated', () => {
// // let headers
// // beforeEach(async () => {
// // headers = await login({
// // email: 'test@example.org',
// // password: '1234'
// // })
// // client = new GraphQLClient(host, {
// // headers
// // })
// // createCommentVariables = {
// // postId: 'p1',
// // content: "I'm authorised to comment",
// // }
// // createPostVariables = {
// // id: 'p1',
// // title: 'post to comment on',
// // content: 'please comment on me',
// // }
// // await client.request(createPostMutation, createPostVariables)
// // })
describe('authenticated', () => {
let headers
beforeEach(async () => {
headers = await login({
email: 'test@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
createCommentVariables = {
id: 'c1',
postId: 'p1',
content: "I'm authorised to comment",
}
deleteCommentVariables = {
id: 'c1',
}
createPostVariables = {
id: 'p1',
title: 'post to comment on',
content: 'please comment on me',
}
await client.request(createPostMutation, createPostVariables)
})
// // it('creates a comment', async () => {
// // const expected = {
// // CreateComment: {
// // content: "I'm authorised to comment",
// // },
// // }
it('deletes the authors comment', async () => {
const { CreateComment } = await client.request(createCommentMutation, createCommentVariables)
// // await expect(
// // client.request(createCommentMutation, createCommentVariables),
// // ).resolves.toMatchObject(expected)
// // })
deleteCommentVariables = {
id: CreateComment.id,
}
const expected = {
DeleteComment: {
id: CreateComment.id,
},
}
await expect(
client.request(deleteCommentMutation, deleteCommentVariables),
).resolves.toMatchObject(expected)
})
// // it('assigns the authenticated user as author', async () => {
// // await client.request(createCommentMutation, createCommentVariables)
// // const {
// // User
// // } = await client.request(`{
// // User(email: "test@example.org") {
// // comments {
// // content
// // }
// // }
// // }`)
// // expect(User).toEqual([{
// // comments: [{
// // content: "I'm authorised to comment"
// // }]
// // }])
// // })
// // it('throw an error if an empty string is sent from the editor as content', async () => {
// // createCommentVariables = {
// // postId: 'p1',
// // content: '<p></p>',
// // }
// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow(
// // 'Comment must be at least 1 character long!',
// // )
// // })
// // it('throws an error if a comment sent from the editor does not contain a single character', async () => {
// // createCommentVariables = {
// // postId: 'p1',
// // content: '<p> </p>',
// // }
// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow(
// // 'Comment must be at least 1 character long!',
// // )
// // })
// // it('throws an error if postId is sent as an empty string', async () => {
// // createCommentVariables = {
// // postId: 'p1',
// // content: '',
// // }
// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow(
// // 'Comment must be at least 1 character long!',
// // )
// // })
// // it('throws an error if content is sent as an string of empty characters', async () => {
// // createCommentVariables = {
// // postId: 'p1',
// // content: ' ',
// // }
// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow(
// // 'Comment must be at least 1 character long!',
// // )
// // })
// // it('throws an error if postId is sent as an empty string', async () => {
// // createCommentVariablesSansPostId = {
// // postId: '',
// // content: 'this comment should not be created',
// // }
// // await expect(
// // client.request(createCommentMutation, createCommentVariablesSansPostId),
// // ).rejects.toThrow('Comment cannot be created without a post!')
// // })
// // it('throws an error if postId is sent as an string of empty characters', async () => {
// // createCommentVariablesSansPostId = {
// // postId: ' ',
// // content: 'this comment should not be created',
// // }
// // await expect(
// // client.request(createCommentMutation, createCommentVariablesSansPostId),
// // ).rejects.toThrow('Comment cannot be created without a post!')
// // })
// // it('throws an error if the post does not exist in the database', async () => {
// // createCommentVariablesWithNonExistentPost = {
// // postId: 'p2',
// // content: "comment should not be created cause the post doesn't exist",
// // }
// // await expect(
// // client.request(createCommentMutation, createCommentVariablesWithNonExistentPost),
// // ).rejects.toThrow('Comment cannot be created without a post!')
// // })
// // it('does not create the comment with the postId as an attribute', async () => {
// // const commentQueryVariablesByContent = {
// // content: "I'm authorised to comment",
// // }
// // await client.request(createCommentMutation, createCommentVariables)
// // const {
// // Comment
// // } = await client.request(
// // commentQueryForPostId,
// // commentQueryVariablesByContent,
// // )
// // expect(Comment).toEqual([{
// // postId: null
// // }])
// // })
// // })
// })
it.todo('throws an error if it tries to delete a comment not from this author')
})
})

View File

@ -1,13 +1,14 @@
import gql from 'graphql-tag'
import Factory from '../seed/factories'
import { GraphQLClient } from 'graphql-request'
import { host, login } from '../jest/helpers'
const factory = Factory()
describe('CreateSocialMedia', () => {
describe('SocialMedia', () => {
let client
let headers
const mutationC = `
const mutationC = gql`
mutation($url: String!) {
CreateSocialMedia(url: $url) {
id
@ -15,7 +16,7 @@ describe('CreateSocialMedia', () => {
}
}
`
const mutationD = `
const mutationD = gql`
mutation($id: ID!) {
DeleteSocialMedia(id: $id) {
id
@ -42,19 +43,28 @@ describe('CreateSocialMedia', () => {
describe('unauthenticated', () => {
it('throws authorization error', async () => {
client = new GraphQLClient(host)
const variables = { url: 'http://nsosp.org' }
const variables = {
url: 'http://nsosp.org',
}
await expect(client.request(mutationC, variables)).rejects.toThrow('Not Authorised')
})
})
describe('authenticated', () => {
beforeEach(async () => {
headers = await login({ email: 'test@example.org', password: '1234' })
client = new GraphQLClient(host, { headers })
headers = await login({
email: 'test@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
})
it('creates social media with correct URL', async () => {
const variables = { url: 'http://nsosp.org' }
const variables = {
url: 'http://nsosp.org',
}
await expect(client.request(mutationC, variables)).resolves.toEqual(
expect.objectContaining({
CreateSocialMedia: {
@ -66,11 +76,15 @@ describe('CreateSocialMedia', () => {
})
it('deletes social media', async () => {
const creationVariables = { url: 'http://nsosp.org' }
const creationVariables = {
url: 'http://nsosp.org',
}
const { CreateSocialMedia } = await client.request(mutationC, creationVariables)
const { id } = CreateSocialMedia
const deletionVariables = { id }
const deletionVariables = {
id,
}
const expected = {
DeleteSocialMedia: {
id: id,
@ -81,12 +95,16 @@ describe('CreateSocialMedia', () => {
})
it('rejects empty string', async () => {
const variables = { url: '' }
const variables = {
url: '',
}
await expect(client.request(mutationC, variables)).rejects.toThrow('Input is not a URL')
})
it('validates URLs', async () => {
const variables = { url: 'not-a-url' }
const variables = {
url: 'not-a-url',
}
await expect(client.request(mutationC, variables)).rejects.toThrow('Input is not a URL')
})
})

View File

@ -74,7 +74,6 @@ export default {
},
async deleteCommentCallback() {
try {
// XXX Make custom mutation and tests in the Backend !!!
var gqlMutation = gql`
mutation($id: ID!) {
DeleteComment(id: $id) {

View File

@ -1,7 +1,10 @@
<template>
<div>
<ds-flex v-if="Post && Post.length"
:width="{ base: '100%' }" gutter="base">
<ds-flex
v-if="Post && Post.length"
:width="{ base: '100%' }"
gutter="base"
>
<hc-post-card
v-for="(post, index) in uniq(Post)"
:key="post.id"
@ -20,8 +23,11 @@
primary
/>
</no-ssr>
<hc-load-more v-if="true"
:loading="$apollo.loading" @click="showMoreContributions" />
<hc-load-more
v-if="true"
:loading="$apollo.loading"
@click="showMoreContributions"
/>
</div>
</template>

View File

@ -1,8 +1,8 @@
<template>
<ds-card>
<h2 style="margin-bottom: .2em;">
Mehr Informationen
</h2>
Mehr Informationen
</h2>
<p>Hier findest du weitere infos zum Thema.</p>
<ds-space />
<h3>
@ -25,8 +25,10 @@ Mehr Informationen
<ds-icon name="tags" />Schlagwörter
</h3>
<div class="tags">
<ds-tag v-for="tag in post.tags"
:key="tag.id">
<ds-tag
v-for="tag in post.tags"
:key="tag.id"
>
<ds-icon name="tag" />
{{ tag.name }}
</ds-tag>
@ -34,8 +36,10 @@ Mehr Informationen
</template>
<h3>Verwandte Beiträge</h3>
<ds-section style="margin: 0 -1.5rem; padding: 1.5rem;">
<ds-flex v-if="post.relatedContributions && post.relatedContributions.length"
gutter="small">
<ds-flex
v-if="post.relatedContributions && post.relatedContributions.length"
gutter="small"
>
<hc-post-card
v-for="(relatedPost, index) in post.relatedContributions"
:key="relatedPost.id"
@ -44,8 +48,12 @@ gutter="small">
@deletePost="post.relatedContributions.splice(index, 1)"
/>
</ds-flex>
<hc-empty v-else
margin="large" icon="file" message="No related Posts" />
<hc-empty
v-else
margin="large"
icon="file"
message="No related Posts"
/>
</ds-section>
<ds-space margin-bottom="large" />
</ds-card>

View File

@ -3,15 +3,27 @@
<ds-card v-if="user && user.image">
<p>PROFILE IMAGE</p>
</ds-card>
<ds-space/>
<ds-flex v-if="user" :width="{ base: '100%' }" gutter="base">
<ds-space />
<ds-flex
v-if="user"
:width="{ base: '100%' }"
gutter="base"
>
<ds-flex-item :width="{ base: '100%', sm: 2, md: 2, lg: 1 }">
<ds-card
:class="{'disabled-content': user.disabled}"
style="position: relative; height: auto;"
>
<hc-upload v-if="myProfile" :user="user"/>
<hc-avatar v-else :user="user" class="profile-avatar" size="x-large"/>
<hc-upload
v-if="myProfile"
:user="user"
/>
<hc-avatar
v-else
:user="user"
class="profile-avatar"
size="x-large"
/>
<no-ssr>
<content-menu
placement="bottom-end"
@ -23,32 +35,54 @@
/>
</no-ssr>
<ds-space margin="small">
<ds-heading tag="h3" align="center" no-margin>{{ userName }}</ds-heading>
<ds-text v-if="user.location" align="center" color="soft" size="small">
<ds-icon name="map-marker"/>
<ds-heading
tag="h3"
align="center"
no-margin
>
{{ userName }}
</ds-heading>
<ds-text
v-if="user.location"
align="center"
color="soft"
size="small"
>
<ds-icon name="map-marker" />
{{ user.location.name }}
</ds-text>
<ds-text
align="center"
color="soft"
size="small"
>{{ $t('profile.memberSince') }} {{ user.createdAt | date('MMMM yyyy') }}</ds-text>
>
{{ $t('profile.memberSince') }} {{ user.createdAt | date('MMMM yyyy') }}
</ds-text>
</ds-space>
<ds-space v-if="user.badges && user.badges.length" margin="x-small">
<hc-badges :badges="user.badges"/>
<ds-space
v-if="user.badges && user.badges.length"
margin="x-small"
>
<hc-badges :badges="user.badges" />
</ds-space>
<ds-flex>
<ds-flex-item>
<no-ssr>
<ds-number :label="$t('profile.followers')">
<hc-count-to slot="count" :end-val="followedByCount"/>
<hc-count-to
slot="count"
:end-val="followedByCount"
/>
</ds-number>
</no-ssr>
</ds-flex-item>
<ds-flex-item>
<no-ssr>
<ds-number :label="$t('profile.following')">
<hc-count-to slot="count" :end-val="Number(user.followingCount) || 0"/>
<hc-count-to
slot="count"
:end-val="Number(user.followingCount) || 0"
/>
</ds-number>
</no-ssr>
</ds-flex-item>
@ -64,69 +98,136 @@
</ds-space>
<template v-if="user.about">
<hr>
<ds-space margin-top="small" margin-bottom="small">
<ds-text color="soft" size="small">{{ user.about }}</ds-text>
<ds-space
margin-top="small"
margin-bottom="small"
>
<ds-text
color="soft"
size="small"
>
{{ user.about }}
</ds-text>
</ds-space>
</template>
</ds-card>
<ds-space/>
<ds-heading tag="h3" soft style="text-align: center; margin-bottom: 10px;">Netzwerk</ds-heading>
<ds-space />
<ds-heading
tag="h3"
soft
style="text-align: center; margin-bottom: 10px;"
>
Netzwerk
</ds-heading>
<ds-card style="position: relative; height: auto;">
<ds-space v-if="user.following && user.following.length" margin="x-small">
<ds-text tag="h5" color="soft">Wem folgt {{ userName | truncate(15) }}?</ds-text>
<ds-space
v-if="user.following && user.following.length"
margin="x-small"
>
<ds-text
tag="h5"
color="soft"
>
Wem folgt {{ userName | truncate(15) }}?
</ds-text>
</ds-space>
<template v-if="user.following && user.following.length">
<ds-space v-for="follow in uniq(user.following)" :key="follow.id" margin="x-small">
<ds-space
v-for="follow in uniq(user.following)"
:key="follow.id"
margin="x-small"
>
<!-- TODO: find better solution for rendering errors -->
<no-ssr>
<user :user="follow" :trunc="15"/>
<user
:user="follow"
:trunc="15"
/>
</no-ssr>
</ds-space>
<ds-space v-if="user.followingCount - user.following.length" margin="small">
<ds-space
v-if="user.followingCount - user.following.length"
margin="small"
>
<ds-text
size="small"
color="softer"
>und {{ user.followingCount - user.following.length }} weitere</ds-text>
>
und {{ user.followingCount - user.following.length }} weitere
</ds-text>
</ds-space>
</template>
<template v-else>
<p style="text-align: center; opacity: .5;">{{ userName }} folgt niemandem</p>
<p style="text-align: center; opacity: .5;">
{{ userName }} folgt niemandem
</p>
</template>
</ds-card>
<ds-space/>
<ds-space />
<ds-card style="position: relative; height: auto;">
<ds-space v-if="user.followedBy && user.followedBy.length" margin="x-small">
<ds-text tag="h5" color="soft">Wer folgt {{ userName | truncate(15) }}?</ds-text>
<ds-space
v-if="user.followedBy && user.followedBy.length"
margin="x-small"
>
<ds-text
tag="h5"
color="soft"
>
Wer folgt {{ userName | truncate(15) }}?
</ds-text>
</ds-space>
<template v-if="user.followedBy && user.followedBy.length">
<ds-space v-for="follow in uniq(user.followedBy)" :key="follow.id" margin="x-small">
<ds-space
v-for="follow in uniq(user.followedBy)"
:key="follow.id"
margin="x-small"
>
<!-- TODO: find better solution for rendering errors -->
<no-ssr>
<user :user="follow" :trunc="15"/>
<user
:user="follow"
:trunc="15"
/>
</no-ssr>
</ds-space>
<ds-space v-if="user.followedByCount - user.followedBy.length" margin="small">
<ds-space
v-if="user.followedByCount - user.followedBy.length"
margin="small"
>
<ds-text
size="small"
color="softer"
>und {{ user.followedByCount - user.followedBy.length }} weitere</ds-text>
>
und {{ user.followedByCount - user.followedBy.length }} weitere
</ds-text>
</ds-space>
</template>
<template v-else>
<p style="text-align: center; opacity: .5;">niemand folgt {{ userName }}</p>
<p style="text-align: center; opacity: .5;">
niemand folgt {{ userName }}
</p>
</template>
</ds-card>
<ds-space v-if="user.socialMedia && user.socialMedia.length" margin="large">
<ds-space
v-if="user.socialMedia && user.socialMedia.length"
margin="large"
>
<ds-card style="position: relative; height: auto;">
<ds-space margin="x-small">
<ds-text
tag="h5"
color="soft"
>{{ $t('profile.socialMedia') }} {{ user.name | truncate(15) }}?</ds-text>
>
{{ $t('profile.socialMedia') }} {{ user.name | truncate(15) }}?
</ds-text>
<template>
<ds-space v-for="link in socialMediaLinks" :key="link.username" margin="x-small">
<ds-space
v-for="link in socialMediaLinks"
:key="link.username"
margin="x-small"
>
<a :href="link.url">
<ds-avatar :image="link.favicon"/>
<ds-avatar :image="link.favicon" />
{{ 'link.username' }}
</a>
</ds-space>
@ -136,7 +237,10 @@
</ds-space>
</ds-flex-item>
<ds-flex-item :width="{ base: '100%', sm: 3, md: 5, lg: 3 }">
<ds-flex :width="{ base: '100%' }" gutter="small">
<ds-flex
:width="{ base: '100%' }"
gutter="small"
>
<ds-flex-item class="profile-top-navigation">
<ds-card class="ds-tab-nav">
<ds-flex>
@ -145,7 +249,10 @@
<!-- TODO: find better solution for rendering errors -->
<no-ssr>
<ds-number :label="$t('common.post', null, user.contributionsCount)">
<hc-count-to slot="count" :end-val="user.contributionsCount"/>
<hc-count-to
slot="count"
:end-val="user.contributionsCount"
/>
</ds-number>
</no-ssr>
</ds-space>
@ -197,11 +304,18 @@
</template>
<template v-else>
<ds-flex-item :width="{ base: '100%' }">
<hc-empty margin="xx-large" icon="file"/>
<hc-empty
margin="xx-large"
icon="file"
/>
</ds-flex-item>
</template>
</ds-flex>
<hc-load-more v-if="hasMore" :loading="$apollo.loading" @click="showMoreContributions"/>
<hc-load-more
v-if="hasMore"
:loading="$apollo.loading"
@click="showMoreContributions"
/>
</ds-flex-item>
</ds-flex>
</div>