Skip to content

Instantly share code, notes, and snippets.

@jbaxleyiii
Last active October 22, 2018 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaxleyiii/c9f0cdfdfce532622b2a37b844f3ddc1 to your computer and use it in GitHub Desktop.
Save jbaxleyiii/c9f0cdfdfce532622b2a37b844f3ddc1 to your computer and use it in GitHub Desktop.
const { createClient, gql } = require("apollo-server/test-utils");
const { server } = require("../server");
describe("admin user", () => {
let client;
beforeAll(async () => {
// second argument here is the request
client = await createClient(server, ({ req }) => ({
user: {
id: 1
}
));
});
it("returns the proper user", async () => {
const { data, http } = await client.query({
query: gql`
query GetUser {
me {
id
roles
}
}
`,
});
expect(data.me.id).toEqual(1);
expect(data.me.roles).toEqual(["admin"]);
});
it("can do multiple actions", async () => {
const { errors, http } = await client.mutate({
query: gql`
mutation SaveUser($id: ID) {
saveUser(id: $id) {
user {
id
}
}
}
`,
variables: { id: 1 },
});
expect(http.status).toEqual(500);
expect(errors[0].message).toEqual("This user (id: 1) already exists");
const { data } = await client.query({
query: gql`
query GetUser($id: ID) {
user(id: $id) {
firstName
}
}
`,
variables: { id: 1 },
});
expect(data.user.firstName).toEqual("Harambe");
});
});
describe("Public user", () => {
let client;
beforeAll(async () => {
client = await createClient(server);
});
describe("logging into the app", async () => {
const { data } = await client.mutate({
query: gql`
mutation Login($username: String!, $password: String!) {
login(username: $username, password: $password) {
token
}
}
`,
variables: { username: "harambe", password: "insideJ@b" },
});
expect(data.login.token).toExist();
const response = await client.query({
query: gql`
query GetUser {
me {
roles
}
}
`,
http: {
headers: {
authorization: data.login.token,
},
},
});
expect(response.data.me.roles).toEqual([]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment