import test, { TestInterface } from 'ava'; import { Factory, RecipeFactoryOptions } from './factory'; import * as testUtils from '../test/utils'; import { RecipeProps, Recipe } from '../app/recipe'; import { User } from '../app/user'; import { RecipeCollection } from '../app/collection'; import { Types } from 'mongoose'; test.before(testUtils.setupDB); test.afterEach(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); test.serial('can create a valid recipe', async t => { const rcp = await Factory.create('recipe'); // returns recipe t.true(rcp instanceof Recipe); // recipe has ingredients t.truthy(rcp.ingredientList); // Ingredients has items t.truthy(rcp.ingredientList.nodes); // Has a user t.truthy(rcp.ownerId); }); test.serial('cleans up database after each test', async t => { // since the last test created a recipe, if count is 0 -> its been cleaned! const count = await Recipe.countDocuments((error: any, count: number) => { return count; }); t.true(count === 0); }); test.serial('Recipe generates the specified amount of Ingredients', async t => { const ingredientCount = 5; const rcp = await Factory.create('recipe', {}, { ingredientCount }); const ingredients = rcp.ingredientList.nodes; t.is(ingredients.length, ingredientCount); }); test.serial( 'Recipe generates the specified amount of IngredientGroups', async t => { const ingredientGroupCount = 2; const rcp = await Factory.create( 'recipe', {}, { ingredientGroupCount, ingredientCount: 0 } ); const ingredients = rcp.ingredientList.nodes; t.is(ingredients.length, ingredientGroupCount); } ); test.serial('recipe creates a linked user', async t => { const rcp = await Factory.create('recipe'); const user = await rcp.getOwner(); t.true(user instanceof User); }); // // Recipe Collection // test.serial('can create a valid collection', async t => { const collection = await Factory.create( 'collection', {}, { recipeCount: 1, } ); // creates the right model t.true(collection instanceof RecipeCollection); // correct attritubes t.true(typeof collection.name === 'string'); t.true(collection.nodes[0].recipe instanceof Types.ObjectId); // creates correct amount of children recipes t.is(collection.nodes.length, 1); }); test.only('recipes created by a collection have the same ownerId', async t => { const collection = await Factory.create( 'collection', {}, { recipeCount: 3, } ); t.plan(3); await collection.populate('nodes.recipe').execPopulate(); collection.nodes.forEach(node => { t.is(node.recipe.ownerId.toHexString(), collection.ownerId.toHexString()); }); });