File tree Expand file tree Collapse file tree 3 files changed +58
-1
lines changed Expand file tree Collapse file tree 3 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 1
- import type { DocumentNode } from '../../core' ;
1
+ import type {
2
+ ApolloClient ,
3
+ DocumentNode ,
4
+ OperationVariables ,
5
+ } from '../../core' ;
2
6
3
7
interface ApolloCustomMatchers < R = void > {
4
8
/**
5
9
* Used to determine if two GraphQL query documents are equal to each other by
6
10
* comparing their printed values. The document must be parsed by `gql`.
7
11
*/
8
12
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 ;
9
25
}
10
26
11
27
declare global {
Original file line number Diff line number Diff line change 1
1
import { expect } from '@jest/globals' ;
2
2
import { toMatchDocument } from './toMatchDocument' ;
3
+ import { toHaveSuspenseCacheEntryUsing } from './toHaveSuspenseCacheEntryUsing' ;
3
4
4
5
expect . extend ( {
6
+ toHaveSuspenseCacheEntryUsing,
5
7
toMatchDocument,
6
8
} ) ;
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments