import ava, { TestInterface } from 'ava'; import { Mongoose } from 'mongoose'; import { ForbiddenError } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { User } from '../../user'; import { Recipe } from '../../recipe'; import { createContext } from '../../../test/utils/create-mock-context'; import { gqlCall } from '../../../test/utils/gqlCall'; import { defineUserAbility } from '../../../server/authorization/user-authorization'; import { isIngredientItem, isIngredientGroup } from '../utils'; // // Setup // const test = ava as TestInterface<{ db: Mongoose }>; test.before(async t => { await testUtils.setupDB(t); }); test.afterEach.always(async t => { await testUtils.cleanupDB(t); }); test.after.always(async t => { await testUtils.tearDownDB(t); }); // ** Constants const removeIngredientMutation = ` mutation RemoveIngredient($input: RemoveIngredientInput!) { removeIngredient(input: $input) { id node { name } } } `; // // Tests // test.serial( 'Should property remove and return a single IngredientItem', async t => { const user = await Factory.create('user'); const recipe = await Factory.create( 'recipe', { ownerId: user.id }, { ingredientCount: 1 } ); const ingredient = recipe.ingredientList.nodes[0]; const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); let ingredientCount = await recipe.ingredientList.nodes.length; t.is(ingredientCount, 1); const response = await gqlCall({ source: removeIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, ingredientId: ingredient.id, }, }, }); // Properly returned the new ingredient t.falsy(response.errors); if (isIngredientItem(ingredient)) { t.is(response.data.removeIngredient.node.name, ingredient.node.name); } else { t.fail('got an ingredient group :('); } // Ingrdient was removed from recipe doc ingredientCount = await Recipe.findById(recipe.id).then(r => { return r.ingredientList.nodes.length; }); t.is(ingredientCount, 0); } ); test.serial( 'Should remove and return an IngredientItem within an IngredientGroup', async t => { const user = await Factory.create('user'); const recipe = await Factory.create( 'recipe', { ownerId: user.id }, { ingredientGroupCount: 3, ingredientCount: 0 } ); const ingredientGroup = recipe.ingredientList.nodes[1]; const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); if (!isIngredientGroup(ingredientGroup)) { t.fail(); return; } const removeIngredient = ingredientGroup.nodes[0]; if (!isIngredientItem(removeIngredient)) { t.fail(); return; } const ingredientCount = ingredientGroup.nodes.length; const response = await gqlCall({ source: removeIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, ingredientId: removeIngredient.id, }, }, }); // Properly returned the removed ingredient t.falsy(response.errors); t.is(response.data.removeIngredient.id, removeIngredient.id); // Ingredient was removed from IngredientGroup const newIngredientCount = await Recipe.findById(recipe.id).then(r => { const ingGroup = r.ingredientList.nodes[1]; return isIngredientGroup(ingGroup) ? ingGroup.nodes.length : t.fail(); }); t.is(newIngredientCount, ingredientCount - 1); } ); test.serial( 'Should return an error if you dont have permission to edit the recipe', async t => { const user = await Factory.create('user'); const recipe = await Factory.create('recipe'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: removeIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, ingredientId: recipe.ingredientList.nodes[0].id, }, }, }); // Should retrun forbidden error t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } );