Skip to content

Commit

Permalink
fix missingHandlerPolicy not passed to a MockLink when creating a cli…
Browse files Browse the repository at this point in the history
…ent (#60)
  • Loading branch information
mykolavlasov authored Apr 11, 2024
1 parent 64a717c commit 1b68292
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/mockClient.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApolloQueryResult, gql, InMemoryCache } from '@apollo/client/core';
import { print } from 'graphql';

// Currently do not test against all valid peer dependency versions of apollo
// Would be nice to have, but can't find an elegant way of doing it.
Expand Down Expand Up @@ -59,6 +60,18 @@ describe('MockClient integration tests', () => {
expect(() => mockClient.query({ query: queryTwo }))
.toThrowError('Request handler not defined for query');
});

it('returns a promise which rejects and but warns in console when a handler not being defined and missingHandlerPolicy is "warn-and-return-error"', async () => {
mockClient = createMockClient({
missingHandlerPolicy: 'warn-and-return-error',
});

let promise = mockClient.query({ query: queryTwo });

await expect(promise).rejects.toThrowError('Request handler not defined for query');
expect(console.warn).toBeCalledTimes(1);
expect(console.warn).toBeCalledWith(`Request handler not defined for query: ${print(queryTwo)}`);
});
});
});

Expand Down
9 changes: 5 additions & 4 deletions src/mockClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ interface CustomOptions {
missingHandlerPolicy?: MissingHandlerPolicy;
}

export type MockApolloClientOptions = Partial<Omit<ApolloClientOptions<NormalizedCacheObject>, 'link'>> & CustomOptions | undefined;
export type MockApolloClientOptions = (Partial<Omit<ApolloClientOptions<NormalizedCacheObject>, 'link'>> & CustomOptions) | undefined;

export const createMockClient = (options?: MockApolloClientOptions): MockApolloClient => {
export const createMockClient = (options: MockApolloClientOptions = {}): MockApolloClient => {
if ((options as any)?.link) {
throw new Error('Providing link to use is not supported.');
}
const { missingHandlerPolicy, ...restOptions } = options;

const mockLink = new MockLink();
const mockLink = new MockLink({ missingHandlerPolicy });

const client = new ApolloClient({
cache: new Cache({
addTypename: false,
}),
...options,
...restOptions,
link: mockLink
});

Expand Down

0 comments on commit 1b68292

Please sign in to comment.