Skip to content

Commit fbe55b3

Browse files
Suspense cache: use Trie directly (#10969)
Co-authored-by: Jerel Miller <[email protected]>
1 parent 7a7a778 commit fbe55b3

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/testing/matchers/index.d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
import type { DocumentNode } from '../../core';
1+
import type {
2+
ApolloClient,
3+
DocumentNode,
4+
OperationVariables,
5+
} from '../../core';
26

37
interface ApolloCustomMatchers<R = void> {
48
/**
59
* Used to determine if two GraphQL query documents are equal to each other by
610
* comparing their printed values. The document must be parsed by `gql`.
711
*/
812
toMatchDocument(document: DocumentNode): R;
13+
14+
/**
15+
* Used to determine if the Suspense cache has a cache entry.
16+
*/
17+
toHaveSuspenseCacheEntryUsing(
18+
client: ApolloClient<unknown>,
19+
query: DocumentNode,
20+
options?: {
21+
variables?: OperationVariables;
22+
queryKey?: string | number | any[];
23+
}
24+
): R;
925
}
1026

1127
declare global {

src/testing/matchers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from '@jest/globals';
22
import { toMatchDocument } from './toMatchDocument';
3+
import { toHaveSuspenseCacheEntryUsing } from './toHaveSuspenseCacheEntryUsing';
34

45
expect.extend({
6+
toHaveSuspenseCacheEntryUsing,
57
toMatchDocument,
68
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { MatcherFunction } from 'expect';
2+
import type { DocumentNode } from 'graphql';
3+
import type { ApolloClient, OperationVariables } from '../../core';
4+
import { SuspenseCache } from '../../react';
5+
import { canonicalStringify } from '../../cache';
6+
7+
export const toHaveSuspenseCacheEntryUsing: MatcherFunction<
8+
[
9+
client: ApolloClient<unknown>,
10+
query: DocumentNode,
11+
options: {
12+
variables?: OperationVariables;
13+
queryKey?: string | number | any[];
14+
}
15+
]
16+
> = function (
17+
suspenseCache,
18+
client,
19+
query,
20+
{ variables, queryKey = [] } = Object.create(null)
21+
) {
22+
if (!(suspenseCache instanceof SuspenseCache)) {
23+
throw new Error('Actual must be an instance of `SuspenseCache`');
24+
}
25+
26+
const cacheKey = (
27+
[client, query, canonicalStringify(variables)] as any[]
28+
).concat(queryKey);
29+
const queryRef = suspenseCache['queryRefs'].lookupArray(cacheKey)?.current;
30+
31+
return {
32+
pass: !!queryRef,
33+
message: () => {
34+
return `Expected suspense cache ${
35+
queryRef ? 'not ' : ''
36+
}to have cache entry using key`;
37+
},
38+
};
39+
};

0 commit comments

Comments
 (0)