Skip to content

Commit

Permalink
fix: strengthen MockedResponse.newData type (#11592)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen2 committed Feb 15, 2024
1 parent a0d40aa commit 1133469
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .api-reports/api-report-testing.md
Expand Up @@ -933,7 +933,7 @@ export interface MockedResponse<TData = Record<string, any>, TVariables = Record
// (undocumented)
maxUsageCount?: number;
// (undocumented)
newData?: ResultFunction<FetchResult>;
newData?: ResultFunction<FetchResult<TData>, TVariables>;
// (undocumented)
request: GraphQLRequest<TVariables>;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-testing_core.md
Expand Up @@ -888,7 +888,7 @@ export interface MockedResponse<TData = Record<string, any>, TVariables = Record
// (undocumented)
maxUsageCount?: number;
// (undocumented)
newData?: ResultFunction<FetchResult>;
newData?: ResultFunction<FetchResult<TData>, TVariables>;
// (undocumented)
request: GraphQLRequest<TVariables>;
// (undocumented)
Expand Down
5 changes: 5 additions & 0 deletions .changeset/dirty-insects-return.md
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Strengthen `MockedResponse.newData` type
66 changes: 65 additions & 1 deletion src/testing/core/mocking/__tests__/mockLink.ts
@@ -1,7 +1,71 @@
import gql from "graphql-tag";
import { MockLink } from "../mockLink";
import { MockLink, MockedResponse } from "../mockLink";
import { execute } from "../../../../link/core/execute";

describe("MockedResponse.newData", () => {
const setup = () => {
const weaklyTypedMockResponse: MockedResponse = {
request: {
query: gql`
query A {
a
}
`,
},
};

const stronglyTypedMockResponse: MockedResponse<
{ a: string },
{ input: string }
> = {
request: {
query: gql`
query A {
a
}
`,
},
};

return {
weaklyTypedMockResponse,
stronglyTypedMockResponse,
};
};

test("returned 'data' can be any object with untyped response", () => {
const { weaklyTypedMockResponse } = setup();

weaklyTypedMockResponse.newData = ({ fake: { faker } }) => ({
data: {
pretend: faker,
},
});
});

test("can't return output that doesn't match TData", () => {
const { stronglyTypedMockResponse } = setup();

// @ts-expect-error return type does not match `TData`
stronglyTypedMockResponse.newData = () => ({
data: {
a: 123,
},
});
});

test("can't use input variables that don't exist in TVariables", () => {
const { stronglyTypedMockResponse } = setup();

// @ts-expect-error unknown variables
stronglyTypedMockResponse.newData = ({ fake: { faker } }) => ({
data: {
a: faker,
},
});
});
});

/*
We've chosen this value as the MAXIMUM_DELAY since values that don't fit into a 32-bit signed int cause setTimeout to fire immediately
*/
Expand Down
2 changes: 1 addition & 1 deletion src/testing/core/mocking/mockLink.ts
Expand Up @@ -35,7 +35,7 @@ export interface MockedResponse<
error?: Error;
delay?: number;
variableMatcher?: VariableMatcher<TVariables>;
newData?: ResultFunction<FetchResult>;
newData?: ResultFunction<FetchResult<TData>, TVariables>;
}

export interface MockLinkOptions {
Expand Down

0 comments on commit 1133469

Please sign in to comment.