Skip to content

Commit d6eff96

Browse files
feat: use onScopeDispose instead of onUnmount in composables (#179)
* fix: change from onUnmount to onScopeDispose hook * fix: use onScopeDispose hook only in vue 3 * fix: formatting mistakes * fix: switched back to onScopeDispose and instead bumped the nuxtjs/composition-api * test: fix some test names Co-authored-by: Damian Osipiuk <osipiukd+git@gmail.com>
1 parent adce06a commit d6eff96

File tree

14 files changed

+44
-44
lines changed

14 files changed

+44
-44
lines changed

examples/nuxt-simple/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"precommit": "npm run lint"
1010
},
1111
"dependencies": {
12-
"@nuxtjs/composition-api": "^0.24.4",
12+
"@nuxtjs/composition-api": "^0.28.0",
1313
"nuxt": "^2.0.0",
1414
"vue-query": "^1.5.1"
1515
},

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
"vue-demi": "0.10.1"
4848
},
4949
"peerDependencies": {
50-
"@nuxtjs/composition-api": "^0.24.4",
51-
"@vue/composition-api": "^1.0.0",
50+
"@nuxtjs/composition-api": "^0.28.0",
51+
"@vue/composition-api": "^1.1.2",
5252
"vue": "^2.0.0 || >=3.0.0"
5353
},
5454
"peerDependenciesMeta": {

src/__mocks__/vue-demi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ module.exports = {
99
...vue,
1010
inject: jest.fn(),
1111
provide: jest.fn(),
12-
onUnmounted: jest.fn(),
12+
onScopeDispose: jest.fn(),
1313
getCurrentInstance: jest.fn(() => ({ proxy: {} })),
1414
};

src/vue/__tests__/useIsFetching.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { onUnmounted } from "vue-demi";
1+
import { onScopeDispose } from "vue-demi";
22
import { setLogger } from "react-query/core";
33

44
import { flushPromises, simpleFetcher, noop } from "./test-utils";
@@ -33,13 +33,13 @@ describe("useIsFetching", () => {
3333
expect(isFetching.value).toStrictEqual(0);
3434
});
3535

36-
test("should stop listening to changes on onUnmount", async () => {
37-
const onUnmountedMock = onUnmounted as jest.MockedFunction<
38-
typeof onUnmounted
36+
test("should stop listening to changes on onScopeDispose", async () => {
37+
const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
38+
typeof onScopeDispose
3939
>;
40-
onUnmountedMock.mockImplementation((fn) => fn());
40+
onScopeDisposeMock.mockImplementation((fn) => fn());
4141

42-
const { status } = useQuery("onUnmounted", simpleFetcher);
42+
const { status } = useQuery("onScopeDispose", simpleFetcher);
4343
const isFetching = useIsFetching();
4444

4545
expect(status.value).toStrictEqual("loading");
@@ -55,6 +55,6 @@ describe("useIsFetching", () => {
5555
expect(status.value).toStrictEqual("loading");
5656
expect(isFetching.value).toStrictEqual(1);
5757

58-
onUnmountedMock.mockReset();
58+
onScopeDisposeMock.mockReset();
5959
});
6060
});

src/vue/__tests__/useIsMutating.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { onUnmounted } from "vue-demi";
1+
import { onScopeDispose } from "vue-demi";
22
import { setLogger } from "react-query/core";
33

44
import { flushPromises, noop, successMutator } from "./test-utils";
@@ -36,11 +36,11 @@ describe("useIsMutating", () => {
3636
expect(isMutating.value).toStrictEqual(0);
3737
});
3838

39-
test("should stop listening to changes on onUnmount", async () => {
40-
const onUnmountedMock = onUnmounted as jest.MockedFunction<
41-
typeof onUnmounted
39+
test("should stop listening to changes on onScopeDispose", async () => {
40+
const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
41+
typeof onScopeDispose
4242
>;
43-
onUnmountedMock.mockImplementation((fn) => fn());
43+
onScopeDisposeMock.mockImplementation((fn) => fn());
4444

4545
const mutation = useMutation((params: string) => successMutator(params));
4646
const mutation2 = useMutation((params: string) => successMutator(params));
@@ -59,7 +59,7 @@ describe("useIsMutating", () => {
5959

6060
expect(isMutating.value).toStrictEqual(0);
6161

62-
onUnmountedMock.mockReset();
62+
onScopeDisposeMock.mockReset();
6363
});
6464

6565
test("should call `useQueryClient` with a proper `queryClientKey`", async () => {

src/vue/__tests__/useQueries.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { onUnmounted, reactive } from "vue-demi";
1+
import { onScopeDispose, reactive } from "vue-demi";
22
import { setLogger } from "react-query/core";
33

44
import {
@@ -166,11 +166,11 @@ describe("useQueries", () => {
166166
]);
167167
});
168168

169-
test("should stop listening to changes on onUnmount", async () => {
170-
const onUnmountedMock = onUnmounted as jest.MockedFunction<
171-
typeof onUnmounted
169+
test("should stop listening to changes on onScopeDispose", async () => {
170+
const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
171+
typeof onScopeDispose
172172
>;
173-
onUnmountedMock.mockImplementationOnce((fn) => fn());
173+
onScopeDisposeMock.mockImplementationOnce((fn) => fn());
174174

175175
const queries = [
176176
{

src/vue/__tests__/useQuery.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
computed,
33
reactive,
44
ref,
5-
onUnmounted,
5+
onScopeDispose,
66
getCurrentInstance,
77
} from "vue-demi";
88
import { QueryObserver, setLogger } from "react-query/core";
@@ -214,13 +214,13 @@ describe("useQuery", () => {
214214
expect(status.value).toStrictEqual("success");
215215
});
216216

217-
test("should stop listening to changes on onUnmount", async () => {
218-
const onUnmountedMock = onUnmounted as jest.MockedFunction<
219-
typeof onUnmounted
217+
test("should stop listening to changes on onScopeDispose", async () => {
218+
const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
219+
typeof onScopeDispose
220220
>;
221-
onUnmountedMock.mockImplementationOnce((fn) => fn());
221+
onScopeDisposeMock.mockImplementationOnce((fn) => fn());
222222

223-
const { status } = useQuery("onUnmounted", simpleFetcher);
223+
const { status } = useQuery("onScopeDispose", simpleFetcher);
224224

225225
expect(status.value).toStrictEqual("loading");
226226

src/vue/__tests__/useQueryProvider.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { provide, onUnmounted } from "vue-demi";
1+
import { provide, onScopeDispose } from "vue-demi";
22
import { QueryClient } from "../queryClient";
33

44
import { useQueryProvider } from "../useQueryProvider";
@@ -10,7 +10,7 @@ jest.mock("../queryClient", () => ({
1010

1111
describe("useQueryProvider", () => {
1212
const provideSpy = provide as jest.Mock;
13-
const onUnmountedSpy = onUnmounted as jest.Mock;
13+
const onScopeDisposeSpy = onScopeDispose as jest.Mock;
1414
const queryClientSpy = QueryClient as jest.Mock;
1515

1616
const mount = jest.fn();
@@ -74,8 +74,8 @@ describe("useQueryProvider", () => {
7474
);
7575
});
7676

77-
test("should call unmount on QueryClient", () => {
78-
onUnmountedSpy.mockImplementationOnce((fn) => fn());
77+
test("should call onScopeDispose on QueryClient", () => {
78+
onScopeDisposeSpy.mockImplementationOnce((fn) => fn());
7979

8080
useQueryProvider();
8181

src/vue/useBaseQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
onUnmounted,
2+
onScopeDispose,
33
toRefs,
44
readonly,
55
ToRefs,
@@ -71,7 +71,7 @@ export function useBaseQuery<
7171
{ deep: true }
7272
);
7373

74-
onUnmounted(() => {
74+
onScopeDispose(() => {
7575
unsubscribe();
7676
});
7777

src/vue/useIsFetching.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { onUnmounted, Ref, ref, watchEffect } from "vue-demi";
1+
import { onScopeDispose, Ref, ref, watchEffect } from "vue-demi";
22
import type { QueryKey } from "react-query/types/core";
33
import type { QueryFilters as QF } from "react-query/types/core/utils";
44

@@ -34,7 +34,7 @@ export function useIsFetching(
3434
filters.value = parsedFiltersUpdate;
3535
});
3636

37-
onUnmounted(() => {
37+
onScopeDispose(() => {
3838
unsubscribe();
3939
});
4040

0 commit comments

Comments
 (0)