Skip to content

Commit

Permalink
Refactoring for making phoenix stable (#17)
Browse files Browse the repository at this point in the history
* removes the society schema for now

* --wip-- [skip ci]

* refactors the resolvers

* refactors test for washer

* refactors package.json for build issues

* removes unneeded packages from package.json
  • Loading branch information
utkarsh867 authored Oct 11, 2019
1 parent 3ca7ae0 commit b04eefb
Show file tree
Hide file tree
Showing 14 changed files with 1,177 additions and 1,594 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trailingComma: "es5"
tabWidth: 4
semi: true
singleQuote: true
singleQuote: false
92 changes: 46 additions & 46 deletions __tests__/users.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
import * as env from 'dotenv';
env.config();
import { prisma } from '../src/generated/prisma-client';
import typeDefs from '../src/schema';
import resolvers from '../src/resolvers';

const { ApolloServer, gql } = require('apollo-server');
const { createTestClient } = require('apollo-server-testing');

const prepareServer = async () => {
return new ApolloServer({
typeDefs,
resolvers,
context: {
prisma,
},
} as any);
};
import { prisma } from '../src/generated/prisma-client';
import testServer from '../src/server';
import {createTestClient} from "apollo-server-testing"

const createUser = async (user) => {
const createUser = async user => {
await prisma.createUser(user);
};

const deleteUsers = async () => {
return prisma.deleteManyUsers({})
};

const findUser = async (username) => {
return prisma.user({username});
return prisma.deleteManyUsers({});
};

const testUserInfo = {
Expand All @@ -39,53 +23,69 @@ const testUserInfo = {
room_no: '111A',
};

test('can query user', async (done) => {
const server = await prepareServer();
const client = createTestClient(server);
await deleteUsers();
await createUser(testUserInfo);
const userQuery = `{
describe('User query and mutations', () => {
test('can query user', async done => {
const client = createTestClient(testServer);
await deleteUsers();
await createUser(testUserInfo);
const userQuery = `{
user (username: "${testUserInfo.username}") {
username,
first_name,
last_name,
room_no
}}`;
const result = await client.query({ query: userQuery });
expect(result.data).toEqual({
user: {
username: testUserInfo.username,
first_name: testUserInfo.first_name,
last_name: testUserInfo.last_name,
room_no: testUserInfo.room_no,
},
const result = await client.query({ query: userQuery });
expect(result.data).toEqual({
user: {
username: testUserInfo.username,
first_name: testUserInfo.first_name,
last_name: testUserInfo.last_name,
room_no: testUserInfo.room_no,
},
});
done();
});
done();
});

describe('User query and mutations', () => {
test('can add user', async (done) => {
test('can add user', async done => {
await deleteUsers();
const server = await prepareServer();
const client = createTestClient(server);
const client = createTestClient(testServer);
const userMutation = `mutation addUser
{
createUser(newUser: {
createUser(
username: "${testUserInfo.username}",
email: "${testUserInfo.email}",
image_url:"${testUserInfo.image_url}",
phone: "${testUserInfo.phone}",
first_name:"${testUserInfo.first_name}",
last_name:"${testUserInfo.last_name}",
room_no:"${testUserInfo.room_no}"
}){
){
username
}
}`;

await client.mutate({ mutation: userMutation });
await findUser(testUserInfo.username);
const userQueryResult = await prisma.user({
username: testUserInfo.username,
});
expect(userQueryResult).toMatchObject(testUserInfo);
await deleteUsers();
done();
});
test('can delete user', async done => {
await deleteUsers();
const client = createTestClient(testServer);
await createUser(testUserInfo);
const userDeleteMutation = `
mutation delete{
deleteUser(username: "${testUserInfo.username}"){
username
}
}`;
await client.mutate({ mutation: userDeleteMutation });
const userQueryResult = await prisma.user({
username: testUserInfo.username,
});
expect(userQueryResult).toEqual(null);
done();
});
});
42 changes: 14 additions & 28 deletions __tests__/washers.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
import * as env from 'dotenv';
env.config();
import typeDefs from '../src/schema';
import resolvers from '../src/resolvers';
import { prisma } from '../src/generated/prisma-client';

const { ApolloServer } = require('apollo-server');
const { createTestClient } = require('apollo-server-testing');
import { prisma } from "../src/generated/prisma-client";
import { createTestClient } from "apollo-server-testing";
import testServer from "../src/server";


const testApolloServer = new ApolloServer({
typeDefs,
resolvers,
context: {
prisma,
},
} as any);

const createTestWasher = async (washer) => {
const createTestWasher = async washer => {
await prisma.createWasher(washer);
};

const deleteTestWashers = async () => {
return prisma.deleteManyWashers({})
return prisma.deleteManyWashers({});
};

const testWasher = {
id: "1",
status:"IDLE",
status: "IDLE",
time_elapsed: "0",
time_remaining:"0"
time_remaining: "0",
};

describe('the graphql washers api', () => {
it('returns the status of the washing machines', async (done) => {

const client = createTestClient(testApolloServer);
describe("the graphql washers api", () => {
test("returns the status of the washing machines", async done => {
const client = createTestClient(testServer);
await deleteTestWashers();
await createTestWasher(testWasher);
const washerQuery = `{
Expand All @@ -45,15 +33,13 @@ describe('the graphql washers api', () => {
time_remaining
}
}`;
const queryResponse = await client.query({query: washerQuery});
const queryResponse = await client.query({ query: washerQuery });
expect(queryResponse.data).toEqual({
washer: testWasher
washer: testWasher,
});
await deleteTestWashers();
done()
done();
});

it('writes status of the washing machines', () => {

});
it("writes status of the washing machines", () => {});
});
Loading

0 comments on commit b04eefb

Please sign in to comment.