Skip to content

Commit ee7f76c

Browse files
Add the ability to preload a query outside of React (#11412)
Co-authored-by: Lenz Weber-Tronic <[email protected]> Co-authored-by: jerelmiller <[email protected]>
1 parent 11803b9 commit ee7f76c

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

src/testing/internal/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
export * from "./profile/index.js";
22
export * from "./disposables/index.js";
33
export { ObservableStream } from "./ObservableStream.js";
4+
5+
export type {
6+
SimpleCaseData,
7+
PaginatedCaseData,
8+
PaginatedCaseVariables,
9+
VariablesCaseData,
10+
VariablesCaseVariables,
11+
} from "./scenarios/index.js";
12+
export {
13+
setupSimpleCase,
14+
setupVariablesCase,
15+
setupPaginatedCase,
16+
} from "./scenarios/index.js";
17+
18+
export type {
19+
RenderWithClientOptions,
20+
RenderWithMocksOptions,
21+
} from "./renderHelpers.js";
22+
export { renderWithClient, renderWithMocks } from "./renderHelpers.js";

src/testing/matchers/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
DocumentNode,
44
OperationVariables,
55
} from "../../core/index.js";
6+
import type { QueryReference } from "../../react/index.js";
67
import {
78
NextRenderOptions,
89
Profiler,
@@ -11,6 +12,11 @@ import {
1112
} from "../internal/index.js";
1213

1314
interface ApolloCustomMatchers<R = void, T = {}> {
15+
/**
16+
* Used to determine if a queryRef has been disposed.
17+
*/
18+
toBeDisposed: T extends QueryReference<any, any> ? () => R
19+
: { error: "matcher needs to be called on a QueryReference" };
1420
/**
1521
* Used to determine if two GraphQL query documents are equal to each other by
1622
* comparing their printed values. The document must be parsed by `gql`.

src/testing/matchers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { toMatchDocument } from "./toMatchDocument.js";
33
import { toHaveSuspenseCacheEntryUsing } from "./toHaveSuspenseCacheEntryUsing.js";
44
import { toRerender, toRenderExactlyTimes } from "./ProfiledComponent.js";
55
import { toBeGarbageCollected } from "./toBeGarbageCollected.js";
6+
import { toBeDisposed } from "./toBeDisposed.js";
67

78
expect.extend({
9+
toBeDisposed,
810
toHaveSuspenseCacheEntryUsing,
911
toMatchDocument,
1012
toRerender,

src/testing/matchers/toBeDisposed.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { MatcherFunction } from "expect";
2+
import type { QueryReference } from "../../react/cache/QueryReference.js";
3+
import {
4+
InternalQueryReference,
5+
unwrapQueryRef,
6+
} from "../../react/cache/QueryReference.js";
7+
8+
function isQueryRef(queryRef: unknown): queryRef is QueryReference {
9+
try {
10+
return unwrapQueryRef(queryRef as any) instanceof InternalQueryReference;
11+
} catch (e) {
12+
return false;
13+
}
14+
}
15+
16+
export const toBeDisposed: MatcherFunction<[]> = function (queryRef) {
17+
const hint = this.utils.matcherHint("toBeDisposed", "queryRef", "", {
18+
isNot: this.isNot,
19+
});
20+
21+
if (!isQueryRef(queryRef)) {
22+
throw new Error(`\n${hint}\n\nmust be called with a valid QueryReference`);
23+
}
24+
25+
const pass = unwrapQueryRef(queryRef).disposed;
26+
27+
return {
28+
pass,
29+
message: () => {
30+
return `${hint}\n\nExpected queryRef ${
31+
this.isNot ? "not " : ""
32+
}to be disposed, but it was${this.isNot ? "" : " not"}.`;
33+
},
34+
};
35+
};

0 commit comments

Comments
 (0)