Skip to content

Commit

Permalink
Add tests for Graphql.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacroldan committed Dec 17, 2024
1 parent 0456648 commit 7f56b5d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/cli-kit/src/private/node/request-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RequestIDCollection {
}

/**
* Get all collected request IDs as a comma-separated string
* Get all collected request IDs
*/
getRequestIds(): string[] {
return this.requestIds
Expand Down
74 changes: 62 additions & 12 deletions packages/cli-kit/src/public/node/api/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import {graphqlRequest, graphqlRequestDoc} from './graphql.js'
import {retryAwareRequest} from '../../../private/node/api.js'
import * as api from '../../../private/node/api.js'
import * as debugRequest from '../../../private/node/api/graphql.js'
import {buildHeaders} from '../../../private/node/api/headers.js'
import {requestIdsCollection} from '../../../private/node/request-ids.js'
import * as metadata from '../metadata.js'
import {GraphQLClient} from 'graphql-request'
import {test, vi, describe, expect, beforeEach} from 'vitest'
import {Headers} from 'node-fetch'
import {TypedDocumentNode} from '@graphql-typed-document-node/core'

vi.mock('../../../private/node/api.js')
let mockedRequestId = 'request-id-123'

vi.mock('graphql-request', async () => {
const actual = await vi.importActual('graphql-request')
const client = vi.fn()
client.prototype.rawRequest = vi.fn()
client.prototype.rawRequest = () => {
return {
status: 200,
headers: new Headers({
'x-request-id': mockedRequestId,
}),
}
}

return {
...(actual as object),
Expand All @@ -20,20 +29,21 @@ vi.mock('graphql-request', async () => {
})
vi.spyOn(debugRequest, 'debugLogRequestInfo').mockResolvedValue(undefined)

const mockedAddress = 'mockedAddress'
const mockedAddress = 'http://localhost:3000'
const mockVariables = {some: 'variables'}
const mockToken = 'token'
const mockedAddedHeaders = {some: 'header'}

beforeEach(async () => {
vi.mocked(retryAwareRequest).mockResolvedValue({
status: 200,
headers: {} as Headers,
})
requestIdsCollection.clear()
})

describe('graphqlRequest', () => {
test('calls debugLogRequestInfo once', async () => {
// Given
const retryAwareSpy = vi.spyOn(api, 'retryAwareRequest')

// When
await graphqlRequest({
query: 'query',
api: 'mockApi',
Expand All @@ -42,6 +52,8 @@ describe('graphqlRequest', () => {
addedHeaders: mockedAddedHeaders,
variables: mockVariables,
})

// Then
expect(GraphQLClient).toHaveBeenCalledWith(mockedAddress, {
agent: expect.any(Object),
headers: {
Expand All @@ -54,12 +66,46 @@ describe('graphqlRequest', () => {
request: expect.any(Function),
url: mockedAddress,
}
expect(retryAwareRequest).toHaveBeenCalledWith(receivedObject, expect.any(Function), undefined)

expect(retryAwareSpy).toHaveBeenCalledWith(receivedObject, expect.any(Function), undefined)
})

test('Logs the request ids to metadata and requestIdCollection', async () => {
// Given
const metadataSpyOn = vi.spyOn(metadata, 'addPublicMetadata').mockImplementation(async () => {})

// When
await graphqlRequest({
query: 'query',
api: 'mockApi',
url: mockedAddress,
token: mockToken,
addedHeaders: mockedAddedHeaders,
variables: mockVariables,
})

mockedRequestId = 'request-id-456'

await graphqlRequest({
query: 'query',
api: 'mockApi',
url: mockedAddress,
token: mockToken,
addedHeaders: mockedAddedHeaders,
variables: mockVariables,
})

// Then
expect(requestIdsCollection.getRequestIds()).toEqual(['request-id-123', 'request-id-456'])
expect(metadataSpyOn).toHaveBeenCalledTimes(2)
expect(metadataSpyOn.mock.calls[0]![0]()).toEqual({cmd_all_last_graphql_request_id: 'request-id-123'})
expect(metadataSpyOn.mock.calls[1]![0]()).toEqual({cmd_all_last_graphql_request_id: 'request-id-456'})
})
})

describe('graphqlRequestDoc', () => {
test('converts document before querying', async () => {
// Given
const document = {
kind: 'Document',
definitions: [
Expand All @@ -80,6 +126,9 @@ describe('graphqlRequestDoc', () => {
],
} as unknown as TypedDocumentNode<unknown, unknown>

const retryAwareSpy = vi.spyOn(api, 'retryAwareRequest')

// When
await graphqlRequestDoc({
query: document,
api: 'mockApi',
Expand All @@ -89,7 +138,8 @@ describe('graphqlRequestDoc', () => {
variables: mockVariables,
})

expect(retryAwareRequest).toHaveBeenCalledWith(
// Then
expect(retryAwareSpy).toHaveBeenCalledWith(
{
request: expect.any(Function),
url: mockedAddress,
Expand All @@ -102,7 +152,7 @@ describe('graphqlRequestDoc', () => {
`query QueryName {
example
}`,
'mockedAddress',
'http://localhost:3000',
mockVariables,
expect.anything(),
)
Expand Down
8 changes: 3 additions & 5 deletions packages/cli-kit/src/public/node/api/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ async function logLastRequestIdFromResponse(response: GraphQLResponse<unknown>)
try {
const requestId = response.headers.get('x-request-id')
requestIdsCollection.addRequestId(requestId)
await addPublicMetadata(async () => {
return {
cmd_all_last_graphql_request_id: requestId ?? undefined,
}
})
await addPublicMetadata(() => ({
cmd_all_last_graphql_request_id: requestId ?? undefined,
}))
// eslint-disable-next-line no-catch-all/no-catch-all
} catch {
// no problem if unable to get request ID.
Expand Down

0 comments on commit 7f56b5d

Please sign in to comment.