Skip to content

Commit

Permalink
suggestion: mock global fetch explicitly (#11779)
Browse files Browse the repository at this point in the history
    * feat: accept min and max delay in createSchemaFetch

    * chore: add snapshot of invariant error and add tests

    * chore: update api reports and .size-limits.json

    * suggestion: mock global `fetch` explicitly

    * chore: update tests

    * chore: extract api

    * chore: update .size-limits.json

    * Clean up Prettier, Size-limit, and Api-Extractor

    ---------

    Co-authored-by: Alessia Bellisario <github@bellisar.io>
    Co-authored-by: alessbell <alessbell@users.n
oreply.github.com>
  • Loading branch information
alessbell committed Apr 12, 2024
1 parent ee21d11 commit 1b9306f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 8 additions & 8 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);
using _fetch = createSchemaFetch(schema).mockGlobal();

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);
using _fetch = createSchemaFetch(forkedSchema).mockGlobal();

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);
using _fetch = createSchemaFetch(schema).mockGlobal();

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);
using _fetch = createSchemaFetch(forkedSchema).mockGlobal();

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);
using _fetch = createSchemaFetch(forkedSchema).mockGlobal();

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);
using _fetch = createSchemaFetch(forkedSchema).mockGlobal();

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);
using _fetch = createSchemaFetch(forkedSchema).mockGlobal();

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);
using _fetch = createSchemaFetch(forkedSchema).mockGlobal();

const client = new ApolloClient({
cache: new InMemoryCache(),
Expand Down
20 changes: 15 additions & 5 deletions src/testing/core/createSchemaFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,23 @@ const createSchemaFetch = (
});
};

window.fetch = mockFetch;
function mockGlobal() {
window.fetch = mockFetch;

const restore = () => {
window.fetch = prevFetch;
};
const restore = () => {
if (window.fetch === mockFetch) {
window.fetch = prevFetch;
}
};

return withCleanup({ restore }, restore);
}

return withCleanup({ mock: mockFetch, restore }, restore);
return Object.assign(mockFetch, {
mockGlobal,
// if https://github.com/rbuckton/proposal-using-enforcement lands
// [Symbol.enter]: mockGlobal
});
};

export { createSchemaFetch };

0 comments on commit 1b9306f

Please sign in to comment.