Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mockLink.ts #11004

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/testing/core/mocking/mockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface MockedResponse<

export interface MockLinkOptions {
showWarnings?: boolean;
preserveMocks?: boolean;
}

function requestToKey(request: GraphQLRequest, addTypename: Boolean): string {
Expand All @@ -47,6 +48,7 @@ function requestToKey(request: GraphQLRequest, addTypename: Boolean): string {
export class MockLink extends ApolloLink {
public operation: Operation;
public addTypename: Boolean = true;
public preserveMocks: Boolean = false;
public showWarnings: boolean = true;
private mockedResponsesByKey: { [key: string]: MockedResponse[] } = {};

Expand All @@ -58,6 +60,7 @@ export class MockLink extends ApolloLink {
super();
this.addTypename = addTypename;
this.showWarnings = options.showWarnings ?? true;
this.preserveMocks = options.preserveMocks ?? false;

if (mockedResponses) {
mockedResponses.forEach(mockedResponse => {
Expand Down Expand Up @@ -116,13 +119,15 @@ ${unmatchedVars.map(d => ` ${stringifyForDisplay(d)}`).join('\n')}

if (this.showWarnings) {
console.warn(
configError.message +
configError.message +
'\nThis typically indicates a configuration error in your mocks ' +
'setup, usually due to a typo or mismatched variable.'
);
}
} else {
mockedResponses.splice(responseIndex, 1);
if (!this.preserveMocks) {
mockedResponses.splice(responseIndex, 1);
}

const { newData } = response;
if (newData) {
Expand Down