import test from 'ava'; import { Error } from 'mongoose'; import { UserInputError } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { RecipeCollection } from '../collection-model'; import { Recipe } from '../../recipe'; import { User } from '../../../app/user'; test.before(testUtils.setupDB); test.afterEach.always(testUtils.cleanupDB); test.after.always(testUtils.tearDownDB); test.serial('can get recipes via populate', async t => { const collection = await Factory.create( 'collection', {}, { recipeCount: 1, } ); // populate on document await collection.populate('nodes.recipe').execPopulate(); t.true(collection.nodes[0].recipe instanceof Recipe); // populate on query const fetchedCollection = await RecipeCollection.findById( collection.id ).populate('nodes.recipe'); t.true(fetchedCollection.nodes[0].recipe instanceof Recipe); }); test.todo('removes recipe from all collections when deleted'); // Add Recipe test.serial('can add recipes to a collection', async t => { const collection = await Factory.create('collection'); const recipe = await Factory.create('recipe'); collection.addRecipe(recipe); await collection.save(); t.is(collection.nodes[0].recipe.toHexString(), recipe.id); }); test('throws errow when recipe is already in the collection', async t => { const user = await Factory.create('user'); const collection = await Factory.create('collection', { ownerId: user.id, }); const recipe = await Factory.create('recipe', { ownerId: user.id }); await collection.addRecipe(recipe); await t.throwsAsync(async () => { return collection.addRecipe(recipe); }, Error.ValidatorError); }); // Remove Recipe test.serial('can remove recipes from a collection', async t => { const collection = await Factory.create( 'collection', {}, { recipeCount: 1, } ); if (collection.nodes.length !== 1) { t.fail('didnt initialize collection correctly'); } const recipe = await Recipe.findById(collection.nodes[0].recipe); collection.removeRecipe(recipe.id); await collection.save(); t.is(collection.nodes.length, 0); }); test.serial( 'should throw an error when *removing* a Recipe that isnt present in the Collection', async t => { const collection = await Factory.create( 'collection', {}, { recipeCount: 5, } ); if (collection.nodes.length !== 5) { t.fail('didnt initialize collection correctly'); } const otherRecipe = await Factory.create('recipe'); await t.throwsAsync(async () => { return collection.removeRecipe(otherRecipe.id); }, Error.ValidatorError); } ); // Update Recipe test.serial('can update the order of Recipe within a Collection', async t => { const collection = await Factory.create( 'collection', {}, { recipeCount: 5, } ); if (collection.nodes.length !== 5) { t.fail('didnt initialize collection correctly'); } const recipeId = collection.nodes[0].recipe; collection.updateRecipe(recipeId, { order: 4 }); await collection.save(); t.is(collection.nodes[4].recipe.toHexString(), recipeId.toHexString()); }); test.serial( 'should throw an error when *updating* a Recipe that isnt present in the Collection', async t => { const collection = await Factory.create( 'collection', {}, { recipeCount: 5, } ); if (collection.nodes.length !== 5) { t.fail('didnt initialize collection correctly'); } const otherRecipe = await Factory.create('recipe'); await t.throwsAsync(async () => { return collection.updateRecipe(otherRecipe.id, { order: 0 }); }, Error.ValidatorError); } );