Skip to content

Instantly share code, notes, and snippets.

@JonRoosevelt
Created September 5, 2020 18:11
Show Gist options
  • Select an option

  • Save JonRoosevelt/47e84700dc421363a7da6af332386eaa to your computer and use it in GitHub Desktop.

Select an option

Save JonRoosevelt/47e84700dc421363a7da6af332386eaa to your computer and use it in GitHub Desktop.
import { request } from "graphql-request";
import { User } from "../../entity/User";
import { startServer } from "../../startServer";
let getHost = () => "";
beforeAll(async () => {
const app = await startServer();
const { port } = app.address();
getHost = () => `http://127.0.0.1:${port}`;
});
const email = "[email protected]";
const password = "akjksdjksd";
const mutation = `
mutation {
register(email: "${email}", password: "${password}") {
path,
message
}
}`;
describe("Register user", async () => {
it("Should return a response equal to null", async () => {
const response = await request(getHost(), mutation);
expect(response).toEqual({ register: null });
});
it("Should return 1 user", async () => {
const users = await User.find({ where: { email } });
expect(users).toHaveLength(1);
});
it("Should return user with expected email and password different from the created (because it is hashed)", async () => {
const users = await User.find({ where: { email } });
const user = await users[0];
expect(user.email).toEqual(email);
expect(user.password).not.toEqual(password);
});
it("Should return error if an email is already taken", async () => {
const response = await request(getHost(), mutation);
expect(response.register).toHaveLength(1);
expect(response.register[0].path).toEqual("email");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment