Skip to content

Commit

Permalink
feat(hooks): add config param for queryProviders (#40)
Browse files Browse the repository at this point in the history
* Add config param for useQueryProvider

* Add config param for useNuxtQueryProvider
  • Loading branch information
henribru authored and DamianOsipiuk committed Jun 27, 2021
1 parent 7388851 commit cb9b84c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/ssr/useNuxtQueryProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useContext } from "@nuxtjs/composition-api";
import { useQueryClient, useQueryProvider } from "../index";
import type { QueryClientConfig } from "../useQueryProvider";
import { hydrate } from "./hydration";

export function useNuxtQueryProvider(): void {
useQueryProvider();
export function useNuxtQueryProvider(config: QueryClientConfig = {}): void {
useQueryProvider(config);

// @ts-expect-error Nuxt.js injected client property
if (process.client) {
Expand Down
9 changes: 7 additions & 2 deletions src/useQueryProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { QueryClient } from "react-query/core";

import { VUE_QUERY_CLIENT } from "./useQueryClient";

export function useQueryProvider(): void {
const queryClient = new QueryClient();
// QueryClientConfig isn't exported so we need to extract it
export type QueryClientConfig = NonNullable<
ConstructorParameters<typeof QueryClient>[0]
>;

export function useQueryProvider(config: QueryClientConfig = {}): void {
const queryClient = new QueryClient(config);
queryClient.mount();

provide(VUE_QUERY_CLIENT, queryClient);
Expand Down
15 changes: 15 additions & 0 deletions tests/ssr/useNuxtQueryProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ describe("useNuxtQueryProvider", () => {
expect(useQueryProviderSpy).toHaveBeenCalledTimes(1);
});

test("should call queryProvider with passed config", () => {
const config = {
defaultOptions: {
queries: {
enabled: false,
},
},
};
// @ts-expect-error Nuxt.js injected client property
process.client = false;
useNuxtQueryProvider(config);

expect(useQueryProviderSpy).toHaveBeenCalledWith(config);
});

test("should call useQueryClient when vueQueryState is present", () => {
useContextSpy.mockReturnValueOnce(withVueQueryState);
useNuxtQueryProvider();
Expand Down
14 changes: 14 additions & 0 deletions tests/useQueryProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ describe("useQueryProvider", () => {
expect(queryClientSpy).toHaveBeenCalledTimes(1);
});

test("should create queryClient with passed config", () => {
const config = {
defaultOptions: {
queries: {
enabled: false,
},
},
};

useQueryProvider(config);

expect(queryClientSpy).toBeCalledWith(config);
});

test("should call mount on QueryClient", () => {
useQueryProvider();

Expand Down

1 comment on commit cb9b84c

@vercel
Copy link

@vercel vercel bot commented on cb9b84c Jun 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.