import ava, { TestInterface } from 'ava'; import { Mongoose } from 'mongoose'; import { ForbiddenError, UserInputError } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { User } from '../../../app/user'; import { Recipe } from '../../../app/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 addIngredientMutation = ` mutation AddIngredient($input: AddIngredientInput!) { addIngredient(input: $input) { id node { name } } } `; // // Tests // test.serial('Should property save and return a single ingredient', async t => { const user = await Factory.create('user'); const recipe = await Factory.create( 'recipe', { ownerId: user.id }, { ingredientCount: 0 } ); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); let ingredientCount = await recipe.ingredientList.nodes.length; t.is(ingredientCount, 0); const response = await gqlCall({ source: addIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, ingredient: { name: 'Rump Roast', quantity: 3, measurement: 'POUND', note: 'extra beefy', }, }, }, }); // Properly returned the new ingredient t.is(response.data.addIngredient.node.name, 'Rump Roast'); // Ingrdient was added to recipe model ingredientCount = await Recipe.findById(recipe.id).then(r => { return r.ingredientList.nodes.length; }); t.is(ingredientCount, 1); }); test.serial( 'Should property save and return an Ingredient into a group', async t => { const user = await Factory.create('user'); const recipe = await Factory.create( 'recipe', { ownerId: user.id }, { ingredientCount: 0, ingredientGroupCount: 1 } ); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const ingredientGroup = recipe.ingredientList.nodes[0]; if (!isIngredientGroup(ingredientGroup)) { t.fail(); return; } let ingredientCount = ingredientGroup.nodes.length; const response = await gqlCall({ source: addIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, groupId: ingredientGroup.id, ingredient: { name: 'Rump Roast', quantity: 3, measurement: 'POUND', note: 'extra beefy', }, }, }, }); // Properly returned the new ingredient t.is(response.data.addIngredient.node.name, 'Rump Roast'); // Ingrdient was added to the group const newIngredientCount = await Recipe.findById(recipe.id).then(r => { const ig = r.ingredientList.nodes.id(ingredientGroup.id); return isIngredientGroup(ig) ? ig.nodes.length : t.fail(); }); t.is(newIngredientCount, ingredientCount + 1); } ); test.serial( 'Should throw an error when the IngredientGroup doesnt exist', async t => { const user = await Factory.create('user'); const recipe = await Factory.create('recipe', { ownerId: user.id }); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: addIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, groupId: 'incorrect-id', ingredient: { name: 'Rump Roast', quantity: 3, measurement: 'POUND', note: 'extra beefy', }, }, }, }); // Should retrun forbidden error t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof UserInputError); } ); test.serial( 'Should add the ingredient in the correct order position', async t => { const user = await Factory.create('user'); const recipe = await Factory.create( 'recipe', { ownerId: user.id, }, { ingredientCount: 4, } ); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); await gqlCall({ source: addIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, ingredient: { name: 'End', quantity: 1, measurement: 'EACH', note: 'should be last', }, }, }, }); await gqlCall({ source: addIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, ingredient: { name: 'Second', quantity: 1, measurement: 'EACH', note: 'should be 2nd', }, order: 1, }, }, }); const fetchedRecipe = await Recipe.findById(recipe.id).exec(); // last ingredient should be 'End' const ing1 = fetchedRecipe.ingredientList.nodes[ fetchedRecipe.ingredientList.nodes.length - 1 ]; if (isIngredientItem(ing1)) { t.is(ing1.node.name, 'End'); } else { t.fail(); } // Second ingredient should be 'Second' const ing2 = fetchedRecipe.ingredientList.nodes[1]; if (isIngredientItem(ing2)) { t.is(ing2.node.name, 'Second'); } else { t.fail(); } } ); 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: addIngredientMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, ingredient: { name: 'Rump Roast', quantity: 3, measurement: 'POUND', note: 'extra beefy', }, }, }, }); // Should retrun forbidden error t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } );