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 { Recipe } from '../../../app/recipe'; import { User } from '../../../app/user'; import { fetchRecipeCollections } from '../schema/fetch-collections'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); // // Tests // test.serial( 'Should return all RecipeCollections owned by active user', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); await Factory.createMany( 'collection', 2, {}, { user, recipeCount: 1 } ); const dbCollections = await fetchRecipeCollections(null, {}, context, null); // Returned correct collection t.is(dbCollections.length, 2); t.true(dbCollections[0] instanceof RecipeCollection); t.true(dbCollections[0].nodes[0].recipe instanceof Recipe); } ); test.serial( 'Should not return collections not owned by active user', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); await Factory.createMany('collection', 10, {}); const dbCollections = await fetchRecipeCollections(null, {}, context, null); // Returned correct collection t.is(dbCollections.length, 0); } ); test.serial( 'Should return empty array when the active user does not own any collections', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const fetchedCollection = await fetchRecipeCollections( null, {}, context, null ); t.is(fetchedCollection.length, 0); } ); test.serial('Should return an error when the no user is logged in', async t => { const context = createContext({ state: { user: null, abilities: defineUserAbility(null) }, }); await t.throwsAsync(async () => { return fetchRecipeCollections(null, {}, context, null); }, AuthenticationError); });