Skip to content

Commit

Permalink
test: additional handler parse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcosta7 committed Nov 19, 2023
1 parent 7c06d30 commit e44a35b
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions src/core/handlers/GraphQLHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ const resolver: ResponseResolver<GraphQLResolverExtras<{ userId: string }>> = ({

function createGetGraphQLRequest(
body: GraphQLRequestBody<any>,
hostname = 'https://example.com',
graphqlEndpoint = 'https://example.com',
) {
const requestUrl = new URL(hostname)
const requestUrl = new URL(graphqlEndpoint)
requestUrl.searchParams.set('query', body?.query)
requestUrl.searchParams.set('variables', JSON.stringify(body?.variables))
return new Request(requestUrl)
}

function createPostGraphQLRequest(
body: GraphQLRequestBody<any>,
hostname = 'https://example.com',
graphqlEndpoint = 'https://example.com',
) {
return new Request(new URL(hostname), {
return new Request(new URL(graphqlEndpoint), {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: encodeBuffer(JSON.stringify(body)),
Expand Down Expand Up @@ -370,6 +370,64 @@ describe('parse', () => {
})
})
})

describe('with endpoint configuration', () => {
test('parses a request when the graphql.link endpoint matches', async () => {
const handler = new GraphQLHandler(
OperationTypeNode.QUERY,
'GetUser',
'https://mswjs.com/graphql',
resolver,
)
const request = createPostGraphQLRequest(
{
query: GET_USER,
variables: {
userId: 'abc-123',
},
},
'https://mswjs.com/graphql',
)

await expect(handler.parse({ request })).resolves.toEqual({
match: {
matches: true,
params: {},
},
operationType: 'query',
operationName: 'GetUser',
query: GET_USER,
variables: {
userId: 'abc-123',
},
})
})

test('parses a request when the graphql.link endpoint does not match', async () => {
const handler = new GraphQLHandler(
OperationTypeNode.QUERY,
'GetUser',
'https://mswjs.com/graphql',
resolver,
)
const request = createPostGraphQLRequest(
{
query: GET_USER,
variables: {
userId: 'abc-123',
},
},
'https://mswjs.com/some/other/endpoint',
)

await expect(handler.parse({ request })).resolves.toEqual({
match: {
matches: false,
params: {},
},
})
})
})
})

describe('predicate', () => {
Expand Down

0 comments on commit e44a35b

Please sign in to comment.