Skip to content

Commit

Permalink
Maintain MockLink public API
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier committed Oct 16, 2020
1 parent 4ce6195 commit ef936ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
30 changes: 22 additions & 8 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
// mocks
import mockQueryManager from '../../../utilities/testing/mocking/mockQueryManager';
import mockWatchQuery from '../../../utilities/testing/mocking/mockWatchQuery';
import { MockApolloLink, mockSingleLink, MockLink } from '../../../utilities/testing/mocking/mockLink';
import { MockApolloLink, mockSingleLink } from '../../../utilities/testing/mocking/mockLink';

// core
import { ApolloQueryResult } from '../../types';
Expand Down Expand Up @@ -495,10 +495,20 @@ describe('QueryManager', () => {
const onRequestSubscribe = jest.fn();
const onRequestUnsubscribe = jest.fn();

const mockedSingleLink = new MockLink([mockedResponse], {
addTypename: true,
onSubscribe: onRequestSubscribe,
onUnsubscribe: onRequestUnsubscribe
const mockedSingleLink = new ApolloLink(() => {
return new Observable(observer => {
onRequestSubscribe();

const timer = setTimeout(() => {
observer.next(mockedResponse.result);
observer.complete();
}, 0);

return () => {
onRequestUnsubscribe();
clearTimeout(timer);
};
});
});

const mockedQueryManger = new QueryManager({
Expand All @@ -512,10 +522,14 @@ describe('QueryManager', () => {
notifyOnNetworkStatusChange: false
});

const observerCallback = wrap(reject, () => {
reject(new Error('Link subscription should have been cancelled'));
});

const subscription = observableQuery.subscribe({
next: wrap(reject, () => {
reject(new Error('Link subscriptions should have been cancelled'));
}),
next: observerCallback,
error: observerCallback,
complete: observerCallback
});

subscription.unsubscribe();
Expand Down
2 changes: 1 addition & 1 deletion src/react/components/__tests__/client/Mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('General Mutation testing', () => {

function mockClient(m: any) {
return new ApolloClient({
link: new MockLink(m, { addTypename: false }),
link: new MockLink(m, false),
cache: new Cache({ addTypename: false })
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/testing/mocking/MockedProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class MockedProvider extends React.Component<
defaultOptions,
link: link || new MockLink(
mocks || [],
{ addTypename },
addTypename,
),
resolvers,
});
Expand Down
21 changes: 4 additions & 17 deletions src/utilities/testing/mocking/mockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,17 @@ function requestToKey(request: GraphQLRequest, addTypename: Boolean): string {
return JSON.stringify(requestKey);
}

interface MockLinkOptions {
addTypename?: boolean;
onSubscribe?: () => void;
onUnsubscribe?: () => void;
}

export class MockLink extends ApolloLink {
public operation: Operation;
public addTypename: Boolean = true;
private mockedResponsesByKey: { [key: string]: MockedResponse[] } = {};
private options: MockLinkOptions;

constructor(
mockedResponses: ReadonlyArray<MockedResponse>,
options: MockLinkOptions = {}
addTypename: Boolean = true
) {
super();
this.options = options;
this.addTypename = options.addTypename ?? true;
this.addTypename = addTypename;
if (mockedResponses) {
mockedResponses.forEach(mockedResponse => {
this.addMockedResponse(mockedResponse);
Expand Down Expand Up @@ -117,9 +109,7 @@ export class MockLink extends ApolloLink {
}
}

const requestObservable = new Observable<FetchResult>(observer => {
this.options.onSubscribe?.();

return new Observable(observer => {
const timer = setTimeout(() => {
if (configError) {
try {
Expand Down Expand Up @@ -151,12 +141,9 @@ export class MockLink extends ApolloLink {
}, response && response.delay || 0);

return () => {
this.options.onUnsubscribe?.();
clearTimeout(timer);
};
});

return requestObservable;
}

private normalizeMockedResponse(
Expand Down Expand Up @@ -196,5 +183,5 @@ export function mockSingleLink(
maybeTypename = true;
}

return new MockLink(mocks, { addTypename: maybeTypename });
return new MockLink(mocks, maybeTypename);
}

0 comments on commit ef936ed

Please sign in to comment.