import test from 'ava'; import { AuthenticationError } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import { createContext } from '../../../test/utils/create-mock-context'; import * as testUtils from '../../../test/utils'; import { defineUserAbility } from '../../../server/authorization/user-authorization'; import { RecipeCollection } from '../collection-model'; import { User } from '../../../app/user'; import { Recipe } from '../../../app/recipe'; import { fetchRecipeCollection } from '../schema/fetch-collection'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); // // Tests // test.serial('Should return a RecipeCollection', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const collection = await Factory.create( 'collection', {}, { user, recipeCount: 1 } ); const dbCollection = await fetchRecipeCollection( null, { id: collection.id }, context, null ); // Returned correct collection t.truthy(dbCollection instanceof RecipeCollection); t.is(dbCollection.id, collection.id); t.true(dbCollection.nodes[0].recipe instanceof Recipe); }); test.serial( 'Should return null when the user does not own the collection', async t => { const user = await Factory.create('user'); const collection = await Factory.create('collection'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const fetchedCollection = await fetchRecipeCollection( null, { id: collection.id, }, context, null ); t.is(fetchedCollection, null); } ); test.serial( 'Should return an error when the user is not logged in', async t => { const context = createContext({ state: { user: null, abilities: defineUserAbility(null) }, }); const collection = await Factory.create('collection'); await t.throwsAsync(async () => { return fetchRecipeCollection( null, { id: collection.id, }, context, null ); }, AuthenticationError); } );