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 ---------------------- frontend/__tests__/Cart.test.js | 15 ++-- frontend/__tests__/__snapshots__/Cart.test.js.snap | 44 ++++++++++++ 6 files changed, 138 insertions(+), 94 deletions(-) create mode 100644 backend/__tests__/item.test.js delete mode 100644 backend/tests/item.test.js create mode 100644 frontend/__tests__/__snapshots__/Cart.test.js.snap 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 -// } -// } diff --git a/frontend/__tests__/Cart.test.js b/frontend/__tests__/Cart.test.js index 0936e97..1e7d7f5 100644 --- a/frontend/__tests__/Cart.test.js +++ b/frontend/__tests__/Cart.test.js @@ -2,6 +2,7 @@ import React from 'react'; import { shallow, mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; import { Cart } from '../components/Cart'; +import { UIProvider } from '../components/UIContext'; const cartItem = { item: { price: 5000 }, @@ -17,14 +18,12 @@ const currentUser = { describe('', () => { it('renders', () => { - const wrapper = shallow(); - }); + const wrapper = mount( + + + + ); - xit('opens', () => { - const wrapper = shallow(); - expect('wes').toEqual('figure out how to call test context'); + expect(toJSON(wrapper)).toMatchSnapshot(); }); - // it('renders', () => { - // shallow(); - // }); }); diff --git a/frontend/__tests__/__snapshots__/Cart.test.js.snap b/frontend/__tests__/__snapshots__/Cart.test.js.snap new file mode 100644 index 0000000..b2b6bc7 --- /dev/null +++ b/frontend/__tests__/__snapshots__/Cart.test.js.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders 1`] = ` +<[object Object] + value={ + Object { + "state": Object { + "isCartOpen": false, + }, + "toggle": [Function], + } + } +> + + +`; -- cgit v1.3