Skip to content

Commit ba1b9cd

Browse files
committed
test(api-client): add type tests for getMutationCache in createAPIOperationClient
1 parent d18656d commit ba1b9cd

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint-disable @typescript-eslint/no-unused-expressions */
2+
3+
import type { CreateAPIBasicQueryClientOptions } from '@openapi-qraft/react';
4+
import { requestFn } from '@openapi-qraft/react';
5+
import * as callbacks from '@openapi-qraft/react/callbacks/index';
6+
import { QueryClient } from '@tanstack/react-query';
7+
import {
8+
createInternalReactAPIClient as createAPIOperationClient,
9+
services,
10+
} from './fixtures/files-api/index.js';
11+
12+
const queryClient = new QueryClient();
13+
14+
// Default callbacks from redocly config should expose getMutationCache for mutation operations.
15+
const apiWithDefaultCallbacks = createAPIOperationClient(services, {
16+
queryClient,
17+
});
18+
19+
apiWithDefaultCallbacks.files.deleteFiles.getMutationCache();
20+
// @ts-expect-error - query operations don't expose getMutationCache
21+
apiWithDefaultCallbacks.files.getFileList.getMutationCache();
22+
23+
// For basic query options, provided callbacks should define the available methods.
24+
type MutationCacheOnlyCallbacks = {
25+
getMutationCache: typeof callbacks.getMutationCache;
26+
};
27+
28+
const customBasicQueryCallbacks: MutationCacheOnlyCallbacks = {
29+
getMutationCache: callbacks.getMutationCache,
30+
};
31+
32+
const basicQueryOptions: CreateAPIBasicQueryClientOptions = { queryClient };
33+
34+
const apiWithCustomBasicQueryCallbacksExplicit = createAPIOperationClient<
35+
typeof services,
36+
MutationCacheOnlyCallbacks
37+
>(services, basicQueryOptions, customBasicQueryCallbacks);
38+
39+
apiWithCustomBasicQueryCallbacksExplicit.files.deleteFiles.getMutationCache();
40+
41+
// getMutationCache should remain unavailable without queryClient.
42+
const apiWithoutQueryClient = createAPIOperationClient<
43+
typeof services,
44+
MutationCacheOnlyCallbacks
45+
>(
46+
services,
47+
{
48+
requestFn,
49+
baseUrl: 'https://example.com',
50+
},
51+
customBasicQueryCallbacks
52+
);
53+
54+
// @ts-expect-error - getMutationCache requires queryClient in options
55+
apiWithoutQueryClient.files.deleteFiles.getMutationCache();

0 commit comments

Comments
 (0)