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 '../../../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 { IngredientItem, IngredientListItemKind, } from '../ingredient-list-model'; 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 addIngredientGroupMutation = ` mutation AddIngredientGroup($input: AddIngredientGroupInput!) { addIngredientGroup(input: $input) { id heading } } `; // // Tests // test.serial('Should property save and return an ingredient group', 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: addIngredientGroupMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, heading: 'Ingredient Group', }, }, }); // Properly returned the new ingredient t.falsy(response.errors); t.is(response.data.addIngredientGroup.heading, 'Ingredient Group'); // Ingrdient was added to recipe model const ingredients = await Recipe.findById(recipe.id).then(r => { return r.ingredientList.nodes; }); t.is(ingredients.length, 1); t.is(ingredients[0].kind, IngredientListItemKind.GROUP); }); 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: addIngredientGroupMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, heading: 'Ingredient Group', }, }, }); // Should retrun forbidden error t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } ); 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: addIngredientGroupMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, heading: 'End', }, }, }); await gqlCall({ source: addIngredientGroupMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, heading: 'Second', 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 (isIngredientGroup(ing1)) { t.is(ing1.heading, 'End'); } else { t.fail(); } // Second ingredient should be 'Second' const ing2 = fetchedRecipe.ingredientList.nodes[1]; if (isIngredientGroup(ing2)) { t.is(ing2.heading, 'Second'); } else { t.fail(); } } );