Merge pull request #1542 from Human-Connection/1538_test-filter-by-followed-by

Test filter by followed by
This commit is contained in:
Robert Schäfer 2019-09-11 13:29:00 +02:00 committed by GitHub
commit a01c73536f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 126 additions and 184 deletions

View File

@ -1,120 +0,0 @@
import { gql } from '../../jest/helpers'
import Factory from '../../seed/factories'
import { createTestClient } from 'apollo-server-testing'
import createServer from '../../server'
import { neode as getNeode, getDriver } from '../../bootstrap/neo4j'
const factory = Factory()
const neode = getNeode()
const driver = getDriver()
let authenticatedUser
let user
let query
const currentUserParams = {
id: 'u1',
email: 'you@example.org',
name: 'This is you',
password: '1234',
}
const followedAuthorParams = {
id: 'u2',
email: 'followed@example.org',
name: 'Followed User',
password: '1234',
}
const randomAuthorParams = {
email: 'someone@example.org',
name: 'Someone else',
password: 'else',
}
const categoryIds = ['cat9']
beforeEach(async () => {
const [currentUser, followedAuthor, randomAuthor] = await Promise.all([
factory.create('User', currentUserParams),
factory.create('User', followedAuthorParams),
factory.create('User', randomAuthorParams),
])
user = currentUser
await neode.create('Category', {
id: 'cat9',
name: 'Democracy & Politics',
icon: 'university',
})
await currentUser.relateTo(followedAuthor, 'following')
await factory.create('Post', {
author: followedAuthor,
title: 'This is the post of a followed user',
categoryIds,
})
await factory.create('Post', {
author: randomAuthor,
title: 'This is some random post',
categoryIds,
})
})
beforeAll(() => {
const { server } = createServer({
context: () => {
return {
driver,
neode,
user: authenticatedUser,
}
},
})
const client = createTestClient(server)
query = client.query
})
afterEach(async () => {
await factory.cleanDatabase()
})
describe('Filter posts by author is followed by sb.', () => {
describe('given an authenticated user', () => {
beforeEach(async () => {
authenticatedUser = await user.toJson()
})
describe('no filter bubble', () => {
it('returns all posts', async () => {
const postQuery = gql`
{
Post(filter: {}) {
title
}
}
`
const expected = {
data: {
Post: [
{ title: 'This is some random post' },
{ title: 'This is the post of a followed user' },
],
},
}
await expect(query({ query: postQuery })).resolves.toMatchObject(expected)
})
})
describe('filtering for posts of followed users only', () => {
it('returns only posts authored by followed users', async () => {
const postQuery = gql`
{
Post(filter: { author: { followedBy_some: { id: "u1" } } }) {
title
}
}
`
const expected = {
data: { Post: [{ title: 'This is the post of a followed user' }] },
}
await expect(query({ query: postQuery })).resolves.toMatchObject(expected)
})
})
})
})

View File

@ -56,7 +56,7 @@ beforeAll(() => {
beforeEach(async () => {
variables = {}
user = await factory.create('User', {
id: 'u198',
id: 'current-user',
name: 'TestUser',
email: 'test@example.org',
password: '1234',
@ -91,44 +91,63 @@ afterEach(async () => {
})
describe('Post', () => {
const postQueryFilteredByCategories = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
categories {
id
}
}
}
`
const postQueryFilteredByEmotions = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
emotions {
emotion
}
}
}
`
describe('can be filtered', () => {
let post31, post32
let followedUser, happyPost, cryPost
beforeEach(async () => {
;[post31, post32] = await Promise.all([
factory.create('Post', { id: 'p31', categoryIds: ['cat4'] }),
factory.create('Post', { id: 'p32', categoryIds: ['cat15'] }),
factory.create('Post', { id: 'p33', categoryIds: ['cat9'] }),
;[followedUser] = await Promise.all([
factory.create('User', {
id: 'followed-by-me',
email: 'followed@example.org',
name: 'Followed User',
password: '1234',
}),
])
;[happyPost, cryPost] = await Promise.all([
factory.create('Post', { id: 'happy-post', categoryIds: ['cat4'] }),
factory.create('Post', { id: 'cry-post', categoryIds: ['cat15'] }),
factory.create('Post', {
id: 'post-by-followed-user',
categoryIds: ['cat9'],
author: followedUser,
}),
])
})
describe('no filter', () => {
it('returns all posts', async () => {
const postQueryNoFilters = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
}
}
`
const expected = [{ id: 'happy-post' }, { id: 'cry-post' }, { id: 'post-by-followed-user' }]
variables = { filter: {} }
await expect(query({ query: postQueryNoFilters, variables })).resolves.toMatchObject({
data: {
Post: expect.arrayContaining(expected),
},
})
})
})
it('by categories', async () => {
const postQueryFilteredByCategories = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
categories {
id
}
}
}
`
const expected = {
data: {
Post: [
{
id: 'p33',
id: 'post-by-followed-user',
categories: [{ id: 'cat9' }],
},
],
@ -140,45 +159,88 @@ describe('Post', () => {
).resolves.toMatchObject(expected)
})
it('by emotions', async () => {
describe('by emotions', () => {
const postQueryFilteredByEmotions = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
emotions {
emotion
}
}
}
`
it('filters by single emotion', async () => {
const expected = {
data: {
Post: [
{
id: 'happy-post',
emotions: [{ emotion: 'happy' }],
},
],
},
}
await user.relateTo(happyPost, 'emoted', { emotion: 'happy' })
variables = { ...variables, filter: { emotions_some: { emotion_in: ['happy'] } } }
await expect(
query({ query: postQueryFilteredByEmotions, variables }),
).resolves.toMatchObject(expected)
})
it('filters by multiple emotions', async () => {
const expected = [
{
id: 'happy-post',
emotions: [{ emotion: 'happy' }],
},
{
id: 'cry-post',
emotions: [{ emotion: 'cry' }],
},
]
await user.relateTo(happyPost, 'emoted', { emotion: 'happy' })
await user.relateTo(cryPost, 'emoted', { emotion: 'cry' })
variables = { ...variables, filter: { emotions_some: { emotion_in: ['happy', 'cry'] } } }
await expect(
query({ query: postQueryFilteredByEmotions, variables }),
).resolves.toMatchObject({
data: {
Post: expect.arrayContaining(expected),
},
})
})
})
it('by followed-by', async () => {
const postQueryFilteredByUsersFollowed = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
author {
id
name
}
}
}
`
await user.relateTo(followedUser, 'following')
variables = { filter: { author: { followedBy_some: { id: 'current-user' } } } }
const expected = {
data: {
Post: [
{
id: 'p31',
emotions: [{ emotion: 'happy' }],
id: 'post-by-followed-user',
author: { id: 'followed-by-me', name: 'Followed User' },
},
],
},
}
await user.relateTo(post31, 'emoted', { emotion: 'happy' })
variables = { ...variables, filter: { emotions_some: { emotion_in: ['happy'] } } }
await expect(query({ query: postQueryFilteredByEmotions, variables })).resolves.toMatchObject(
expected,
)
})
it('supports filtering by multiple emotions', async () => {
const expected = [
{
id: 'p31',
emotions: [{ emotion: 'happy' }],
},
{
id: 'p32',
emotions: [{ emotion: 'cry' }],
},
]
await user.relateTo(post31, 'emoted', { emotion: 'happy' })
await user.relateTo(post32, 'emoted', { emotion: 'cry' })
variables = { ...variables, filter: { emotions_some: { emotion_in: ['happy', 'cry'] } } }
await expect(query({ query: postQueryFilteredByEmotions, variables })).resolves.toMatchObject(
{
data: {
Post: expect.arrayContaining(expected),
},
},
)
await expect(
query({ query: postQueryFilteredByUsersFollowed, variables }),
).resolves.toMatchObject(expected)
})
})
})
@ -656,7 +718,7 @@ describe('emotions', () => {
const expected = {
data: {
AddPostEmotions: {
from: { id: 'u198' },
from: { id: 'current-user' },
to: { id: 'p1376' },
emotion: 'happy',
},
@ -690,8 +752,8 @@ describe('emotions', () => {
Post: [
{
emotions: expect.arrayContaining([
{ emotion: 'happy', User: { id: 'u198' } },
{ emotion: 'surprised', User: { id: 'u198' } },
{ emotion: 'happy', User: { id: 'current-user' } },
{ emotion: 'surprised', User: { id: 'current-user' } },
]),
},
],
@ -795,7 +857,7 @@ describe('emotions', () => {
data: {
RemovePostEmotions: {
to: { id: 'p1376' },
from: { id: 'u198' },
from: { id: 'current-user' },
emotion: 'cry',
},
},