import test from 'ava'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { RecipeCollection } from '../collection-model'; import { Recipe } from '../../recipe'; import { User } from '../../../app/user'; import { createContext } from '../../../test/utils/create-mock-context'; import { defineUserAbility } from '../../../server/authorization/user-authorization'; import { AuthenticationError } from 'apollo-server-micro'; import { removeRecipesFromCollection } from '../schema/remove-recipes'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); // // Tests // test.serial('Should remove Recipes from 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, recipeCount: 2 } ); const updatedCollection = await removeRecipesFromCollection( null, { input: { recipeCollectionId: collection.id, recipes: [collection.nodes[0].recipe.toHexString()], }, }, context, null ); const dbCollection = await RecipeCollection.findById(collection.id); t.is(updatedCollection.recipeCollection.nodes.length, 1); t.is(updatedCollection.removedRecipes.length, 1); t.is(dbCollection.nodes.length, 1); }); test.serial( 'Should return 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, recipeCount: 2 } ); const updatedCollection = await removeRecipesFromCollection( null, { input: { recipeCollectionId: collection.id, recipes: [collection.nodes[0].recipe.toHexString()], }, }, context, null ); t.true( updatedCollection.recipeCollection.nodes[0].recipe instanceof Recipe ); } ); test.serial('Should reject Recipes that are not in 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, recipeCount: 2 } ); const recipe = await Factory.create('recipe', { ownerId: user.id }); const response = await removeRecipesFromCollection( null, { input: { recipeCollectionId: collection.id, recipes: [recipe.id], }, }, context, null ); t.is(response.rejectedRecipes[0], recipe.id); }); 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 removeRecipesFromCollection( null, { input: { recipeCollectionId: '', recipes: [], }, }, context, null ); }, AuthenticationError); } );