Skip to content

Commit

Permalink
Adds GraphQL variables integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Mar 9, 2020
1 parent 4409ebe commit 4436075
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/graphql-api/variables.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { composeMocks, graphql } from 'msw'

const { start } = composeMocks(
graphql.query({ operation: 'GetGithubUser' }, (req, res, ctx) => {
const { username } = req.variables

return res(
ctx.data({
user: {
firstName: 'John',
username,
},
}),
)
}),

graphql.mutation({ operation: 'DeletePost' }, (req, res, ctx) => {
const { postId } = req.variables

return res(
ctx.data({
deletePost: {
postId,
},
}),
)
}),

graphql.query({ operation: 'GetActiveUser' }, (req, res, ctx) => {
// Intentionally unused variable
const { foo } = req.variables

return res(
ctx.data({
user: {
id: 1,
},
}),
)
}),
)

start()
120 changes: 120 additions & 0 deletions test/graphql-api/variables.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import * as path from 'path'
import { Response } from 'puppeteer'
import { TestAPI, runBrowserWith } from '../support/runBrowserWith'
import { executeOperation } from './utils/executeOperation'

describe('GraphQL: Variables', () => {
let api: TestAPI

beforeAll(async () => {
api = await runBrowserWith(path.resolve(__dirname, 'variables.mocks.ts'))
})

afterAll(() => {
return api.cleanup()
})

describe('given a query with variables', () => {
let res: Response
let body: Record<string, any>

beforeAll(async () => {
res = await executeOperation(api.page, {
query: `
query GetGithubUser($username: String!) {
user(login: $username) {
firstName
username
}
}
`,
variables: {
username: 'octocat',
},
})
body = await res.json()
})

it('should return 200 status code', () => {
expect(res.status()).toBe(200)
})

it('should mock the response data', () => {
expect(body).toEqual({
data: {
user: {
firstName: 'John',
username: 'octocat',
},
},
})
})
})

describe('given a mutation with variables', () => {
let res: Response
let body: Record<string, any>

beforeAll(async () => {
res = await executeOperation(api.page, {
query: `
mutation DeletePost($postId: String!) {
deletePost(id: $postId) {
postId
}
}
`,
variables: {
postId: 'abc-123',
},
})
body = await res.json()
})

it('should return 200 status code', () => {
expect(res.status()).toBe(200)
})

it('should mock the response data', () => {
expect(body).toEqual({
data: {
deletePost: {
postId: 'abc-123',
},
},
})
})
})

describe('given GraphQL operation without variables', () => {
let res: Response
let body: Record<string, any>

beforeAll(async () => {
res = await executeOperation(api.page, {
query: `
query GetActiveUser {
user {
id
}
}
`,
})
body = await res.json()
})

it('should return 200 status code', () => {
expect(res.status()).toBe(200)
})

it('should mock the response data', () => {
expect(body).toEqual({
data: {
user: {
id: 1,
},
},
})
})
})
})

0 comments on commit 4436075

Please sign in to comment.