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