Skip to content

Commit

Permalink
fix tests inconsistencies (#1360)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Jun 10, 2022
1 parent 830f839 commit 5a02e72
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 70 deletions.
52 changes: 19 additions & 33 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 All @@ -18,9 +17,6 @@ import {
import { getContextFactory, getSchema } from '../src'
import type { Options } from '../src'

let schema: GraphQLSchema
let context: Record<string, any>

const apiOptions = {
platform: 'vtex',
account: 'storeframework',
Expand All @@ -35,6 +31,18 @@ const 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,17 +64,13 @@ jest.mock('../src/platforms/vtex/clients/fetch.ts', () => ({
mockedFetch(info, init),
}))

beforeAll(async () => {
schema = await getSchema(apiOptions)

const contextFactory = getContextFactory(apiOptions)

context = contextFactory({})
})
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())
beforeEach(() => {
mockedFetch.mockClear()
})

test('`validateCart` mutation should return `null` when a valid cart is passed', async () => {
const fetchAPICalls = [
Expand All @@ -79,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 @@ -111,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 @@ -143,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
58 changes: 21 additions & 37 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 @@ -30,9 +29,6 @@ import {
attributeSearchCategory1Fetch,
} from '../mocks/SearchQuery'

let schema: GraphQLSchema
let context: Record<string, any>

const apiOptions = {
platform: 'vtex',
account: 'storeframework',
Expand All @@ -47,6 +43,18 @@ const 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,17 +75,13 @@ jest.mock('../src/platforms/vtex/clients/fetch.ts', () => ({
fetchAPI: (info: RequestInfo, init?: RequestInit) => mockedFetch(info, init),
}))

beforeAll(async () => {
schema = await getSchema(apiOptions)

const contextFactory = getContextFactory(apiOptions)

context = contextFactory({})
})
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())
beforeEach(() => {
mockedFetch.mockClear()
})

test('`collection` query', async () => {
const fetchAPICalls = [
Expand All @@ -90,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 @@ -116,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 @@ -145,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 @@ -171,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 @@ -200,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

1 comment on commit 5a02e72

@vercel
Copy link

@vercel vercel bot commented on 5a02e72 Jun 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.