import test, { TestInterface } from 'ava'; import { Factory } from '../../database/factory'; import * as testUtils from '../../test/utils'; import { User } from './user-model'; import { Recipe } from '../recipe'; import { RecipeCollection } from '../collection'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); test.serial('can get recipes via populate', async t => { const user = await Factory.create('user'); const recipes = await Factory.createMany('recipe', 3, { ownerId: user._id, }); const fetchedUser = await User.findById(user.id).populate('recipes'); t.is(fetchedUser.recipes.length, 3); }); test.serial('can get recipes via getRecipes method', async t => { const user = await Factory.create('user', { firstName: 'paul' }); const recipes = await Factory.createMany('recipe', 3, { ownerId: user._id, }); const fetchedRecipes = await user.getRecipes(); t.is(fetchedRecipes.length, 3); }); test.serial('deleting a user deletes *all* their recipes', async t => { const user = await Factory.create('user', { firstName: 'paul' }); const recipes = await Factory.createMany('recipe', 3, { ownerId: user._id, }); let fetchedRecipes = await Recipe.find({}); t.is(fetchedRecipes.length, 3); await user.remove(); fetchedRecipes = await Recipe.find({ ownerId: user._id }); t.is(fetchedRecipes.length, 0); }); test.serial('deleting a user deletes only *their* recipes', async t => { const user = await Factory.create('user', { firstName: 'paul' }); await Factory.createMany('recipe', 2, { ownerId: user._id, }); await Factory.createMany('recipe', 2); let fetchedRecipes = await Recipe.find({}); t.is(fetchedRecipes.length, 4); await user.remove(); fetchedRecipes = await Recipe.find({}); t.is(fetchedRecipes.length, 2); }); test.serial('can retrieve all their Recipe Collections', async t => { const user = await Factory.create('user', { firstName: 'paul' }); await Factory.createMany('collection', 2, {}, { user }); const collections = await user.getRecipeCollections().exec(); t.is(collections.length, 2); }); test.serial('deleting a user deletes all their Recipe Collections', async t => { const user = await Factory.create('user', { firstName: 'paul' }); const collections = await Factory.createMany( 'collection', 3, { ownerId: user._id, } ); let collectionCount = await RecipeCollection.countDocuments(); t.is(collectionCount, 3); await user.remove(); collectionCount = await RecipeCollection.countDocuments(); t.is(collectionCount, 0); });