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 { 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 updateIngredientGroupMutation = ` mutation UpdateIngredientGroup($input: UpdateIngredientGroupInput!) { updateIngredientGroup(input: $input) { id heading order } } `; // // Tests // test.serial('Should property update an Ingredient Group', async t => { const user = await Factory.create('user'); const recipe = await Factory.create( 'recipe', { ownerId: user.id }, { ingredientGroupCount: 1, ingredientCount: 0 } ); const ingredientGroup = recipe.ingredientList.nodes[0]; const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateIngredientGroupMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, groupId: ingredientGroup.id, heading: 'New Heading', }, }, }); t.falsy(response.errors); // Properly returned the new IngredientGroup t.is(response.data.updateIngredientGroup.heading, 'New Heading'); // db is updated with new IngredientGroup const fetchedIngredient = await Recipe.findById(recipe.id).then(r => { return r.ingredientList.nodes.id(ingredientGroup.id); }); if (!isIngredientGroup(fetchedIngredient)) { t.fail('wrong ingredient type'); } else { t.is(fetchedIngredient.heading, 'New Heading'); } }); test.serial( 'Should property update the order of an IngredientGroup', async t => { const user = await Factory.create('user'); const recipe = await Factory.create( 'recipe', { ownerId: user.id }, { ingredientCount: 4, ingredientGroupCount: 1 } ); const ingredientGroup = recipe.ingredientList.nodes[recipe.ingredientList.nodes.length - 1]; const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateIngredientGroupMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, groupId: ingredientGroup.id, order: 1, }, }, }); // Properly returned the IngredientGroup t.falsy(response.errors); t.is(response.data.updateIngredientGroup.id, ingredientGroup.id); t.is(response.data.updateIngredientGroup.order, 1); // The ingredient is now located at index 1 (item #2) const fetchedIngredient = await Recipe.findById(recipe.id).then(r => { return r.ingredientList.nodes[1]; }); t.is(fetchedIngredient.id, ingredientGroup.id); } ); 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 ingredient = recipe.ingredientList.nodes[0]; const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateIngredientGroupMutation, contextValue: context, variableValues: { input: { recipeId: recipe.id, groupId: ingredient.id, }, }, }); // Should return forbidden error t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } );