Skip to content

Commit 1810d13

Browse files
committed
fix: reactivity when passing more than one argument to hooks
1 parent 593119b commit 1810d13

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

src/utils.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,58 @@ export function isQueryKey(value: unknown): value is QueryKey {
1616

1717
export function parseQueryArgs<TOptions extends QueryOptions<any, any, any>>(
1818
arg1: QueryKey | TOptions,
19-
arg2?: QueryFunction | TOptions,
20-
arg3?: TOptions
19+
arg2: QueryFunction | TOptions = {} as TOptions,
20+
arg3: TOptions = {} as TOptions
2121
): TOptions {
2222
if (!isQueryKey(arg1)) {
2323
return arg1 as TOptions;
2424
}
2525

2626
if (typeof arg2 === "function") {
27-
return { ...arg3, queryKey: arg1, queryFn: arg2 } as TOptions;
27+
return Object.assign(arg3, {
28+
queryKey: arg1,
29+
queryFn: arg2,
30+
}) as TOptions;
2831
}
2932

30-
return { ...arg2, queryKey: arg1 } as TOptions;
33+
return Object.assign(arg2, { queryKey: arg1 }) as TOptions;
3134
}
3235

3336
export function parseFilterArgs<
3437
TFilters extends QueryFilters,
3538
TOptions = unknown
3639
>(
3740
arg1?: QueryKey | TFilters,
38-
arg2?: TFilters | TOptions,
39-
arg3?: TOptions
41+
arg2: TFilters | TOptions = {} as TOptions,
42+
arg3: TOptions = {} as TOptions
4043
): [TFilters, TOptions | undefined] {
4144
return (isQueryKey(arg1)
42-
? [{ ...arg2, queryKey: arg1 }, arg3]
45+
? [Object.assign(arg2, { queryKey: arg1 }), arg3]
4346
: [arg1 || {}, arg2]) as [TFilters, TOptions];
4447
}
4548

4649
export function parseMutationArgs<
4750
TOptions extends MutationOptions<any, any, any, any>
4851
>(
4952
arg1: MutationKey | MutationFunction<any, any> | TOptions,
50-
arg2?: MutationFunction<any, any> | TOptions,
51-
arg3?: TOptions
53+
arg2: MutationFunction<any, any> | TOptions = {} as TOptions,
54+
arg3: TOptions = {} as TOptions
5255
): TOptions {
5356
if (isQueryKey(arg1)) {
5457
if (typeof arg2 === "function") {
55-
return { ...arg3, mutationKey: arg1, mutationFn: arg2 } as TOptions;
58+
return Object.assign(arg3, {
59+
mutationKey: arg1,
60+
mutationFn: arg2,
61+
}) as TOptions;
5662
}
57-
return { ...arg2, mutationKey: arg1 } as TOptions;
63+
return Object.assign(arg2, { mutationKey: arg1 }) as TOptions;
5864
}
5965

6066
if (typeof arg1 === "function") {
61-
return { ...arg2, mutationFn: arg1 } as TOptions;
67+
return Object.assign(arg2, { mutationFn: arg1 }) as TOptions;
6268
}
6369

64-
return { ...arg1 } as TOptions;
70+
return Object.assign(arg1) as TOptions;
6571
}
6672

6773
export function updateState(

tests/useQuery.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("useQuery", () => {
3838
});
3939

4040
test("should properly execute query with all three parameters", () => {
41-
useQuery("key0", () => simpleFetcher(), { staleTime: 1000 });
41+
useQuery("key0", simpleFetcher, { staleTime: 1000 });
4242

4343
expect(useBaseQuery).toBeCalledWith(
4444
{
@@ -51,7 +51,7 @@ describe("useQuery", () => {
5151
});
5252

5353
test("should properly execute query with queryKey and options", () => {
54-
useQuery("key01", { queryFn: () => simpleFetcher(), staleTime: 1000 });
54+
useQuery("key01", { queryFn: simpleFetcher, staleTime: 1000 });
5555

5656
expect(useBaseQuery).toBeCalledWith(
5757
{
@@ -66,7 +66,7 @@ describe("useQuery", () => {
6666
test("should properly execute query with all three parameters", () => {
6767
useQuery({
6868
queryKey: "key02",
69-
queryFn: () => simpleFetcher(),
69+
queryFn: simpleFetcher,
7070
staleTime: 1000,
7171
});
7272

@@ -81,24 +81,23 @@ describe("useQuery", () => {
8181
});
8282

8383
test("should return loading status initially", () => {
84-
const { status, isLoading, isFetching } = useQuery("key1", () =>
85-
simpleFetcher()
86-
);
84+
const { status, isLoading, isFetching } = useQuery("key1", simpleFetcher);
8785

8886
expect(status.value).toEqual("loading");
8987
expect(isLoading.value).toEqual(true);
9088
expect(isFetching.value).toEqual(true);
9189
});
9290

9391
test("should be marked as stale initially", () => {
94-
const { isStale } = useQuery("key2", () => simpleFetcher());
92+
const { isStale } = useQuery("key2", simpleFetcher);
9593

9694
expect(isStale.value).toEqual(true);
9795
});
9896

9997
test("should return false for other states initially", () => {
100-
const { isSuccess, isError, isIdle, isFetched } = useQuery("key3", () =>
101-
simpleFetcher()
98+
const { isSuccess, isError, isIdle, isFetched } = useQuery(
99+
"key3",
100+
simpleFetcher
102101
);
103102

104103
expect(isSuccess.value).toEqual(false);
@@ -115,7 +114,7 @@ describe("useQuery", () => {
115114
isFetched,
116115
isFetching,
117116
isSuccess,
118-
} = useQuery("key4", () => simpleFetcher());
117+
} = useQuery("key4", simpleFetcher);
119118

120119
await flushPromises();
121120

@@ -137,7 +136,7 @@ describe("useQuery", () => {
137136
isFetching,
138137
isError,
139138
failureCount,
140-
} = useQuery("key5", () => rejectFetcher(), {
139+
} = useQuery("key5", rejectFetcher, {
141140
retry: false,
142141
});
143142

@@ -155,16 +154,17 @@ describe("useQuery", () => {
155154

156155
test("should update query on reactive prop change", async () => {
157156
const spy = jest.fn();
158-
const onSuccessFn = ref(noop);
159-
const onSuccess = () => {
160-
onSuccessFn.value();
161-
};
162-
useQuery("key6", () => simpleFetcher(), {
163-
onSuccess,
164-
staleTime: 1000,
165-
});
157+
const onSuccess = ref(noop);
158+
useQuery(
159+
"key6",
160+
simpleFetcher,
161+
reactive({
162+
onSuccess,
163+
staleTime: 1000,
164+
})
165+
);
166166

167-
onSuccessFn.value = spy;
167+
onSuccess.value = spy;
168168

169169
await flushPromises();
170170

@@ -177,9 +177,9 @@ describe("useQuery", () => {
177177
const enabled = computed(() => !!data.value);
178178

179179
const { status } = useQuery(
180+
"dependant2",
181+
simpleFetcher,
180182
reactive({
181-
queryKey: "dependant2",
182-
queryFn: simpleFetcher,
183183
enabled,
184184
})
185185
);

0 commit comments

Comments
 (0)