Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Mar 4, 2024
1 parent 4b786c2 commit 8e0bb7c
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions packages/client-react-streaming/src/registerApolloClient.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-inner-declarations */
import { it } from "node:test";
import assert from "node:assert";
import { runInConditions } from "./util/runInConditions.js";
Expand Down Expand Up @@ -129,3 +130,66 @@ it("calling `getClient` with parameters results in an error", async () => {
/You cannot pass arguments into `getClient`./.test(error?.message || "")
);
});

it("warns if `makeClient` calls that should return different `ApolloClient` instances return the same one (outside of React)", () => {
const warn = console.warn;
try {
let warnArgs: unknown[] | undefined = undefined;
console.warn = (...args) => (warnArgs = args);
const same = makeClient();
const { getClient } = registerApolloClient(() => same);

getClient();
// `console.warn` has not been called
assert.equal(warnArgs, undefined);

getClient();
// `console.warn` has been called
assert.ok(
/Multiple calls to `getClient` for different requests returned the same client instance./i.test(
String(warnArgs![0])
)
);
} finally {
console.warn = warn;
}
});

it("warns if `makeClient` calls that should return different `ApolloClient` instances return the same one (in React)", async () => {
const warn = console.warn;
try {
let warnArgs: unknown[] | undefined = undefined;
console.warn = (...args) => (warnArgs = args);
const same = makeClient();
const { getClient } = registerApolloClient(() => same);

function App() {
getClient();
// it should be perfectly fine and not cause any errors to call `getClient` in here as often as we want to
getClient();
getClient();
return <div></div>;
}

{
const stream = renderToPipeableStream(React.createElement(App), {});
await drain(stream);
}
// we had only one request, `console.warn` has not been called
assert.equal(warnArgs, undefined);

{
// second render - different request, but returns `same` ApolloClient as before
const stream = renderToPipeableStream(React.createElement(App), {});
await drain(stream);
}

assert.ok(
/Multiple calls to `getClient` for different requests returned the same client instance./i.test(
String(warnArgs![0])
)
);
} finally {
console.warn = warn;
}
});

0 comments on commit 8e0bb7c

Please sign in to comment.