import ava, { TestInterface } from 'ava'; import { Mongoose } from 'mongoose'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { User } from '../../../app/user'; import { Recipe, RecipeProps } from '../../recipe'; import { createContext } from '../../../test/utils/create-mock-context'; import { gqlCall } from '../../../test/utils/gqlCall'; import { defineUserAbility } from '../../../server/authorization/user-authorization'; // // 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 recipesQuery = ` query { recipes { id } } `; // // Tests // // ** FetchAllRecipes test.serial( 'fetchAllRecipes: Should return an array of users recipes', async t => { const user = await Factory.create('user'); const recipe = await Factory.createMany('recipe', 2, { ownerId: user.id, }); // dummy recipes, should not be returned await Factory.createMany('recipe', 3); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: recipesQuery, contextValue: context, }); t.falsy(response.errors); t.is(response.data.recipes.length, 2); } ); test.todo('fetchAllRecipes: Should Throw an Error if no user is logged in');