import test from 'ava'; import { AuthenticationError } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { createContext } from '../../../test/utils/create-mock-context'; import { defineUserAbility } from '../../../server/authorization/user-authorization'; import { RecipeCollection } from '../collection-model'; import { Recipe } from '../../recipe'; import { User } from '../../../app/user'; import { addRecipesToCollection } from '../schema/add-recipes'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); // // Tests // test.serial( 'Should return a RecipeCollection with populated Recipes', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'collection', {}, { user } ); const recipe = await Factory.create('recipe', { ownerId: user.id, }); const updatedCollection = await addRecipesToCollection( null, { input: { recipeCollectionId: collection.id, recipes: [recipe.id], }, }, context, null ); t.truthy( updatedCollection.recipeCollection.nodes[0].recipe instanceof Recipe ); } ); test.serial('Should add Recipes to a Recipe Collection', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'collection', {}, { user } ); const recipes = await Factory.createMany('recipe', 3, { ownerId: user.id, }); const updatedCollection = await addRecipesToCollection( null, { input: { recipeCollectionId: collection.id, recipes: recipes.map(r => r.id), }, }, context, null ); const dbCollection = await RecipeCollection.findById(collection.id); t.is(updatedCollection.recipeCollection.nodes.length, 3); t.is(dbCollection.nodes.length, 3); }); test.skip('Should add Recipes in the correct order', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'collection', {}, { user } ); const recipes = await Factory.createMany('recipe', 3, { ownerId: user.id, }); const updatedCollection = await addRecipesToCollection( null, { input: { recipeCollectionId: collection.id, recipes: recipes.map(r => r.id), }, }, context, null ); t.is(updatedCollection.nodes.length, 3); }); test.serial('Should not add invalid recipe ids to the collection', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'collection', {}, { user } ); // Recipe is not persisted to the DB, thus unable to be added const recipe = await Factory.build('recipe'); const response = await addRecipesToCollection( null, { input: { recipeCollectionId: collection.id, recipes: [recipe.id], }, }, context, null ); t.is(response.recipeCollection.nodes.length, 0); t.is(response.rejectedRecipes.length, 1); }); test.serial( 'Should not add a recipe when it does not belong to the user', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'collection', {}, { user } ); const recipe = await Factory.create('recipe'); const response = await addRecipesToCollection( null, { input: { recipeCollectionId: collection.id, recipes: [recipe.id], }, }, context, null ); t.is(response.recipeCollection.nodes.length, 0); t.is(response.rejectedRecipes.length, 1); } ); test.serial( 'Should return an error when the user is not logged in', async t => { const context = createContext({ state: { user: null, abilities: defineUserAbility(null) }, }); await t.throwsAsync(async () => { return addRecipesToCollection( null, { input: { recipeCollectionId: '', recipes: [], }, }, context, null ); }, AuthenticationError); } );