2020-09-02 16:09:11 -07:00
|
|
|
const gql = require("graphql-tag");
|
2020-09-02 23:00:16 -07:00
|
|
|
const { query, getDbCalls, logInAsTestUser } = require("./setup.js");
|
2020-09-02 16:09:11 -07:00
|
|
|
|
|
|
|
describe("User", () => {
|
|
|
|
it("looks up a user", async () => {
|
|
|
|
const res = await query({
|
|
|
|
query: gql`
|
|
|
|
query {
|
|
|
|
user(id: "6") {
|
|
|
|
id
|
|
|
|
username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res).toHaveNoErrors();
|
|
|
|
expect(res.data).toMatchInlineSnapshot(`
|
|
|
|
Object {
|
|
|
|
"user": Object {
|
|
|
|
"id": "6",
|
|
|
|
"username": "matchu",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
expect(getDbCalls()).toMatchInlineSnapshot(`
|
|
|
|
Array [
|
|
|
|
Array [
|
|
|
|
"SELECT * FROM users WHERE id IN (?)",
|
|
|
|
Array [
|
|
|
|
"6",
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns null when user not found", async () => {
|
|
|
|
const res = await query({
|
|
|
|
query: gql`
|
|
|
|
query {
|
|
|
|
user(id: "<invalid-user-id>") {
|
|
|
|
id
|
|
|
|
username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res).toHaveNoErrors();
|
2020-09-02 23:00:16 -07:00
|
|
|
expect(res.data).toEqual({ user: null });
|
2020-09-02 16:09:11 -07:00
|
|
|
expect(getDbCalls()).toMatchInlineSnapshot(`
|
|
|
|
Array [
|
|
|
|
Array [
|
|
|
|
"SELECT * FROM users WHERE id IN (?)",
|
|
|
|
Array [
|
|
|
|
"<invalid-user-id>",
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
`);
|
|
|
|
});
|
2020-09-02 23:00:16 -07:00
|
|
|
|
|
|
|
it("gets current user, if logged in", async () => {
|
|
|
|
await logInAsTestUser();
|
|
|
|
|
|
|
|
const res = await query({
|
|
|
|
query: gql`
|
|
|
|
query {
|
|
|
|
currentUser {
|
|
|
|
id
|
|
|
|
username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res).toHaveNoErrors();
|
|
|
|
expect(res.data).toMatchInlineSnapshot(`
|
|
|
|
Object {
|
|
|
|
"currentUser": Object {
|
|
|
|
"id": "44743",
|
|
|
|
"username": "dti-test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
expect(getDbCalls()).toMatchInlineSnapshot(`
|
|
|
|
Array [
|
|
|
|
Array [
|
|
|
|
"SELECT * FROM users WHERE id IN (?)",
|
|
|
|
Array [
|
|
|
|
"44743",
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("gets no user, if logged out", async () => {
|
|
|
|
const res = await query({
|
|
|
|
query: gql`
|
|
|
|
query {
|
|
|
|
currentUser {
|
|
|
|
id
|
|
|
|
username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res).toHaveNoErrors();
|
|
|
|
expect(res.data).toEqual({ currentUser: null });
|
|
|
|
expect(getDbCalls()).toMatchInlineSnapshot(`Array []`);
|
|
|
|
});
|
2020-09-02 16:09:11 -07:00
|
|
|
});
|