Skip to content

Commit

Permalink
no globals
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Jun 2, 2022
1 parent b7e65f6 commit 7fac806
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 62 deletions.
45 changes: 16 additions & 29 deletions packages/api/test/mutations.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { execute, parse } from 'graphql'
import type { GraphQLSchema } from 'graphql'

import {
checkoutOrderFormCustomDataInvalidFetch,
Expand Down Expand Up @@ -30,11 +29,20 @@ const apiOptions = {
},
} as Options

let schema: GraphQLSchema
let context: Record<string, any>
const contextFactory = getContextFactory(apiOptions)
const mockedFetch = jest.fn()

const createRunner = () => {
const schemaPromise = getSchema(apiOptions)
const contextFactory = getContextFactory(apiOptions)

return async (query: string, variables?: any) => {
const schema = await schemaPromise
const context = contextFactory({})

return execute(schema, parse(query), null, context, variables)
}
}

function pickFetchAPICallResult(
info: RequestInfo,
_: RequestInit | undefined,
Expand All @@ -56,15 +64,12 @@ jest.mock('../src/platforms/vtex/clients/fetch.ts', () => ({
mockedFetch(info, init),
}))

beforeAll(async () => {
schema = await getSchema(apiOptions)
})
const run = createRunner()

// Always clear the mocked fetch before each test so we can count and validate
// the calls performed by each query independently.
beforeEach(() => {
mockedFetch.mockClear()
context = contextFactory({})
})

test('`validateCart` mutation should return `null` when a valid cart is passed', async () => {
Expand All @@ -78,13 +83,7 @@ test('`validateCart` mutation should return `null` when a valid cart is passed',
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(
schema,
parse(ValidateCartMutation),
null,
context,
{ cart: ValidCart }
)
const response = await run(ValidateCartMutation, { cart: ValidCart })

expect(mockedFetch).toHaveBeenCalledTimes(3)

Expand All @@ -110,13 +109,7 @@ test('`validateCart` mutation should return the full order when an invalid cart
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(
schema,
parse(ValidateCartMutation),
null,
context,
{ cart: InvalidCart }
)
const response = await run(ValidateCartMutation, { cart: InvalidCart })

expect(mockedFetch).toHaveBeenCalledTimes(4)

Expand All @@ -142,13 +135,7 @@ test('`validateCart` mutation should return new cart when etag is stale', async
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(
schema,
parse(ValidateCartMutation),
null,
context,
{ cart: InvalidCart }
)
const response = await run(ValidateCartMutation, { cart: InvalidCart })

expect(mockedFetch).toHaveBeenCalledTimes(3)

Expand Down
51 changes: 18 additions & 33 deletions packages/api/test/queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { GraphQLSchema } from 'graphql'
import { execute, parse } from 'graphql'

import type { Options } from '../src'
Expand Down Expand Up @@ -42,11 +41,20 @@ const apiOptions = {
},
} as Options

let schema: GraphQLSchema
let context: Record<string, any>
const contextFactory = getContextFactory(apiOptions)
const mockedFetch = jest.fn()

const createRunner = () => {
const schemaPromise = getSchema(apiOptions)
const contextFactory = getContextFactory(apiOptions)

return async (query: string, variables?: any) => {
const schema = await schemaPromise
const context = contextFactory({})

return execute(schema, parse(query), null, context, variables)
}
}

function pickFetchAPICallResult(
info: RequestInfo,
_: RequestInit | undefined,
Expand All @@ -67,15 +75,12 @@ jest.mock('../src/platforms/vtex/clients/fetch.ts', () => ({
fetchAPI: (info: RequestInfo, init?: RequestInit) => mockedFetch(info, init),
}))

beforeAll(async () => {
schema = await getSchema(apiOptions)
})
const run = createRunner()

// Always clear the mocked fetch before each test so we can count and validate
// the calls performed by each query independently.
beforeEach(() => {
mockedFetch.mockClear()
context = contextFactory({})
})

test('`collection` query', async () => {
Expand All @@ -89,12 +94,7 @@ test('`collection` query', async () => {
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(
schema,
parse(CollectionDesksQuery),
null,
context
)
const response = await run(CollectionDesksQuery)

expect(mockedFetch).toHaveBeenCalledTimes(3)

Expand All @@ -115,7 +115,7 @@ test('`product` query', async () => {
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(schema, parse(ProductByIdQuery), null, context)
const response = await run(ProductByIdQuery)

expect(mockedFetch).toHaveBeenCalledTimes(1)

Expand Down Expand Up @@ -144,12 +144,7 @@ test('`allCollections` query', async () => {
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(
schema,
parse(AllCollectionsQueryFirst5),
null,
context
)
const response = await run(AllCollectionsQueryFirst5)

expect(mockedFetch).toHaveBeenCalledTimes(7)

Expand All @@ -170,12 +165,7 @@ test('`allProducts` query', async () => {
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(
schema,
parse(AllProductsQueryFirst5),
null,
context
)
const response = await run(AllProductsQueryFirst5)

expect(mockedFetch).toHaveBeenCalledTimes(1)

Expand All @@ -199,12 +189,7 @@ test('`search` query', async () => {
pickFetchAPICallResult(info, init, fetchAPICalls)
)

const response = await execute(
schema,
parse(SearchQueryFirst5Products),
null,
context
)
const response = await run(SearchQueryFirst5Products)

expect(mockedFetch).toHaveBeenCalledTimes(2)

Expand Down

0 comments on commit 7fac806

Please sign in to comment.