From b89043166b89844094a4221f4a7edd5ef9e4b9c7 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Mon, 9 Sep 2019 16:35:11 +0200 Subject: [PATCH] Add backend tests for filtering posts by emotions - Co-authored-by: Mike Aono --- backend/src/schema/resolvers/posts.spec.js | 66 ++++++++++++++++++++-- backend/src/seed/factories/posts.js | 2 +- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 7c5e88d69..af2d8089c 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -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), + }, + }, + ) }) }) }) diff --git a/backend/src/seed/factories/posts.js b/backend/src/seed/factories/posts.js index e81251c53..3058204a1 100644 --- a/backend/src/seed/factories/posts.js +++ b/backend/src/seed/factories/posts.js @@ -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')]))