import test from 'ava'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { Recipe } from '../recipe-model'; import { User } from '../../user'; import { RecipeCollection } from '../../collection'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); test.serial('can get the owner of a recipe', async t => { const user = await Factory.create('user'); const recipe = await Factory.create('recipe', { ownerId: user._id, }); const owner = await recipe.getOwner(); t.is(owner.id, user.id); }); test.serial( 'recipe is removed from all collections when it is removed', async t => { const user = await Factory.create('user'); const collection = await Factory.create( 'collection', {}, { user, recipeCount: 5 } ); await collection.populate('nodes.recipe').execPopulate(); const recipe = collection.nodes[0].recipe; // delete recipe await recipe.remove(); const updatedCollection = await RecipeCollection.findById( collection.id ).exec(); t.is(updatedCollection.nodes.length, 4); t.is( -1, updatedCollection.nodes.findIndex(rcp => rcp.recipe.equals(recipe.id)) ); } ); test.only('recipe is removed from all collections when it is deleted via findOneAndDelete', async t => { const user = await Factory.create('user'); const collection = await Factory.create( 'collection', {}, { user, recipeCount: 5 } ); await collection.populate('nodes.recipe').execPopulate(); const recipe = collection.nodes[0].recipe; // delete recipe await Recipe.findOneAndDelete({ _id: recipe.id, }).exec(); const updatedCollection = await RecipeCollection.findById( collection.id ).exec(); t.is(updatedCollection.nodes.length, 4); t.is( -1, updatedCollection.nodes.findIndex(rcp => rcp.recipe.equals(recipe.id)) ); });