Created
September 5, 2020 18:11
-
-
Save JonRoosevelt/47e84700dc421363a7da6af332386eaa to your computer and use it in GitHub Desktop.
Register test for @benawad part8 video https://github.com/benawad/graphql-ts-server-boilerplate/tree/8_handle_errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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