Skip to content

Commit 2825885

Browse files
committed
feat: create VueQueryPlugin
1 parent 1f74353 commit 2825885

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// import { successMutator, simpleFetcher } from "./test-utils";
2+
import { QueryClient } from "react-query/types/core";
3+
import { VueQueryPlugin } from "../vueQueryPlugin";
4+
import { VUE_QUERY_CLIENT } from "../useQueryClient";
5+
6+
const vue3AppMock = {
7+
provide: jest.fn(),
8+
};
9+
10+
describe("VueQueryPlugin", () => {
11+
beforeEach(() => {
12+
jest.clearAllMocks();
13+
});
14+
15+
describe("when called without additional options", () => {
16+
test("should instantiate a client with default clientKey", () => {
17+
VueQueryPlugin(vue3AppMock);
18+
19+
expect(vue3AppMock.provide).toHaveBeenCalledWith(
20+
VUE_QUERY_CLIENT,
21+
expect.objectContaining({ defaultOptions: {} })
22+
);
23+
});
24+
});
25+
26+
describe("when called with custom clientKey", () => {
27+
test("should instantiate a client with customized clientKey", () => {
28+
VueQueryPlugin(vue3AppMock, { queryClientKey: "CUSTOM" });
29+
30+
expect(vue3AppMock.provide).toHaveBeenCalledWith(
31+
VUE_QUERY_CLIENT + "CUSTOM",
32+
expect.objectContaining({ defaultOptions: {} })
33+
);
34+
});
35+
});
36+
37+
describe("when called with custom client", () => {
38+
test("should instantiate that custom client", () => {
39+
const customClient = { mount: jest.fn() } as unknown as QueryClient;
40+
VueQueryPlugin(vue3AppMock, { queryClient: customClient });
41+
42+
expect(customClient.mount).toHaveBeenCalled();
43+
expect(vue3AppMock.provide).toHaveBeenCalledWith(
44+
VUE_QUERY_CLIENT,
45+
customClient
46+
);
47+
});
48+
});
49+
50+
describe("when called with custom client condig", () => {
51+
test("should instantiate a client with the provided config", () => {
52+
VueQueryPlugin(vue3AppMock, {
53+
queryClientConfig: {
54+
defaultOptions: { queries: { enabled: true } },
55+
},
56+
});
57+
58+
expect(vue3AppMock.provide).toHaveBeenCalledWith(
59+
VUE_QUERY_CLIENT,
60+
expect.objectContaining({
61+
defaultOptions: { queries: { enabled: true } },
62+
})
63+
);
64+
});
65+
});
66+
});

src/vue/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export { useQueryClient, VUE_QUERY_CLIENT } from "./useQueryClient";
44
export { useQueryProvider } from "./useQueryProvider";
5+
export { VueQueryPlugin } from "./vueQueryPlugin";
56

67
export { useQuery } from "./useQuery";
78
export { useQueries } from "./useQueries";

src/vue/vueQueryPlugin.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { InjectionKey } from "vue-demi";
2+
import { QueryClient } from "react-query/core";
3+
import { VUE_QUERY_CLIENT } from "./useQueryClient";
4+
import { QueryClientConfig } from "./useQueryProvider";
5+
6+
type QueryClientPluginOptions =
7+
| {
8+
queryClientConfig?: QueryClientConfig;
9+
queryClientKey?: string;
10+
}
11+
| {
12+
queryClient?: QueryClient;
13+
queryClientKey?: string;
14+
};
15+
16+
// Vue 3 only
17+
interface Vue3App {
18+
provide<T>(key: InjectionKey<T> | symbol | string, value: T): this;
19+
}
20+
21+
export function VueQueryPlugin(
22+
app: Vue3App,
23+
options: QueryClientPluginOptions = {}
24+
): void {
25+
const clientKeySuffix = options.queryClientKey || "";
26+
let client: QueryClient;
27+
28+
if ("queryClient" in options && options.queryClient) {
29+
client = options.queryClient;
30+
} else {
31+
const clientConfig =
32+
"queryClientConfig" in options ? options.queryClientConfig : undefined;
33+
client = new QueryClient(clientConfig);
34+
}
35+
36+
client.mount();
37+
38+
app.provide(VUE_QUERY_CLIENT + clientKeySuffix, client);
39+
}

0 commit comments

Comments
 (0)