Skip to content

Commit c204e24

Browse files
committed
fix: fix
1 parent a45270a commit c204e24

6 files changed

+76
-81
lines changed

packages/hooks/src/useRequest/__tests__/useCachePlugin.test.tsx

+39-40
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { act, renderHook, waitFor } from "@testing-library/react";
2-
import { render } from "@testing-library/react";
3-
import useRequest, { clearCache } from "../index";
4-
import { request } from "../../utils/testingHelpers";
5-
import React, { useState } from "react";
6-
import "jest-localstorage-mock";
7-
8-
describe("useCachePlugin", () => {
1+
import { act, renderHook, waitFor } from '@testing-library/react';
2+
import { render } from '@testing-library/react';
3+
import useRequest, { clearCache } from '../index';
4+
import { request } from '../../utils/testingHelpers';
5+
import React, { useState } from 'react';
6+
import 'jest-localstorage-mock';
7+
8+
describe('useCachePlugin', () => {
99
jest.useFakeTimers();
1010

11-
const setup = (service, options) =>
12-
renderHook(() => useRequest(service, options));
11+
const setup = (service, options) => renderHook(() => useRequest(service, options));
1312

1413
const testCacheKey = async (options: any) => {
1514
const hook = setup(request, options);
@@ -18,79 +17,79 @@ describe("useCachePlugin", () => {
1817
jest.advanceTimersByTime(1000);
1918
});
2019
expect(hook.result.current.loading).toBe(false);
21-
expect(hook.result.current.data).toBe("success");
20+
expect(hook.result.current.data).toBe('success');
2221
hook.unmount();
2322
};
2423

25-
it("useRequest cacheKey should work", async () => {
24+
it('useRequest cacheKey should work', async () => {
2625
await testCacheKey({
27-
cacheKey: "testCacheKey",
26+
cacheKey: 'testCacheKey',
2827
});
2928

3029
jest.advanceTimersByTime(100);
3130

3231
const hook2 = setup(request, {
33-
cacheKey: "testCacheKey",
32+
cacheKey: 'testCacheKey',
3433
});
3534
expect(hook2.result.current.loading).toBe(true);
36-
expect(hook2.result.current.data).toBe("success");
35+
expect(hook2.result.current.data).toBe('success');
3736
await act(async () => {
3837
jest.advanceTimersByTime(1000);
3938
});
4039
expect(hook2.result.current.loading).toBe(false);
4140
});
4241

43-
it("useRequest staleTime should work", async () => {
42+
it('useRequest staleTime should work', async () => {
4443
await testCacheKey({
45-
cacheKey: "testStaleTime",
44+
cacheKey: 'testStaleTime',
4645
staleTime: 3000,
4746
});
4847

4948
jest.advanceTimersByTime(1000);
5049

5150
const hook2 = setup(request, {
52-
cacheKey: "testStaleTime",
51+
cacheKey: 'testStaleTime',
5352
staleTime: 3000,
5453
});
5554
expect(hook2.result.current.loading).toBe(false);
56-
expect(hook2.result.current.data).toBe("success");
55+
expect(hook2.result.current.data).toBe('success');
5756
hook2.unmount();
5857

5958
jest.advanceTimersByTime(3001);
6059

6160
const hook3 = setup(request, {
62-
cacheKey: "testStaleTime",
61+
cacheKey: 'testStaleTime',
6362
staleTime: 3000,
6463
});
6564
expect(hook3.result.current.loading).toBe(true);
66-
expect(hook3.result.current.data).toBe("success");
65+
expect(hook3.result.current.data).toBe('success');
6766

6867
await act(async () => {
6968
jest.advanceTimersByTime(1000);
7069
});
7170
expect(hook3.result.current.loading).toBe(false);
7271
});
7372

74-
it("useRequest cacheTime should work", async () => {
73+
it('useRequest cacheTime should work', async () => {
7574
await testCacheKey({
76-
cacheKey: "testCacheTime",
75+
cacheKey: 'testCacheTime',
7776
cacheTime: 5000,
7877
});
7978

8079
jest.advanceTimersByTime(1000);
8180

8281
const hook2 = setup(request, {
83-
cacheKey: "testCacheTime",
82+
cacheKey: 'testCacheTime',
8483
cacheTime: 5000,
8584
});
8685
expect(hook2.result.current.loading).toBe(true);
87-
expect(hook2.result.current.data).toBe("success");
86+
expect(hook2.result.current.data).toBe('success');
8887
hook2.unmount();
8988

9089
jest.advanceTimersByTime(5001);
9190

9291
const hook3 = setup(request, {
93-
cacheKey: "testCacheTime",
92+
cacheKey: 'testCacheTime',
9493
cacheTime: 5000,
9594
});
9695
expect(hook3.result.current.loading).toBe(true);
@@ -100,46 +99,46 @@ describe("useCachePlugin", () => {
10099
jest.advanceTimersByTime(1000);
101100
});
102101
expect(hook3.result.current.loading).toBe(false);
103-
expect(hook3.result.current.data).toBe("success");
102+
expect(hook3.result.current.data).toBe('success');
104103
});
105104

106-
it("clearCache should work", async () => {
107-
await testCacheKey("testClearCache");
105+
it('clearCache should work', async () => {
106+
await testCacheKey('testClearCache');
108107

109-
clearCache("testClearCache");
108+
clearCache('testClearCache');
110109
const hook2 = setup(request, {
111-
cacheKey: "testClearCache",
110+
cacheKey: 'testClearCache',
112111
});
113112
expect(hook2.result.current.loading).toBe(true);
114113
expect(hook2.result.current.data).toBeUndefined();
115114
});
116115

117-
it("setCache/getCache should work", async () => {
116+
it('setCache/getCache should work', async () => {
118117
const cacheKey = `setCacheKey`;
119118
await testCacheKey({
120119
cacheKey,
121120
setCache: (data) => localStorage.setItem(cacheKey, JSON.stringify(data)),
122-
getCache: () => JSON.parse(localStorage.getItem(cacheKey) || "{}"),
121+
getCache: () => JSON.parse(localStorage.getItem(cacheKey) || '{}'),
123122
});
124123

125124
jest.advanceTimersByTime(1000);
126125
const hook2 = setup(request, {
127126
cacheKey,
128127
setCache: (data) => localStorage.setItem(cacheKey, JSON.stringify(data)),
129-
getCache: () => JSON.parse(localStorage.getItem(cacheKey) || "{}"),
128+
getCache: () => JSON.parse(localStorage.getItem(cacheKey) || '{}'),
130129
});
131130
expect(hook2.result.current.loading).toBe(true);
132-
expect(hook2.result.current.data).toBe("success");
131+
expect(hook2.result.current.data).toBe('success');
133132

134133
await act(async () => {
135134
jest.advanceTimersByTime(1000);
136135
});
137136
expect(hook2.result.current.loading).toBe(false);
138137
});
139138

140-
it("cache should work when change data immediately", async () => {
139+
it('cache should work when change data immediately', async () => {
141140
const { result } = setup(request, {
142-
cacheKey: "mutateCacheKey",
141+
cacheKey: 'mutateCacheKey',
143142
});
144143
act(() => {
145144
result.current.mutate(1);
@@ -149,12 +148,12 @@ describe("useCachePlugin", () => {
149148
jest.advanceTimersByTime(1000);
150149
});
151150
expect(result.current.loading).toBe(false);
152-
expect(result.current.data).toBe("success");
151+
expect(result.current.data).toBe('success');
153152
});
154153

155154
//github.com/alibaba/hooks/issues/1859
156-
it("error should reset with activeKey", async () => {
157-
const errSpy = jest.spyOn(console, "error").mockImplementation(() => {});
155+
it('error should reset with activeKey', async () => {
156+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
158157

159158
let res = {} as any;
160159
const TestComponent = () => {

packages/hooks/src/useRequest/__tests__/useDebouncePlugin.test.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { act, renderHook, waitFor } from "@testing-library/react";
2-
import useRequest from "../index";
3-
import { request } from "../../utils/testingHelpers";
1+
import { act, renderHook, waitFor } from '@testing-library/react';
2+
import useRequest from '../index';
3+
import { request } from '../../utils/testingHelpers';
44

5-
describe("useDebouncePlugin", () => {
6-
const setUp = (service, options) =>
7-
renderHook((o) => useRequest(service, o || options));
5+
describe('useDebouncePlugin', () => {
6+
const setUp = (service, options) => renderHook((o) => useRequest(service, o || options));
87

98
let hook;
10-
it("useDebouncePlugin should work", async () => {
9+
it('useDebouncePlugin should work', async () => {
1110
jest.useFakeTimers();
1211
const callback = jest.fn();
1312

@@ -20,7 +19,7 @@ describe("useDebouncePlugin", () => {
2019
{
2120
manual: true,
2221
debounceWait: 100,
23-
}
22+
},
2423
);
2524
});
2625

packages/hooks/src/useRequest/__tests__/usePollingPlugin.test.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import type { RenderHookResult } from "@testing-library/react";
2-
import { act, renderHook, waitFor } from "@testing-library/react";
3-
import useRequest from "../index";
4-
import { request } from "../../utils/testingHelpers";
1+
import type { RenderHookResult } from '@testing-library/react';
2+
import { act, renderHook, waitFor } from '@testing-library/react';
3+
import useRequest from '../index';
4+
import { request } from '../../utils/testingHelpers';
55

6-
describe("usePollingPlugin", () => {
6+
describe('usePollingPlugin', () => {
77
jest.useFakeTimers();
88

9-
const setUp = (service, options) =>
10-
renderHook((o) => useRequest(service, o || options));
9+
const setUp = (service, options) => renderHook((o) => useRequest(service, o || options));
1110

1211
let hook: RenderHookResult<any, any>;
1312

14-
it("usePollingPlugin pollingInterval=100 pollingWhenHidden=true should work", async () => {
13+
it('usePollingPlugin pollingInterval=100 pollingWhenHidden=true should work', async () => {
1514
const callback = jest.fn();
1615
act(() => {
1716
hook = setUp(
@@ -22,7 +21,7 @@ describe("usePollingPlugin", () => {
2221
{
2322
pollingInterval: 100,
2423
pollingWhenHidden: true,
25-
}
24+
},
2625
);
2726
});
2827
expect(hook.result.current.loading).toBe(true);
@@ -31,7 +30,7 @@ describe("usePollingPlugin", () => {
3130
jest.runAllTimers();
3231
});
3332
await waitFor(() => expect(hook.result.current.loading).toBe(false));
34-
expect(hook.result.current.data).toBe("success");
33+
expect(hook.result.current.data).toBe('success');
3534
expect(callback).toHaveBeenCalledTimes(1);
3635

3736
act(() => {
@@ -68,7 +67,7 @@ describe("usePollingPlugin", () => {
6867
});
6968

7069
let hook2: RenderHookResult<any, any>;
71-
it("usePollingPlugin pollingErrorRetryCount=3 should work", async () => {
70+
it('usePollingPlugin pollingErrorRetryCount=3 should work', async () => {
7271
// if request error and set pollingErrorRetryCount
7372
// and the number of consecutive failures exceeds pollingErrorRetryCount, polling stops
7473
let errorCallback: jest.Mock | undefined = undefined;

packages/hooks/src/useRequest/__tests__/useRefreshOnWindowFocusPlugin.test.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import type { RenderHookResult } from "@testing-library/react";
2-
import { act, renderHook, waitFor } from "@testing-library/react";
3-
import { fireEvent } from "@testing-library/react";
4-
import useRequest from "../index";
5-
import { request } from "../../utils/testingHelpers";
1+
import type { RenderHookResult } from '@testing-library/react';
2+
import { act, renderHook, waitFor } from '@testing-library/react';
3+
import { fireEvent } from '@testing-library/react';
4+
import useRequest from '../index';
5+
import { request } from '../../utils/testingHelpers';
66

7-
describe("useRefreshOnWindowFocusPlugin", () => {
7+
describe('useRefreshOnWindowFocusPlugin', () => {
88
jest.useFakeTimers();
99

10-
const setUp = (service, options) =>
11-
renderHook((o) => useRequest(service, o || options));
10+
const setUp = (service, options) => renderHook((o) => useRequest(service, o || options));
1211

1312
let hook: RenderHookResult<any, any>;
14-
it("useRefreshOnWindowFocusPlugin should work", async () => {
13+
it('useRefreshOnWindowFocusPlugin should work', async () => {
1514
act(() => {
1615
hook = setUp(request, {
1716
refreshOnWindowFocus: true,
@@ -39,7 +38,7 @@ describe("useRefreshOnWindowFocusPlugin", () => {
3938
expect(hook.result.current.loading).toBe(true);
4039
});
4140

42-
it("fix: multiple unsubscriptions should not delete the last subscription listener ", async () => {
41+
it('fix: multiple unsubscriptions should not delete the last subscription listener ', async () => {
4342
let hook1;
4443
let hook2;
4544
act(() => {

packages/hooks/src/useRequest/__tests__/useThrottlePlugin.test.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import type { RenderHookResult } from "@testing-library/react";
2-
import { act, renderHook } from "@testing-library/react";
3-
import useRequest from "../index";
4-
import { request } from "../../utils/testingHelpers";
1+
import type { RenderHookResult } from '@testing-library/react';
2+
import { act, renderHook } from '@testing-library/react';
3+
import useRequest from '../index';
4+
import { request } from '../../utils/testingHelpers';
55

6-
describe("useThrottlePlugin", () => {
6+
describe('useThrottlePlugin', () => {
77
jest.useFakeTimers();
88

9-
const setUp = (service, options) =>
10-
renderHook((o) => useRequest(service, o || options));
9+
const setUp = (service, options) => renderHook((o) => useRequest(service, o || options));
1110

1211
let hook: RenderHookResult<any, any>;
13-
it("useThrottlePlugin should work", async () => {
12+
it('useThrottlePlugin should work', async () => {
1413
const callback = jest.fn();
1514

1615
act(() => {
@@ -22,7 +21,7 @@ describe("useThrottlePlugin", () => {
2221
{
2322
manual: true,
2423
throttleWait: 100,
25-
}
24+
},
2625
);
2726
});
2827

packages/hooks/src/utils/testingHelpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export function request(req: any) {
1010
return new Promise((resolve, reject) =>
1111
setTimeout(() => {
1212
if (req === 0) {
13-
reject(new Error("fail"));
13+
reject(new Error('fail'));
1414
} else {
15-
resolve("success");
15+
resolve('success');
1616
}
17-
}, 1000)
17+
}, 1000),
1818
);
1919
}

0 commit comments

Comments
 (0)