import ava, { TestInterface } from 'ava'; import { Mongoose } from 'mongoose'; import * as testUtils from '../../../test/utils'; import { User } from '../user-model'; import { createContext } from '../../../test/utils/create-mock-context'; import { gqlCall } from '../../../test/utils/gqlCall'; import { UserInputError, ForbiddenError, AuthenticationError, } from 'apollo-server-micro'; // // 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 createUserMutation = ` mutation Create($input: RegisterUserInput!) { registerUser(input: $input) { id firstName } } `; // // Tests // test.only('Should save new user to DB', async t => { const context = createContext(); let userCount = await User.countDocuments(); t.is(userCount, 0); const response = await gqlCall({ source: createUserMutation, contextValue: context, variableValues: { input: { firstName: 'New', lastName: 'User', password: 'password', email: 'user@email.com', }, }, }); userCount = await User.countDocuments(); t.falsy(response.errors); t.is(userCount, 1); }); test.serial('Should return the created user', async t => { const context = createContext(); const response = await gqlCall({ source: createUserMutation, contextValue: context, variableValues: { input: { firstName: 'New', lastName: 'User', password: 'password', }, }, }); t.is(response.data.createRecipe.firstName, 'New'); });