summaryrefslogtreecommitdiffstats
path: root/backend
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-03-20 21:52:36 -0400
committerWes Bos <wesbos@gmail.com>2018-03-20 21:52:36 -0400
commit93495ce146c4e81ecfab3a0bca99460e75a759da (patch)
tree12b2de283a017ff6cc53c1175371f9add84238d2 /backend
parent92f612a995c54f7423bd2b760fb1a324044693a6 (diff)
tests
Diffstat (limited to 'backend')
-rw-r--r--backend/__tests__/item.test.js94
-rw-r--r--backend/__tests__/user.test.js26
-rw-r--r--backend/package.json8
-rw-r--r--backend/src/createServer.js30
-rw-r--r--backend/src/index.js34
-rw-r--r--backend/src/resolvers/Mutation.js71
-rw-r--r--backend/src/resolvers/Query.js25
7 files changed, 131 insertions, 157 deletions
diff --git a/backend/__tests__/item.test.js b/backend/__tests__/item.test.js
index 0e47419..7214a7b 100644
--- a/backend/__tests__/item.test.js
+++ b/backend/__tests__/item.test.js
@@ -1,9 +1,25 @@
const { request } = require('graphql-request');
+const createServer = require('../src/createServer');
-let id;
+let server;
+const port = 2342;
+const endpoint = `http://localhost:${port}`;
-test('Creating an item', async () => {
- const query = `
+const wait = amount => new Promise(resolve => setTimeout(resolve, amount));
+
+beforeAll(async () => {
+ server = await createServer().start({ port });
+});
+
+afterAll(async () => {
+ server.close();
+});
+
+describe('Item C.R.U.D. Operations', () => {
+ let id;
+
+ it('Creating an item', async () => {
+ const query = `
mutation createItem {
createItem(title: "wes", description:"Bos", price: 500) {
title
@@ -14,17 +30,17 @@ test('Creating an item', async () => {
}
`;
- const { createItem } = await request('http://localhost:4000', query);
+ const { createItem } = await request(endpoint, query);
- expect(createItem.title).toEqual('wes');
- expect(createItem.description).toEqual('Bos');
- expect(createItem).toHaveProperty('id');
- expect(createItem.price).toEqual(500);
- id = createItem.id;
-});
+ 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 = `
+ it('Get an array of items', async () => {
+ const query = `
query getAllItems {
items {
title
@@ -35,12 +51,12 @@ test('Get an array of items', async () => {
}
`;
- const { items } = await request('http://localhost:4000', query);
- expect(items.length).toBeGreaterThan(0);
-});
+ const { items } = await request(endpoint, query);
+ expect(items.length).toBeGreaterThan(0);
+ });
-test('Query a specific item', async () => {
- const query = `
+ it('Query a specific item', async () => {
+ const query = `
query findAnItem {
items(where: {
id: "${id}"
@@ -53,29 +69,37 @@ test('Query a specific item', async () => {
}
`;
- 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);
-});
+ const { items } = await request(endpoint, 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 = `
+ it('updates an item', async () => {
+ const query = `
+ mutation updateItem {
+ updateItem(id: "${id}", title: "Updated Title") {
+ id
+ title
+ }
+ }
+ `;
+ const { updateItem } = await request(endpoint, query);
+ expect(updateItem.id).toBe(id);
+ expect(updateItem.title).toBe('Updated Title');
+ });
+
+ // Test Delete that item
+ it('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);
+ const res = await request(endpoint, query);
+ expect(res.deleteItem.id).toEqual(id);
+ });
});
-
-// mutation updateItem {
-// updateItem(id: "cjd21afpr47m00172nigtlkw8", title: "WES from playground") {
-// id
-// title
-// }
-// }
diff --git a/backend/__tests__/user.test.js b/backend/__tests__/user.test.js
new file mode 100644
index 0000000..bedca3f
--- /dev/null
+++ b/backend/__tests__/user.test.js
@@ -0,0 +1,26 @@
+const { request } = require('graphql-request');
+const createServer = require('../src/createServer');
+
+import { SIGNUP_MUTATION } from '../../frontend/queries/index';
+
+console.log(SIGNUP_MUTATION);
+
+let server;
+const port = 2342;
+const endpoint = `http://localhost:${port}`;
+
+beforeAll(async () => {
+ server = await createServer().start({ port });
+});
+
+afterAll(async () => {
+ server.close();
+});
+
+describe('User C.R.U.D. Operations', () => {
+ it('Creates a user', () => {
+ const query = `
+
+ `;
+ });
+});
diff --git a/backend/package.json b/backend/package.json
index 31f0618..645e930 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -17,6 +17,7 @@
"stripe": "^5.4.0"
},
"devDependencies": {
+ "babel-preset-env": "^1.6.1",
"chalk": "^2.3.0",
"dotenv": "5.0.0",
"graphql-cli": "2.14.0",
@@ -29,5 +30,10 @@
"license": "MIT",
"repository": {
"url": "wesbos.com"
+ },
+ "babel": {
+ "presets": [
+ "env"
+ ]
}
-} \ No newline at end of file
+}
diff --git a/backend/src/createServer.js b/backend/src/createServer.js
new file mode 100644
index 0000000..57518b2
--- /dev/null
+++ b/backend/src/createServer.js
@@ -0,0 +1,30 @@
+const { GraphQLServer } = require('graphql-yoga');
+const { Prisma } = require('prisma-binding');
+require('dotenv').config();
+
+const Mutation = require('./resolvers/Mutation');
+const Query = require('./resolvers/Query');
+const AuthPayload = require('./resolvers/AuthPayload');
+
+function createServer() {
+ return new GraphQLServer({
+ typeDefs: 'src/schema.graphql',
+ resolvers: {
+ // detail resolvers
+ Mutation,
+ Query,
+ AuthPayload,
+ },
+ context: req => ({
+ ...req,
+ db: new Prisma({
+ typeDefs: 'src/generated/prisma.graphql',
+ endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
+ secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
+ debug: false, // log all GraphQL queries & mutations
+ }),
+ }),
+ });
+}
+
+module.exports = createServer;
diff --git a/backend/src/index.js b/backend/src/index.js
index 522fcfe..a37e1f7 100644
--- a/backend/src/index.js
+++ b/backend/src/index.js
@@ -1,36 +1,8 @@
-const { GraphQLServer } = require('graphql-yoga');
-const { Prisma } = require('prisma-binding');
+const createServer = require('./createServer');
-const Mutation = require('./resolvers/Mutation');
-const Query = require('./resolvers/Query');
-const AuthPayload = require('./resolvers/AuthPayload');
+const server = createServer();
-const server = new GraphQLServer({
- typeDefs: 'src/schema.graphql',
- resolvers: {
- // detail resolvers
- Mutation,
- Query,
- AuthPayload,
- },
- context: req => ({
- ...req,
- db: new Prisma({
- typeDefs: 'src/generated/prisma.graphql',
- endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
- secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
- debug: false, // log all GraphQL queries & mutations
- }),
- }),
-});
-
-// // This is an example of custom express middlware
-// server.express.use((req, res, next) => {
-// // console.log("Hi I'm middlware");
-// next();
-// });
-
-server.start(deets => {
+server.start({ port: 4444 }, deets => {
console.log(`Server is running on http://localhost:${deets.port}`);
});
diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js
index 6eeab7c..3d9ebb8 100644
--- a/backend/src/resolvers/Mutation.js
+++ b/backend/src/resolvers/Mutation.js
@@ -66,68 +66,6 @@ const mutations = {
});
},
- // async createDraft(parent, { title, text }, ctx, info) {
- // // const userId = getUserId(ctx);
- // const userId = getUserId(ctx);
- // return ctx.db.mutation.createPost(
- // {
- // data: {
- // title,
- // text,
- // isPublished: false,
- // author: {
- // connect: { id: userId },
- // },
- // },
- // },
- // info
- // );
- // },
-
- // async publish(parent, { id }, ctx, info) {
- // const userId = getUserId(ctx);
- // const postExists = await ctx.db.exists.Post({
- // id,
- // author: { id: userId },
- // });
- // if (!postExists) {
- // throw new Error(`Post not found or you're not the author`);
- // }
-
- // return ctx.db.mutation.updatePost(
- // {
- // where: { id },
- // data: { isPublished: true },
- // },
- // info
- // );
- // },
-
- // async deletePost(parent, { id }, ctx, info) {
- // const userId = getUserId(ctx);
- // const postExists = await ctx.db.exists.Post({
- // id,
- // author: { id: userId },
- // });
- // if (!postExists) {
- // throw new Error(`Post not found or you're not the author`);
- // }
-
- // return ctx.db.mutation.deletePost({ where: { id } });
- // },
-
- // // Wes Added this brand new one!
- // async updatePost(parent, args, ctx, info) {
- // const updatedPost = await ctx.db.mutation.updatePost({
- // where: { id: args.id },
- // data: {
- // title: args.title,
- // text: args.text,
- // },
- // });
- // return updatedPost;
- // },
-
// Send password request
async requestReset(parent, args, ctx, info) {
// 1. find if there is a user with that email
@@ -307,18 +245,15 @@ const mutations = {
// 6. Clean up, clear the users cart adn send back { user, order }
// Delete the users current cart items
const cartItemIds = user.cart.map(cartItem => cartItem.id);
- const deletedCartItems = await ctx.db.mutation.deleteManyCartItems({
+ await ctx.db.mutation.deleteManyCartItems({
where: {
id_in: cartItemIds,
},
});
- console.l('--------ORDER-------------');
- console.log(order);
- console.l('--------ORDER-------------');
+ // 5. Send the order back to the client
return order;
- // 4. Send an email with their order
- // 5. Send the order back
+ // 4. TODO: Send an email with their order
},
async updateUser(parent, args, ctx, info) {
const userId = getUserId(ctx);
diff --git a/backend/src/resolvers/Query.js b/backend/src/resolvers/Query.js
index 262517a..2aa4f5c 100644
--- a/backend/src/resolvers/Query.js
+++ b/backend/src/resolvers/Query.js
@@ -8,31 +8,11 @@ const Query = {
},
itemsConnection: forwardTo('db'),
- order: forwardTo('db'),
-
- // feed(parent, args, ctx, info) {
- // return ctx.db.query.posts({}, info);
- // },
-
- // drafts(parent, args, ctx, info) {
- // // const id = getUserId(ctx);
-
- // const where = {
- // isPublished: false,
- // // author: {
- // // id,
- // // },
- // };
- // return ctx.db.query.posts({ where }, info);
- // },
-
- // post(parent, { id }, ctx, info) {
- // return ctx.db.query.post({ where: { id } }, info);
- // },
+ // TODO: Make sure they own this order before looking it up
+ order: forwardTo('db'),
me(parent, args, ctx, info) {
- console.l('me!');
const Authorization = ctx.request.get('Authorization');
if (!Authorization || Authorization === 'null') {
console.log('Authorization is null');
@@ -41,6 +21,7 @@ const Query = {
const id = getUserId(ctx);
return ctx.db.query.user({ where: { id } }, info);
},
+
async orders(parent, args, ctx, info) {
const userId = getUserId(ctx);
return ctx.db.query.orders(