Skip to content

Commit

Permalink
fix(suspense): always return a promise (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianOsipiuk authored Sep 2, 2021
1 parent ab76c10 commit e701752
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 26 deletions.
19 changes: 1 addition & 18 deletions src/vue/__tests__/useQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,7 @@ describe("useQuery", () => {
});

describe("suspense", () => {
test("should return undefined by default", () => {
const query = useQuery("suspense1", simpleFetcher);
const result = query.suspense();

expect(result).toEqual(undefined);
});

test("should return undefined when not used in a vue component", () => {
const getCurrentInstanceSpy = getCurrentInstance as jest.Mock;
getCurrentInstanceSpy.mockImplementation(() => null);

const query = useQuery("suspense2", simpleFetcher);
const result = query.suspense();

expect(result).toEqual(undefined);
});

test("should return a Promise when inside a Suspense boundary", () => {
test("should return a Promise", () => {
const getCurrentInstanceSpy = getCurrentInstance as jest.Mock;
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }));

Expand Down
10 changes: 2 additions & 8 deletions src/vue/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ToRefs,
reactive,
watchEffect,
getCurrentInstance,
} from "vue-demi";

import type { QueryObserver } from "react-query/core";
Expand All @@ -30,7 +29,7 @@ export type UseQueryReturnType<
TError,
Result = UseQueryResult<TData, TError>
> = ToRefs<Readonly<Result>> & {
suspense: () => Promise<Result> | void;
suspense: () => Promise<Result>;
};

export function useBaseQuery<TQueryFnData, TError, TData, TQueryData>(
Expand Down Expand Up @@ -59,12 +58,7 @@ export function useBaseQuery<TQueryFnData, TError, TData, TQueryData>(
>;

// Suspense
const currentInstance = getCurrentInstance();
// @ts-expect-error Suspense is considered experimental and not exposed
const isSuspense = Boolean(currentInstance?.suspense);
const suspense = isSuspense
? () => observer.fetchOptimistic(defaultedOptions)
: () => undefined;
const suspense = () => observer.fetchOptimistic(defaultedOptions);

return {
...resultRefs,
Expand Down

1 comment on commit e701752

@vercel
Copy link

@vercel vercel bot commented on e701752 Sep 2, 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.