From 92f612a995c54f7423bd2b760fb1a324044693a6 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Tue, 20 Mar 2018 14:48:34 -0400 Subject: Starting testing --- backend/__tests__/item.test.js | 81 +++++++++++++++++++++++++++++++++++++++ backend/src/index.js | 4 +- backend/src/resolvers/Mutation.js | 7 ++-- backend/tests/item.test.js | 81 --------------------------------------- 4 files changed, 87 insertions(+), 86 deletions(-) create mode 100644 backend/__tests__/item.test.js delete mode 100644 backend/tests/item.test.js (limited to 'backend') diff --git a/backend/__tests__/item.test.js b/backend/__tests__/item.test.js new file mode 100644 index 0000000..0e47419 --- /dev/null +++ b/backend/__tests__/item.test.js @@ -0,0 +1,81 @@ +const { request } = require('graphql-request'); + +let id; + +test('Creating an item', async () => { + const query = ` + mutation createItem { + createItem(title: "wes", description:"Bos", price: 500) { + title + description + price + id + } + } + `; + + const { createItem } = await request('http://localhost:4000', query); + + expect(createItem.title).toEqual('wes'); + expect(createItem.description).toEqual('Bos'); + expect(createItem).toHaveProperty('id'); + expect(createItem.price).toEqual(500); + id = createItem.id; +}); + +test('Get an array of items', async () => { + const query = ` + query getAllItems { + items { + title + id + description + price + } + } + `; + + const { items } = await request('http://localhost:4000', query); + expect(items.length).toBeGreaterThan(0); +}); + +test('Query a specific item', async () => { + const query = ` + query findAnItem { + items(where: { + id: "${id}" + }) { + title + id + description + price + } + } + `; + + const { items } = await request('http://localhost:4000', query); + const item = items[0]; + expect(item.title).toEqual('wes'); + expect(item.description).toEqual('Bos'); + expect(item.price).toEqual(500); +}); + +// Test Delete that item +test('delete an item', async () => { + const query = ` + mutation remove { + deleteItem(id: "${id}") { + id + } + } + `; + const res = await request('http://localhost:4000', query); + expect(res.deleteItem.id).toEqual(id); +}); + +// mutation updateItem { +// updateItem(id: "cjd21afpr47m00172nigtlkw8", title: "WES from playground") { +// id +// title +// } +// } diff --git a/backend/src/index.js b/backend/src/index.js index 3630b74..522fcfe 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -30,7 +30,9 @@ const server = new GraphQLServer({ // next(); // }); -server.start(() => console.log('Server is running on http://localhost:4000')); +server.start(deets => { + console.log(`Server is running on http://localhost:${deets.port}`); +}); // overwrite console.log const chalk = require('chalk'); diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js index cd25599..6eeab7c 100644 --- a/backend/src/resolvers/Mutation.js +++ b/backend/src/resolvers/Mutation.js @@ -1,6 +1,5 @@ const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); -const { forwardTo } = require('prisma-binding'); const { getUserId, Context } = require('../utils'); const { randomBytes } = require('crypto'); const { promisify } = require('util'); @@ -137,7 +136,6 @@ const mutations = { if (!user) { throw new Error(`No user with the email ${args.email}`); } - console.log(user); // 2. Set a reset token, and a reset date const resetToken = (await promisify(randomBytes)(20)).toString('hex'); const resetTokenExpiry = Date.now() + 3600000; // 1 hour from now @@ -151,11 +149,12 @@ const mutations = { // 3. Send them their token via email const mailRes = await mail.sendMail({ from: 'wesbos@gmail.com', - to: 'wesbos@gmail.com', + to: user.email, subject: 'Your password reset token', + // TODO: don't hardcore localhost here html: `Here is your reset link: http://localhost:3000/reset?resetToken=${resetToken}`, }); - + console.log(mailRes); return res.updateUser; }, diff --git a/backend/tests/item.test.js b/backend/tests/item.test.js deleted file mode 100644 index 0e47419..0000000 --- a/backend/tests/item.test.js +++ /dev/null @@ -1,81 +0,0 @@ -const { request } = require('graphql-request'); - -let id; - -test('Creating an item', async () => { - const query = ` - mutation createItem { - createItem(title: "wes", description:"Bos", price: 500) { - title - description - price - id - } - } - `; - - const { createItem } = await request('http://localhost:4000', query); - - expect(createItem.title).toEqual('wes'); - expect(createItem.description).toEqual('Bos'); - expect(createItem).toHaveProperty('id'); - expect(createItem.price).toEqual(500); - id = createItem.id; -}); - -test('Get an array of items', async () => { - const query = ` - query getAllItems { - items { - title - id - description - price - } - } - `; - - const { items } = await request('http://localhost:4000', query); - expect(items.length).toBeGreaterThan(0); -}); - -test('Query a specific item', async () => { - const query = ` - query findAnItem { - items(where: { - id: "${id}" - }) { - title - id - description - price - } - } - `; - - const { items } = await request('http://localhost:4000', query); - const item = items[0]; - expect(item.title).toEqual('wes'); - expect(item.description).toEqual('Bos'); - expect(item.price).toEqual(500); -}); - -// Test Delete that item -test('delete an item', async () => { - const query = ` - mutation remove { - deleteItem(id: "${id}") { - id - } - } - `; - const res = await request('http://localhost:4000', query); - expect(res.deleteItem.id).toEqual(id); -}); - -// mutation updateItem { -// updateItem(id: "cjd21afpr47m00172nigtlkw8", title: "WES from playground") { -// id -// title -// } -// } -- cgit v1.3