From e44a35bf45709bb33453e01dcff9617870069075 Mon Sep 17 00:00:00 2001 From: Matthew Costabile Date: Sat, 18 Nov 2023 21:24:22 -0600 Subject: [PATCH] test: additional handler parse tests --- src/core/handlers/GraphQLHandler.test.ts | 66 ++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/src/core/handlers/GraphQLHandler.test.ts b/src/core/handlers/GraphQLHandler.test.ts index 99b11b74b..2c348b5f3 100644 --- a/src/core/handlers/GraphQLHandler.test.ts +++ b/src/core/handlers/GraphQLHandler.test.ts @@ -26,9 +26,9 @@ const resolver: ResponseResolver> = ({ function createGetGraphQLRequest( body: GraphQLRequestBody, - 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) @@ -36,9 +36,9 @@ function createGetGraphQLRequest( function createPostGraphQLRequest( body: GraphQLRequestBody, - 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)), @@ -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', () => {