Last active
December 25, 2018 17:23
Revisions
-
fernandes revised this gist
Dec 25, 2018 . 1 changed file with 107 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,107 @@ // Use a (simple) dummy resolver, so we can instantiate a new class for each test // don't hit the database and run (faster) tests in parallel import test from "ava" const { createTestClient } = require('apollo-server-testing') const { gql } = require('apollo-server') const server = require('./server') const { ApolloServer } = require('apollo-server') const GET_USERS = gql` query getUsers { users { name id } } ` const CREATE_USER = gql` mutation createUser($data : UserCreateInput!) { createUser(data: $data) { name } } ` class MockedResolver { constructor() { // this is your database initial state this.db = { users: [{ id: 1, name: "Fixture User" }] } } users() { return this.db.users } createUser(data) { this.db.users = [...this.db.users, {...data, id: this.randomId()}] return data } randomId() { return Date.now() } } test.beforeEach(async t => { const mockedResolver = new MockedResolver const mockedServer = new ApolloServer({ ...server, context: (req) => ({ ...req, db: mockedResolver }) }) const { query, mutate } = createTestClient(mockedServer) t.context.query = query t.context.mutate = mutate }); test("[mock] fixtures are loaded", async t => { const res = await t.context.query({ query: GET_USERS }); const { users } = res.data t.is(users.length, 1) // check the names const userNames = users.map((x) => x.name).sort() t.deepEqual(userNames, ["Fixture User"]) }); test("[mock] should list only user 1", async t => { await t.context.mutate({ mutation: CREATE_USER, variables: { data: { name: "Dummy 1" } } }) const res = await t.context.query({ query: GET_USERS }); const { users } = res.data t.is(users.length, 2) // check the names const userNames = users.map((x) => x.name).sort() t.deepEqual(userNames, ["Dummy 1", "Fixture User"]) }); test("[mock] should list only user 2", async t => { await t.context.mutate({ mutation: CREATE_USER, variables: { data: { name: "Dummy 2" } } }) const res = await t.context.query({ query: GET_USERS }) const { users } = res.data t.is(users.length, 2) // check the names const userNames = users.map((x) => x.name).sort() t.deepEqual(userNames, ["Dummy 2", "Fixture User"]) }); -
fernandes revised this gist
Dec 25, 2018 . No changes.There are no files selected for viewing
-
fernandes created this gist
Dec 25, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ const { ApolloServer, gql } = require('apollo-server') const { Prisma } = require('../generated/prisma-client') const { importSchema } = require('graphql-import') const resolvers = require('./resolvers') const importedTypeDefs = importSchema(__dirname + '/../generated/prisma.graphql'); const typeDefs = gql`${importedTypeDefs}` const endpoint = 'http://localhost:4466' const server = new ApolloServer({ typeDefs, resolvers, context: req => ({ ...req, db: new Prisma({ endpoint: endpoint, }), }), resolverValidationOptions :{ requireResolversForResolveType: false }, }) module.exports = server 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ import test from "ava" const { createTestClient } = require('apollo-server-testing') const { gql } = require('apollo-server') const server = require('./server') const { query, mutate } = createTestClient(server) const GET_USERS = gql` query getUsers { users { name id } } ` const CREATE_USER = gql` mutation createUser($data : UserCreateInput!) { createUser(data: $data) { name } } ` const loadFixtures = async () => { await mutate({ mutation: CREATE_USER, variables: { data: { name: "Fixture User" } } }) } test.beforeEach(async t => { await fetch("http://localhost:4466/private", { body: JSON.stringify({ query: "mutation { resetData }" }), headers: { "content-type": "application/json" }, method: "post", }); await loadFixtures() }); test.serial("fixtures are loaded", async t => { const res = await query({ query: GET_USERS }); t.is(res.data.users.length, 1) // check the names const userNames = res.data.users.map((x) => x.name).sort() t.deepEqual(userNames, ["Fixture User"]) }); test.serial("should list only user 1", async t => { await mutate({ mutation: CREATE_USER, variables: { data: { name: "Dummy 1" } } }) const res = await query({ query: GET_USERS }); t.is(res.data.users.length, 2) // check the names const userNames = res.data.users.map((x) => x.name).sort() t.deepEqual(userNames, ["Dummy 1", "Fixture User"]) }); test.serial("should list only user 2", async t => { await mutate({ mutation: CREATE_USER, variables: { data: { name: "Dummy 2" } } }) const res = await query({ query: GET_USERS }); t.is(res.data.users.length, 2) // check the names const userNames = res.data.users.map((x) => x.name).sort() t.deepEqual(userNames, ["Dummy 2", "Fixture User"]) });