Skip to content

Commit dc220cb

Browse files
committed
Move toHaveSuspenseCacheEntryUsing matchers to global matchers
1 parent 1649e9b commit dc220cb

File tree

4 files changed

+58
-46
lines changed

4 files changed

+58
-46
lines changed

src/react/hooks/__tests__/useSuspenseQuery.test.tsx

-45
Original file line numberDiff line numberDiff line change
@@ -47,51 +47,6 @@ import { ApolloProvider } from '../../context';
4747
import { SuspenseCache } from '../../cache';
4848
import { SuspenseQueryHookFetchPolicy } from '../../../react';
4949
import { useSuspenseQuery } from '../useSuspenseQuery';
50-
import { canonicalStringify } from '../../../cache';
51-
52-
expect.extend({
53-
toHaveSuspenseCacheEntryUsing(
54-
suspenseCache: SuspenseCache,
55-
client: ApolloClient<unknown>,
56-
query: DocumentNode,
57-
{
58-
variables,
59-
queryKey = [],
60-
}: {
61-
variables?: OperationVariables;
62-
queryKey?: string | number | any[];
63-
} = Object.create(null)
64-
) {
65-
const cacheKey = (
66-
[client, query, canonicalStringify(variables)] as any[]
67-
).concat(queryKey);
68-
const queryRef = suspenseCache['queryRefs'].lookupArray(cacheKey)?.current;
69-
70-
return {
71-
pass: !!queryRef,
72-
message: () => {
73-
return `Expected suspense cache ${
74-
queryRef ? 'not ' : ''
75-
}to have cache entry using key`;
76-
},
77-
};
78-
},
79-
});
80-
81-
declare global {
82-
namespace jest {
83-
interface Matchers<R = void> {
84-
toHaveSuspenseCacheEntryUsing(
85-
client: ApolloClient<unknown>,
86-
query: DocumentNode,
87-
options?: {
88-
variables?: OperationVariables;
89-
queryKey?: string | number | any[];
90-
}
91-
): R;
92-
}
93-
}
94-
}
9550

9651
type RenderSuspenseHookOptions<Props, TSerializedCache = {}> = Omit<
9752
RenderHookOptions<Props>,

src/testing/matchers/index.d.ts

+17-1
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

+2
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
});
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)