Skip to content

Commit c10ad65

Browse files
fix: suspense respects enabled and stale (#238)
* fix: suspense respects enabled and stale * test: suspense tests * fix: suspense test against disabled specifically
1 parent 2ab927d commit c10ad65

13 files changed

+62
-40
lines changed

jest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const config: Config.InitialOptions = {
1616
// Exlude devtools
1717
"!src/vuejs/devtools/**/*",
1818
],
19+
clearMocks: true,
1920
};
2021

2122
export default config;

src/vuejs/__tests__/mutationCache.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ describe("MutationCache", () => {
99
jest.spyOn(MutationCacheOrigin.prototype, "findAll");
1010
});
1111

12-
beforeEach(() => {
13-
jest.clearAllMocks();
14-
});
15-
1612
describe("find", () => {
1713
test("should properly unwrap parameters", async () => {
1814
const mutationCache = new MutationCache();

src/vuejs/__tests__/queryCache.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ describe("QueryCache", () => {
99
jest.spyOn(QueryCacheOrigin.prototype, "findAll");
1010
});
1111

12-
beforeEach(() => {
13-
jest.clearAllMocks();
14-
});
15-
1612
describe("find", () => {
1713
test("should properly unwrap parameters", async () => {
1814
const queryCache = new QueryCache();

src/vuejs/__tests__/queryClient.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ describe("QueryCache", () => {
1616
// jest.spyOn(QueryCacheOrigin.prototype, "findAll");
1717
// });
1818

19-
beforeEach(() => {
20-
jest.clearAllMocks();
21-
});
22-
2319
describe("isFetching", () => {
2420
test("should properly unwrap 1 parameter", async () => {
2521
const queryClient = new QueryClient();

src/vuejs/__tests__/useInfiniteQuery.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { useInfiniteQuery } from "../useInfiniteQuery";
44
jest.mock("../useQueryClient");
55

66
describe("useQuery", () => {
7-
beforeEach(() => {
8-
jest.clearAllMocks();
9-
});
10-
117
test("should properly execute infinite query", async () => {
128
const { data, fetchNextPage, status } = useInfiniteQuery(
139
["infiniteQuery"],

src/vuejs/__tests__/useIsFetching.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import { parseFilterArgs, useIsFetching } from "../useIsFetching";
77
jest.mock("../useQueryClient");
88

99
describe("useIsFetching", () => {
10-
beforeEach(() => {
11-
jest.clearAllMocks();
12-
});
13-
1410
test("should properly return isFetching state", async () => {
1511
const { isFetching: isFetchingQuery } = useQuery(
1612
["isFetching1"],

src/vuejs/__tests__/useIsMutating.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import { useQueryClient } from "../useQueryClient";
88
jest.mock("../useQueryClient");
99

1010
describe("useIsMutating", () => {
11-
beforeEach(() => {
12-
jest.clearAllMocks();
13-
});
14-
1511
test("should properly return isMutating state", async () => {
1612
const mutation = useMutation((params: string) => successMutator(params));
1713
const mutation2 = useMutation((params: string) => successMutator(params));

src/vuejs/__tests__/useMutation.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import { useQueryClient } from "../useQueryClient";
66
jest.mock("../useQueryClient");
77

88
describe("useMutation", () => {
9-
beforeEach(() => {
10-
jest.clearAllMocks();
11-
});
12-
139
test("should be in idle state initially", () => {
1410
const mutation = useMutation((params) => successMutator(params));
1511

src/vuejs/__tests__/useQueries.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import { useQueries } from "../useQueries";
1111
jest.mock("../useQueryClient");
1212

1313
describe("useQueries", () => {
14-
beforeEach(() => {
15-
jest.clearAllMocks();
16-
});
17-
1814
test("should return result for each query", () => {
1915
const queries = [
2016
{

src/vuejs/__tests__/useQuery.test.ts

+38-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ jest.mock("../useQueryClient");
2020
jest.mock("../useBaseQuery");
2121

2222
describe("useQuery", () => {
23-
beforeEach(() => {
24-
jest.clearAllMocks();
25-
});
26-
2723
test("should properly execute query", () => {
2824
useQuery(["key0"], simpleFetcher, { staleTime: 1000 });
2925

@@ -239,10 +235,47 @@ describe("useQuery", () => {
239235
const getCurrentInstanceSpy = getCurrentInstance as jest.Mock;
240236
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }));
241237

242-
const query = useQuery(["suspense3"], simpleFetcher);
238+
const query = useQuery(["suspense"], simpleFetcher);
243239
const result = query.suspense();
244240

245241
expect(result).toBeInstanceOf(Promise);
246242
});
243+
244+
test("should resolve after being enabled", () => {
245+
const getCurrentInstanceSpy = getCurrentInstance as jest.Mock;
246+
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }));
247+
248+
let afterTimeout = false;
249+
const isEnabeld = ref(false);
250+
const query = useQuery(["suspense"], simpleFetcher, {
251+
enabled: isEnabeld,
252+
});
253+
254+
setTimeout(() => {
255+
afterTimeout = true;
256+
isEnabeld.value = true;
257+
}, 200);
258+
259+
return query.suspense().then((result) => {
260+
expect(afterTimeout).toBe(true);
261+
});
262+
});
263+
264+
test("should resolve immidiately when stale without refetching", () => {
265+
const getCurrentInstanceSpy = getCurrentInstance as jest.Mock;
266+
getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }));
267+
268+
const fetcherSpy = jest.fn(() => simpleFetcher());
269+
270+
// let afterTimeout = false;
271+
const query = useQuery(["suspense"], simpleFetcher, {
272+
staleTime: 10000,
273+
initialData: "foo",
274+
});
275+
276+
return query.suspense().then(() => {
277+
expect(fetcherSpy).toHaveBeenCalledTimes(0);
278+
});
279+
});
247280
});
248281
});

src/vuejs/__tests__/useQueryClient.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe("useQueryClient", () => {
88

99
beforeEach(() => {
1010
jest.restoreAllMocks();
11-
jest.clearAllMocks();
1211
});
1312

1413
test("should return queryClient when it is provided in the context", () => {

src/vuejs/__tests__/vueQueryPlugin.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function getAppMock(withUnmountHook = false): TestApp {
3737

3838
describe("VueQueryPlugin", () => {
3939
beforeEach(() => {
40-
jest.clearAllMocks();
4140
window.__VUE_QUERY_CONTEXT__ = undefined;
4241
});
4342

src/vuejs/useBaseQuery.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,31 @@ export function useBaseQuery<
7676
unsubscribe();
7777
});
7878

79+
const suspense = () => {
80+
return new Promise<QueryObserverResult<TData, TError>>((resolve) => {
81+
const run = () => {
82+
const newOptions = queryClient.defaultQueryOptions(
83+
getQueryUnreffedOptions()
84+
);
85+
if (newOptions.enabled !== false) {
86+
const optimisticResult = observer.getOptimisticResult(newOptions);
87+
if (optimisticResult.isStale) {
88+
resolve(observer.fetchOptimistic(defaultedOptions));
89+
} else {
90+
resolve(optimisticResult);
91+
}
92+
}
93+
};
94+
95+
run();
96+
97+
watch([() => arg1, () => arg2, () => arg3], run, { deep: true });
98+
});
99+
};
100+
79101
return {
80102
...(toRefs(readonly(state)) as UseQueryReturnType<TData, TError>),
81-
suspense: () => observer.fetchOptimistic(defaultedOptions),
103+
suspense,
82104
};
83105

84106
/**

0 commit comments

Comments
 (0)