Skip to content

Commit

Permalink
feat: support multiple graphql endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ypresto committed Jun 17, 2024
1 parent 4202f69 commit 309cfa6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/playwright-msw/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,37 @@ describe('utils', () => {
});
});

describe('getHandlerPath for GraphQL handler', () => {
it.each<{ path: Path; expected: Path }>`
path | expected
${'/api/users'} | ${'/api/users'}
${/^\/api\/.*/} | ${/^\/api\/.*/}
`(
'return "$expected" for graphql.link() with "$path"',
({ path, expected }) => {
expect(
getHandlerPath(
graphql
.link(path)
.query('Query', () => HttpResponse.json({ data: {} })),
{},
),
).toStrictEqual(expected);
},
);

it('return the default endpoint path for graphql handler without link() call', () => {
const defaultHandler = graphql.query('Query', () =>
HttpResponse.json({ data: {} }),
);
expect(
getHandlerPath(defaultHandler, {
graphqlUrl: '/defaultGraphqlEndpoint',
}),
).toStrictEqual('/defaultGraphqlEndpoint');
});
});

describe('convertMswPathToPlaywrightUrl', () => {
it.each<{ mswPath: string; playwrightUrl: string; expected: boolean }>`
mswPath | playwrightUrl | expected
Expand Down
4 changes: 4 additions & 0 deletions packages/playwright-msw/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const getHandlerPath = (
config: Config,
): Path => {
if (getHandlerType(handler) === 'graphql') {
const handlerInternal = handler as unknown as { endpoint: Path }; // private GraphQLHandler.endpoint
if (handlerInternal.endpoint !== '*') {
return handlerInternal.endpoint;
}
const { graphqlUrl } = config;
if (!graphqlUrl) {
throw new Error(
Expand Down

0 comments on commit 309cfa6

Please sign in to comment.