Skip to content

Commit

Permalink
Update mockLink.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiancedenogallego authored Jun 22, 2023
1 parent 6875b62 commit 9b3fdbd
Showing 1 changed file with 7 additions and 2 deletions.
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

0 comments on commit 9b3fdbd

Please sign in to comment.