Skip to content

Commit

Permalink
Revert "suggestion: mock global fetch explicitly (#11779)"
Browse files Browse the repository at this point in the history
This reverts commit 243c538.
  • Loading branch information
alessbell committed Apr 12, 2024
1 parent 243c538 commit ee21d11
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 271 deletions.
15 changes: 5 additions & 10 deletions .api-reports/api-report-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,11 @@ export function createMockClient<TData>(data: TData, query: DocumentNode, variab

// @alpha
export const createSchemaFetch: (schema: GraphQLSchema, mockFetchOpts?: {
validate?: boolean;
delay?: {
min: number;
max: number;
};
}) => ((uri: any, options: any) => Promise<Response>) & {
mockGlobal: () => {
restore: () => void;
} & Disposable;
};
validate: boolean;
}) => {
mock: (uri: any, options: any) => Promise<Response>;
restore: () => void;
} & Disposable;

// Warning: (ae-forgotten-export) The symbol "TestSchemaOptions" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts
Expand Down
15 changes: 5 additions & 10 deletions .api-reports/api-report-testing_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,11 @@ export function createMockClient<TData>(data: TData, query: DocumentNode, variab

// @alpha
export const createSchemaFetch: (schema: GraphQLSchema, mockFetchOpts?: {
validate?: boolean;
delay?: {
min: number;
max: number;
};
}) => ((uri: any, options: any) => Promise<Response>) & {
mockGlobal: () => {
restore: () => void;
} & Disposable;
};
validate: boolean;
}) => {
mock: (uri: any, options: any) => Promise<Response>;
restore: () => void;
} & Disposable;

// Warning: (ae-forgotten-export) The symbol "TestSchemaOptions" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts
Expand Down
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39539,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32810
"dist/apollo-client.min.cjs": 39538,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32809
}

This file was deleted.

204 changes: 8 additions & 196 deletions src/testing/core/__tests__/createTestSchema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe("schema proxy", () => {
it("mocks scalars and resolvers", async () => {
const Profiler = createDefaultProfiler<ViewerQueryData>();

using _fetch = createSchemaFetch(schema).mockGlobal();
using _fetch = createSchemaFetch(schema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -266,7 +266,7 @@ describe("schema proxy", () => {

const Profiler = createDefaultProfiler<ViewerQueryData>();

using _fetch = createSchemaFetch(forkedSchema).mockGlobal();
using _fetch = createSchemaFetch(forkedSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -349,7 +349,7 @@ describe("schema proxy", () => {
it("does not pollute the original schema", async () => {
const Profiler = createDefaultProfiler<ViewerQueryData>();

using _fetch = createSchemaFetch(schema).mockGlobal();
using _fetch = createSchemaFetch(schema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -444,7 +444,7 @@ describe("schema proxy", () => {

const Profiler = createDefaultProfiler<ViewerQueryData>();

using _fetch = createSchemaFetch(forkedSchema).mockGlobal();
using _fetch = createSchemaFetch(forkedSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -566,7 +566,7 @@ describe("schema proxy", () => {

const Profiler = createDefaultProfiler<ViewerQueryData>();

using _fetch = createSchemaFetch(forkedSchema).mockGlobal();
using _fetch = createSchemaFetch(forkedSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -709,7 +709,7 @@ describe("schema proxy", () => {

const { ErrorBoundary } = createTrackedErrorComponents(Profiler);

using _fetch = createSchemaFetch(forkedSchema).mockGlobal();
using _fetch = createSchemaFetch(forkedSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -789,7 +789,7 @@ describe("schema proxy", () => {
const { ErrorBoundary } = createTrackedErrorComponents(Profiler);

// @ts-expect-error - we're intentionally passing an invalid schema
using _fetch = createSchemaFetch(forkedSchema).mockGlobal();
using _fetch = createSchemaFetch(forkedSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -916,7 +916,7 @@ describe("schema proxy", () => {

const Profiler = createDefaultProfiler<ViewerQueryData>();

using _fetch = createSchemaFetch(forkedSchema).mockGlobal();
using _fetch = createSchemaFetch(forkedSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down Expand Up @@ -1036,192 +1036,4 @@ describe("schema proxy", () => {

unmount();
});

it("createSchemaFetch respects min and max delay", async () => {
const Profiler = createDefaultProfiler<ViewerQueryData>();

const maxDelay = 2000;

using _fetch = createSchemaFetch(schema, {
delay: { min: 10, max: maxDelay },
}).mockGlobal();

const client = new ApolloClient({
cache: new InMemoryCache(),
uri,
});

const query: TypedDocumentNode<ViewerQueryData> = gql`
query {
viewer {
id
name
age
book {
id
title
publishedAt
}
}
}
`;

const Fallback = () => {
useTrackRenders();
return <div>Loading...</div>;
};

const App = () => {
return (
<React.Suspense fallback={<Fallback />}>
<Child />
</React.Suspense>
);
};

const Child = () => {
const result = useSuspenseQuery(query);

useTrackRenders();

Profiler.mergeSnapshot({
result,
} as Partial<{}>);

return <div>Hello</div>;
};

const { unmount, rerender } = renderWithClient(<App />, {
client,
wrapper: Profiler,
});

// initial suspended render
await Profiler.takeRender();

{
try {
const { snapshot: _snapshot } = await Profiler.takeRender();
} catch (e) {
// default timeout is 1000, so this throws
if (e instanceof Error) {
expect(e.message).toMatch(
/Exceeded timeout waiting for next render./
);
}
}
}

rerender(<App />);

// suspended render
await Profiler.takeRender();

{
// with a timeout > maxDelay, this passes
const { snapshot } = await Profiler.takeRender({
timeout: maxDelay + 100,
});

expect(snapshot.result?.data).toEqual({
viewer: {
__typename: "User",
age: 42,
id: "1",
name: "Jane Doe",
book: {
__typename: "TextBook",
id: "1",
publishedAt: "2024-01-01",
title: "The Book",
},
},
});
}

unmount();
});

it("should call invariant.error if min delay is greater than max delay", async () => {
using _consoleSpy = spyOnConsole.takeSnapshots("error");
const Profiler = createDefaultProfiler<ViewerQueryData>();

using _fetch = createSchemaFetch(schema, {
delay: { min: 3000, max: 1000 },
}).mockGlobal();

const client = new ApolloClient({
cache: new InMemoryCache(),
uri,
});

const query: TypedDocumentNode<ViewerQueryData> = gql`
query {
viewer {
id
name
age
book {
id
title
publishedAt
}
}
}
`;

const Fallback = () => {
useTrackRenders();
return <div>Loading...</div>;
};

const App = () => {
return (
<React.Suspense fallback={<Fallback />}>
<Child />
</React.Suspense>
);
};

const Child = () => {
const result = useSuspenseQuery(query);

useTrackRenders();

Profiler.mergeSnapshot({
result,
} as Partial<{}>);

return <div>Hello</div>;
};

const { unmount } = renderWithClient(<App />, {
client,
wrapper: Profiler,
});

// suspended render
await Profiler.takeRender();

{
const { snapshot } = await Profiler.takeRender();

expect(snapshot.result?.data).toEqual({
viewer: {
__typename: "User",
age: 42,
id: "1",
name: "Jane Doe",
book: {
__typename: "TextBook",
id: "1",
publishedAt: "2024-01-01",
title: "The Book",
},
},
});
}

unmount();
});
});
Loading

0 comments on commit ee21d11

Please sign in to comment.