-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit tests for
registerApolloClient
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/client-react-streaming/src/registerApolloClient.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { it } from "node:test"; | ||
import assert from "node:assert"; | ||
import { runInConditions } from "./util/runInConditions.js"; | ||
import { Writable } from "node:stream"; | ||
|
||
runInConditions("react-server"); | ||
|
||
const { registerApolloClient, ApolloClient, InMemoryCache } = await import( | ||
"#bundled" | ||
); | ||
|
||
type ReactServer = { | ||
renderToPipeableStream( | ||
model: React.ReactNode, | ||
webpackMap: unknown, | ||
options?: { | ||
environmentName?: string; | ||
onError?: (error: any) => void; | ||
onPostpone?: (reason: string) => void; | ||
identifierPrefix?: string; | ||
} | ||
): { | ||
abort(reason: any): void; | ||
pipe<T extends Writable>(destination: T): T; | ||
}; | ||
}; | ||
|
||
const { renderToPipeableStream } = (await import( | ||
// @ts-expect-error close enough | ||
"react-server-dom-webpack/server" | ||
)) as ReactServer; | ||
const React = await import("react"); | ||
|
||
function drain(stream: ReturnType<typeof renderToPipeableStream>) { | ||
let result = ""; | ||
return new Promise<string>((resolve) => { | ||
stream.pipe( | ||
new Writable({ | ||
write(chunk, _encoding, callback) { | ||
result += chunk.toString(); | ||
callback(); | ||
}, | ||
final(callback) { | ||
resolve(result); | ||
callback(); | ||
}, | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
function makeClient() { | ||
return new ApolloClient({ | ||
cache: new InMemoryCache(), | ||
connectToDevTools: false, | ||
}); | ||
} | ||
|
||
it("calling `getClient` outside of a React render creates a new instance every time", () => { | ||
const { getClient } = registerApolloClient(makeClient); | ||
|
||
const client1 = getClient(); | ||
const client2 = getClient(); | ||
assert.notStrictEqual(client1, client2); | ||
}); | ||
|
||
it("calling `getClient` twice during the same React render will return the same instance", async () => { | ||
const { getClient } = registerApolloClient(makeClient); | ||
|
||
const clients: any[] = []; | ||
function App() { | ||
clients.push(getClient()); | ||
clients.push(getClient()); | ||
return <div></div>; | ||
} | ||
|
||
const stream = renderToPipeableStream(React.createElement(App), {}); | ||
await drain(stream); | ||
|
||
assert.equal(clients.length, 2); | ||
assert.ok(clients[0] instanceof ApolloClient); | ||
assert.strictEqual(clients[0], clients[1]); | ||
}); | ||
|
||
it("calling `getClient` twice during different React renders will return different instances", async () => { | ||
const { getClient } = registerApolloClient(makeClient); | ||
|
||
const clients: any[] = []; | ||
function App() { | ||
clients.push(getClient()); | ||
return <div></div>; | ||
} | ||
|
||
{ | ||
const stream = renderToPipeableStream(React.createElement(App), {}); | ||
await drain(stream); | ||
} | ||
{ | ||
const stream = renderToPipeableStream(React.createElement(App), {}); | ||
await drain(stream); | ||
} | ||
|
||
assert.equal(clients.length, 2); | ||
assert.ok(clients[0] instanceof ApolloClient); | ||
assert.notStrictEqual(clients[0], clients[1]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters