Write backend tests

This commit is contained in:
Wolfgang Huß 2019-08-14 18:05:13 +02:00
parent 43c2764fcf
commit e8c1a5fe8d

View File

@ -1,6 +1,6 @@
import { GraphQLClient } from 'graphql-request'
import Factory from '../../seed/factories'
import { host, login } from '../../jest/helpers'
import { host, login, gql } from '../../jest/helpers'
const factory = Factory()
let client
@ -18,22 +18,24 @@ afterEach(async () => {
await factory.cleanDatabase()
})
describe('Notification', () => {
const query = `{
Notification {
id
describe('query for notification', () => {
const notificationQuery = gql`
{
Notification {
id
}
}
}`
`
describe('unauthenticated', () => {
it('throws authorization error', async () => {
client = new GraphQLClient(host)
await expect(client.request(query)).rejects.toThrow('Not Authorised')
await expect(client.request(notificationQuery)).rejects.toThrow('Not Authorised')
})
})
})
describe('currentUser { notifications }', () => {
describe('currentUser notifications', () => {
const variables = {}
describe('authenticated', () => {
@ -58,108 +60,194 @@ describe('currentUser { notifications }', () => {
await Promise.all([
factory.create('User', neighborParams),
factory.create('Notification', {
id: 'not-for-you',
id: 'post-mention-not-for-you',
}),
factory.create('Notification', {
id: 'already-seen',
id: 'post-mention-already-seen',
read: true,
}),
factory.create('Notification', {
id: 'post-mention-unseen',
}),
factory.create('Notification', {
id: 'comment-mention-not-for-you',
}),
factory.create('Notification', {
id: 'comment-mention-already-seen',
read: true,
}),
factory.create('Notification', {
id: 'comment-mention-unseen',
}),
])
await factory.create('Notification', {
id: 'unseen',
})
await factory.authenticateAs(neighborParams)
await factory.create('Post', {
id: 'p1',
})
// Post and its notifications
await Promise.all([
factory.create('Post', {
id: 'p1',
}),
])
await Promise.all([
factory.relate('Notification', 'User', {
from: 'not-for-you',
from: 'post-mention-not-for-you',
to: 'neighbor',
}),
factory.relate('Notification', 'Post', {
from: 'p1',
to: 'not-for-you',
to: 'post-mention-not-for-you',
}),
factory.relate('Notification', 'User', {
from: 'unseen',
from: 'post-mention-unseen',
to: 'you',
}),
factory.relate('Notification', 'Post', {
from: 'p1',
to: 'unseen',
to: 'post-mention-unseen',
}),
factory.relate('Notification', 'User', {
from: 'already-seen',
from: 'post-mention-already-seen',
to: 'you',
}),
factory.relate('Notification', 'Post', {
from: 'p1',
to: 'already-seen',
to: 'post-mention-already-seen',
}),
])
// Comment and its notifications
await Promise.all([
factory.create('Comment', {
id: 'c1',
postId: 'p1',
}),
])
await Promise.all([
factory.relate('Notification', 'User', {
from: 'comment-mention-not-for-you',
to: 'neighbor',
}),
factory.relate('Notification', 'Comment', {
from: 'c1',
to: 'comment-mention-not-for-you',
}),
factory.relate('Notification', 'User', {
from: 'comment-mention-unseen',
to: 'you',
}),
factory.relate('Notification', 'Comment', {
from: 'c1',
to: 'comment-mention-unseen',
}),
factory.relate('Notification', 'User', {
from: 'comment-mention-already-seen',
to: 'you',
}),
factory.relate('Notification', 'Comment', {
from: 'c1',
to: 'comment-mention-already-seen',
}),
])
})
describe('filter for read: false', () => {
const query = `query($read: Boolean) {
currentUser {
notifications(read: $read, orderBy: createdAt_desc) {
id
post {
const queryCurrentUserNotificationsFilterRead = gql`
query($read: Boolean) {
currentUser {
notifications(read: $read, orderBy: createdAt_desc) {
id
post {
id
}
comment {
id
}
}
}
}
}`
const variables = { read: false }
`
const variables = {
read: false,
}
it('returns only unread notifications of current user', async () => {
const expected = {
currentUser: {
notifications: [
notifications: expect.arrayContaining([
{
id: 'unseen',
id: 'post-mention-unseen',
post: {
id: 'p1',
},
comment: null,
},
],
{
id: 'comment-mention-unseen',
post: null,
comment: {
id: 'c1',
},
},
]),
},
}
await expect(client.request(query, variables)).resolves.toEqual(expected)
await expect(
client.request(queryCurrentUserNotificationsFilterRead, variables),
).resolves.toEqual(expected)
})
})
describe('no filters', () => {
const query = `{
currentUser {
notifications(orderBy: createdAt_desc) {
id
post {
const queryCurrentUserNotifications = gql`
{
currentUser {
notifications(orderBy: createdAt_desc) {
id
post {
id
}
comment {
id
}
}
}
}
}`
`
it('returns all notifications of current user', async () => {
const expected = {
currentUser: {
notifications: [
notifications: expect.arrayContaining([
{
id: 'unseen',
id: 'post-mention-unseen',
post: {
id: 'p1',
},
comment: null,
},
{
id: 'already-seen',
id: 'post-mention-already-seen',
post: {
id: 'p1',
},
comment: null,
},
],
{
id: 'comment-mention-unseen',
comment: {
id: 'c1',
},
post: null,
},
{
id: 'comment-mention-already-seen',
comment: {
id: 'c1',
},
post: null,
},
]),
},
}
await expect(client.request(query, variables)).resolves.toEqual(expected)
await expect(client.request(queryCurrentUserNotifications, variables)).resolves.toEqual(
expected,
)
})
})
})
@ -167,17 +255,24 @@ describe('currentUser { notifications }', () => {
})
describe('UpdateNotification', () => {
const mutation = `mutation($id: ID!, $read: Boolean){
UpdateNotification(id: $id, read: $read) {
id read
const mutationUpdateNotification = gql`
mutation($id: ID!, $read: Boolean) {
UpdateNotification(id: $id, read: $read) {
id
read
}
}
}`
const variables = {
id: 'to-be-updated',
`
const variablesPostUpdateNotification = {
id: 'post-mention-to-be-updated',
read: true,
}
const variablesCommentUpdateNotification = {
id: 'comment-mention-to-be-updated',
read: true,
}
describe('given a notifications', () => {
describe('given some notifications', () => {
let headers
beforeEach(async () => {
@ -187,22 +282,47 @@ describe('UpdateNotification', () => {
password: '1234',
slug: 'mentioned',
}
await factory.create('User', mentionedParams)
await factory.create('Notification', {
id: 'to-be-updated',
})
await Promise.all([
factory.create('User', mentionedParams),
factory.create('Notification', {
id: 'post-mention-to-be-updated',
}),
factory.create('Notification', {
id: 'comment-mention-to-be-updated',
}),
])
await factory.authenticateAs(userParams)
await factory.create('Post', {
id: 'p1',
})
// Post and its notifications
await Promise.all([
factory.create('Post', {
id: 'p1',
}),
])
await Promise.all([
factory.relate('Notification', 'User', {
from: 'to-be-updated',
from: 'post-mention-to-be-updated',
to: 'mentioned-1',
}),
factory.relate('Notification', 'Post', {
from: 'p1',
to: 'to-be-updated',
to: 'post-mention-to-be-updated',
}),
])
// Comment and its notifications
await Promise.all([
factory.create('Comment', {
id: 'c1',
postId: 'p1',
}),
])
await Promise.all([
factory.relate('Notification', 'User', {
from: 'comment-mention-to-be-updated',
to: 'mentioned-1',
}),
factory.relate('Notification', 'Comment', {
from: 'p1',
to: 'comment-mention-to-be-updated',
}),
])
})
@ -210,7 +330,9 @@ describe('UpdateNotification', () => {
describe('unauthenticated', () => {
it('throws authorization error', async () => {
client = new GraphQLClient(host)
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
await expect(
client.request(mutationUpdateNotification, variablesPostUpdateNotification),
).rejects.toThrow('Not Authorised')
})
})
@ -226,7 +348,9 @@ describe('UpdateNotification', () => {
})
it('throws authorization error', async () => {
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
await expect(
client.request(mutationUpdateNotification, variablesPostUpdateNotification),
).rejects.toThrow('Not Authorised')
})
describe('and owner', () => {
@ -240,14 +364,28 @@ describe('UpdateNotification', () => {
})
})
it('updates notification', async () => {
it('updates post notification', async () => {
const expected = {
UpdateNotification: {
id: 'to-be-updated',
id: 'post-mention-to-be-updated',
read: true,
},
}
await expect(client.request(mutation, variables)).resolves.toEqual(expected)
await expect(
client.request(mutationUpdateNotification, variablesPostUpdateNotification),
).resolves.toEqual(expected)
})
it('updates comment notification', async () => {
const expected = {
UpdateNotification: {
id: 'comment-mention-to-be-updated',
read: true,
},
}
await expect(
client.request(mutationUpdateNotification, variablesCommentUpdateNotification),
).resolves.toEqual(expected)
})
})
})