import test from 'ava'; import { AuthenticationError, UserInputError } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { createContext } from '../../../test/utils/create-mock-context'; import { defineUserAbility } from '../../../server/authorization/user-authorization'; import { RecipeCollection } from '../collection-model'; import { Recipe } from '../../recipe'; import { User } from '../../../app/user'; import { updateRecipeInCollection } from '../schema/update-recipe'; import { SmartRecipeCollection } from '../smart-collection-model'; import { RecipeFilterAction, RecipeFilterAttribute } from '../filters'; import { updateFilterInCollection } from '../schema/update-filter'; import { RecipeFilterActionET } from '../schema'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); // // Tests // test.serial('Should udpate the filter correctly', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'smartCollection', {}, { user, filterCount: 3 } ); const updatedProps = { id: collection.filters[0].id as string, attribute: RecipeFilterAttribute.INGREDIENT, action: RecipeFilterAction.NOT_INCLUDE, value: 'New Value', }; const updatedCollection = await updateFilterInCollection( null, { input: { recipeCollectionId: collection.id, filter: updatedProps, }, }, context, null ); const dbCollection = await SmartRecipeCollection.findById(collection.id); t.is(updatedCollection.filters[0].value, updatedProps.value); t.is(updatedCollection.filters[0].attribute, updatedProps.attribute); t.is(updatedCollection.filters[0].action, updatedProps.action); t.is(dbCollection.filters[0].value, updatedProps.value); t.is(dbCollection.filters[0].attribute, updatedProps.attribute); t.is(dbCollection.filters[0].action, updatedProps.action); }); test.serial( 'Should udpate Filter order within a Recipe Collection', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'smartCollection', {}, { user, filterCount: 4 } ); const filter = collection.filters[0]; const updatedCollection = await updateFilterInCollection( null, { input: { filter: { id: filter.id, }, recipeCollectionId: collection.id, order: 2, }, }, context, null ); const dbCollection = await SmartRecipeCollection.findById(collection.id); t.is(updatedCollection.filters[2].id, filter.id); t.is(dbCollection.filters[2].id, filter.id); } ); test.serial( 'Should throw error when the recipe is not present 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 }); await t.throwsAsync(async () => { return updateRecipeInCollection( null, { input: { recipeCollectionId: collection.id, recipeId: recipe.id, order: 0, }, }, context, null ); }, UserInputError); } ); 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 updateFilterInCollection( null, { input: { recipeCollectionId: '', filter: { id: '', }, }, }, context, null ); }, AuthenticationError); } );