Add backend tests for filtering posts by emotions

- Co-authored-by: Mike Aono <aonomike@gmail.com>
This commit is contained in:
mattwr18 2019-09-09 16:35:11 +02:00
parent 48e696dde4
commit b89043166b
2 changed files with 63 additions and 5 deletions

View File

@ -91,7 +91,7 @@ afterEach(async () => {
})
describe('Post', () => {
const postQuery = gql`
const postQueryFilteredByCategories = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
@ -102,13 +102,28 @@ describe('Post', () => {
}
`
const postQueryFilteredByEmotions = gql`
query Post($filter: _PostFilter) {
Post(filter: $filter) {
id
emotions {
emotion
}
}
}
`
describe('can be filtered', () => {
it('by categories', async () => {
await Promise.all([
let post31, post32
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'] }),
])
})
it('by categories', async () => {
const expected = {
data: {
Post: [
@ -120,7 +135,50 @@ describe('Post', () => {
},
}
variables = { ...variables, filter: { categories_some: { id_in: ['cat9'] } } }
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
await expect(
query({ query: postQueryFilteredByCategories, variables }),
).resolves.toMatchObject(expected)
})
it('by emotions', async () => {
const expected = {
data: {
Post: [
{
id: 'p31',
emotions: [{ emotion: 'happy' }],
},
],
},
}
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),
},
},
)
})
})
})

View File

@ -30,7 +30,7 @@ export default function create() {
let { categories, categoryIds } = args
delete args.categories
delete args.categoryIds
if (categories && categoryIds) throw new Error('You provided both category and categoryIds')
if (categories && categoryIds) throw new Error('You provided both categories and categoryIds')
if (categoryIds)
categories = await Promise.all(categoryIds.map(id => neodeInstance.find('Category', id)))
categories = categories || (await Promise.all([factoryInstance.create('Category')]))