Repository: alibaba/hooks
Pull request #1762 (2022-07-14 04:07:01)

doc(useInterval): update Description
<!--
First of all, thank you for your contribution! 😄

New feature please send a pull request to master branch.
Pull requests will be merged after one of the collaborators approve.
Please makes sure that these forms are filled before submitting your pull request, thank you!
-->

[[中文版模板 / Chinese template](https://github.com/alibaba/hooks/blob/master/.github/PULL_REQUEST_TEMPLATE/pr_cn.md)]

### 🤔 This is a ...

- [ ] New feature
- [ ] Bug fix
- [x] Site / documentation update
- [ ] Demo update
- [ ] TypeScript definition update
- [ ] Bundle size optimization
- [ ] Performance optimization
- [ ] Enhancement feature
- [ ] Internationalization
- [ ] Refactoring
- [ ] Code style optimization
- [ ] Test Case
- [ ] Branch merge
- [ ] Other (about what?)

### 🔗 Related issue link

<!--
1. Describe the source of requirement, like related issue link.
-->

### 💡 Background and solution

<!--
1. Describe the problem and the scenario.
2. GIF or snapshot should be provided if includes UI/interactive modification.
3. How to fix the problem, and list final API implementation and usage sample if that is a new feature.
-->

### 📝 Changelog

<!--
Describe changes from the user side, and list all potential break changes or other risks.
--->

| Language   | Changelog |
| ---------- | --------- |
| 🇺🇸 English |           |
| 🇨🇳 Chinese |           |

### ☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

- [ ] Doc is updated/provided or not needed
- [ ] Demo is updated/provided or not needed
- [ ] TypeScript definition is updated/provided or not needed
- [ ] Changelog is provided or not needed



Patch:
diff --git a/package.json b/package.json
index fae2a205..7d0c277b 100755
--- a/package.json
+++ b/package.json
@@ -6,7 +6,7 @@
     "url": "git+https://github.com/alibaba/hooks.git"
   },
   "scripts": {
-    "init": "pnpm install && pnpm run build",
+    "init": "pnpm clean && pnpm install && pnpm run build",
     "start": "pnpm run dev",
     "dev": "dumi dev",
     "clean": "rimraf 'packages/*/{lib,es,node_modules,dist}' && rimraf node_modules",
diff --git a/packages/hooks/src/useInterval/index.zh-CN.md b/packages/hooks/src/useInterval/index.zh-CN.md
index 32086a89..e2fe0dc9 100644
--- a/packages/hooks/src/useInterval/index.zh-CN.md
+++ b/packages/hooks/src/useInterval/index.zh-CN.md
@@ -29,11 +29,11 @@ useInterval(
 
 ### Params
 
-| 参数    | 说明                                        | 类型                    |
-| ------- | ------------------------------------------- | ----------------------- |
-| fn      | 要定时调用的函数                            | `() => void`            |
-| delay   | 间隔时间，当取值 `undefined` 时会停止计时器 | `number` \| `undefined` |
-| options | 配置计时器的行为                            | `Options`               |
+| 参数    | 说明                                            | 类型                    |
+| ------- | ----------------------------------------------- | ----------------------- |
+| fn      | 要定时调用的函数                                | `() => void`            |
+| delay   | 间隔时间，当设置值为 `undefined` 时会停止计时器 | `number` \| `undefined` |
+| options | 配置计时器的行为                                | `Options`               |
 
 ### Options
 


Test patch:
diff --git a/packages/hooks/src/useLongPress/__tests__/index.test.ts b/packages/hooks/src/useLongPress/__tests__/index.test.ts
index 52d88e5a..122feb34 100644
--- a/packages/hooks/src/useLongPress/__tests__/index.test.ts
+++ b/packages/hooks/src/useLongPress/__tests__/index.test.ts
@@ -1,12 +1,12 @@
 import { renderHook } from '@testing-library/react-hooks';
 import useLongPress from '../index';
-import type { Options } from '../index';
+import { sleep } from '../../utils/testingHelpers';
 
 const mockCallback = jest.fn();
 const mockClickCallback = jest.fn();
 const mockLongPressEndCallback = jest.fn();
 
-let events = {};
+const events = {};
 const mockTarget = {
   addEventListener: jest.fn((event, callback) => {
     events[event] = callback;
@@ -16,38 +16,30 @@ const mockTarget = {
   }),
 };
 
-const setup = (onLongPress: any, target, options?: Options) =>
-  renderHook(() => useLongPress(onLongPress, target, options));
-
 describe('useLongPress', () => {
-  beforeEach(() => {
-    jest.useFakeTimers();
-  });
-
-  afterEach(() => {
-    events = {};
-    jest.useRealTimers();
-  });
-
-  it('longPress callback correct', () => {
-    setup(mockCallback, mockTarget, {
-      onClick: mockClickCallback,
-      onLongPressEnd: mockLongPressEndCallback,
-    });
+  it('longPress callback correct', async () => {
+    renderHook(() =>
+      useLongPress(mockCallback, mockTarget as any, {
+        onClick: mockClickCallback,
+        onLongPressEnd: mockLongPressEndCallback,
+      }),
+    );
     expect(mockTarget.addEventListener).toBeCalled();
     events['mousedown']();
-    jest.advanceTimersByTime(350);
+    await sleep(350);
     events['mouseleave']();
     expect(mockCallback).toBeCalledTimes(1);
     expect(mockLongPressEndCallback).toBeCalledTimes(1);
     expect(mockClickCallback).toBeCalledTimes(0);
   });
 
-  it('click callback correct', () => {
-    setup(mockCallback, mockTarget, {
-      onClick: mockClickCallback,
-      onLongPressEnd: mockLongPressEndCallback,
-    });
+  it('click callback correct', async () => {
+    renderHook(() =>
+      useLongPress(mockCallback, mockTarget as any, {
+        onClick: mockClickCallback,
+        onLongPressEnd: mockLongPressEndCallback,
+      }),
+    );
     expect(mockTarget.addEventListener).toBeCalled();
     events['mousedown']();
     events['mouseup']();
@@ -58,14 +50,16 @@ describe('useLongPress', () => {
     expect(mockClickCallback).toBeCalledTimes(2);
   });
 
-  it('longPress and click callback correct', () => {
-    setup(mockCallback, mockTarget, {
-      onClick: mockClickCallback,
-      onLongPressEnd: mockLongPressEndCallback,
-    });
+  it('longPress and click callback correct', async () => {
+    renderHook(() =>
+      useLongPress(mockCallback, mockTarget as any, {
+        onClick: mockClickCallback,
+        onLongPressEnd: mockLongPressEndCallback,
+      }),
+    );
     expect(mockTarget.addEventListener).toBeCalled();
     events['mousedown']();
-    jest.advanceTimersByTime(350);
+    await sleep(350);
     events['mouseup']();
     events['mousedown']();
     events['mouseup']();
@@ -73,37 +67,4 @@ describe('useLongPress', () => {
     expect(mockLongPressEndCallback).toBeCalledTimes(1);
     expect(mockClickCallback).toBeCalledTimes(1);
   });
-
-  it('onLongPress should not be called when over the threshold', () => {
-    const { unmount } = setup(mockCallback, mockTarget, {
-      moveThreshold: {
-        x: 30,
-        y: 20,
-      },
-    });
-    expect(events['mousemove']).toBeDefined();
-    events['mousedown'](new MouseEvent('mousedown'));
-    events['mousemove'](
-      new MouseEvent('mousemove', {
-        clientX: 40,
-        clientY: 10,
-      }),
-    );
-    jest.advanceTimersByTime(320);
-    expect(mockCallback).not.toBeCalled();
-
-    unmount();
-    expect(events['mousemove']).toBeUndefined();
-  });
-
-  it(`should not work when target don't support addEventListener method`, () => {
-    Object.defineProperty(mockTarget, 'addEventListener', {
-      get() {
-        return false;
-      },
-    });
-
-    setup(() => {}, mockTarget);
-    expect(Object.keys(events)).toHaveLength(0);
-  });
 });