Skip to content

Commit

Permalink
test: useRequest add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
brickspert committed May 24, 2020
1 parent 4795bdc commit 524c270
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 119 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"now-build": "echo \"hello\""
},
"devDependencies": {
"@testing-library/react": "^10.0.4",
"@testing-library/react-hooks": "^2.0.1",
"@types/jest": "^24.0.15",
"@types/lodash.debounce": "^4.0.6",
Expand Down
1 change: 1 addition & 0 deletions packages/use-request/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const {
focusTimespan,
debounceInterval,
throttleInterval,
ready,
});
```

Expand Down
1 change: 1 addition & 0 deletions packages/use-request/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const {
focusTimespan,
debounceInterval,
throttleInterval,
ready,
});
```

Expand Down
66 changes: 0 additions & 66 deletions packages/use-request/src/__tests__/failed.test.js

This file was deleted.

112 changes: 109 additions & 3 deletions packages/use-request/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { fireEvent } from '@testing-library/react'
import useRequest from '../index';

describe('useRequest', () => {
Expand All @@ -24,14 +25,14 @@ describe('useRequest', () => {
} else {
resolve('success');
}
}, 2000);
}, 1000);
});

it('should be defined', () => {
expect(useRequest).toBeDefined();
});

const setUp = (service, options) => renderHook(() => useRequest(service, options))
const setUp = (service, options) => renderHook(o => useRequest(service, o || options))
let hook;
it('useRequest should auto run', async () => {
let successValue;
Expand Down Expand Up @@ -101,7 +102,8 @@ describe('useRequest', () => {
callback();
return request();
}, {
pollingInterval: 100
pollingInterval: 100,
pollingWhenHidden: true
});
});
expect(hook.result.current.loading).toEqual(true);
Expand Down Expand Up @@ -300,4 +302,108 @@ describe('useRequest', () => {
expect(hook.result.current.loading).toEqual(false);
hook.unmount();
})

it('useRequest refreshDeps should work', async () => {
act(() => {
hook = setUp(request, {
refreshDeps: [1]
});
});
expect(hook.result.current.loading).toEqual(true);
jest.runAllTimers();
await hook.waitForNextUpdate();
expect(hook.result.current.loading).toEqual(false);
hook.rerender({
refreshDeps: [2]
});
expect(hook.result.current.loading).toEqual(true);
jest.runAllTimers();
await hook.waitForNextUpdate();
expect(hook.result.current.loading).toEqual(false);
hook.unmount();
});

it('useRequest ready should work', async () => {
act(() => {
hook = setUp(request, {
ready: false
});
});
expect(hook.result.current.loading).toEqual(false);
hook.rerender({
ready: true
});
expect(hook.result.current.loading).toEqual(true);
hook.unmount();
})

it('useRequest initialData should work', async () => {
act(() => {
hook = setUp(request, {
initialData: 'hello'
});
});
expect(hook.result.current.loading).toEqual(true);
expect(hook.result.current.data).toEqual('hello');
jest.runAllTimers();
await hook.waitForNextUpdate();

expect(hook.result.current.data).toEqual('success');
hook.unmount();
})

it('useRequest formatResult should work', async () => {
let formarParams = '';
act(() => {
hook = setUp(request, {
formatResult: p => {
formarParams = p;
return 'hello';
}
});
});
expect(hook.result.current.loading).toEqual(true);
jest.runAllTimers();
await hook.waitForNextUpdate();
expect(hook.result.current.loading).toEqual(false);
expect(formarParams).toEqual('success');
expect(hook.result.current.data).toEqual('hello');
hook.unmount();
})

it('useRequest defaultParams should work', async () => {
act(() => {
hook = setUp(request, {
defaultParams: [1, 2, 3]
});
});
expect(hook.result.current.loading).toEqual(true);
jest.runAllTimers();
await hook.waitForNextUpdate();
expect(hook.result.current.params).toEqual([1, 2, 3]);
hook.unmount();
})

it('useRequest refreshOnWindowFocus&focusTimespan should work', async () => {
act(() => {
hook = setUp(request, {
refreshOnWindowFocus: true,
focusTimespan: 5000
});
});
expect(hook.result.current.loading).toEqual(true);
jest.advanceTimersByTime(1001);
await hook.waitForNextUpdate();
expect(hook.result.current.loading).toEqual(false);
fireEvent.focus(window);
expect(hook.result.current.loading).toEqual(true);
jest.advanceTimersByTime(2000);
await hook.waitForNextUpdate();
expect(hook.result.current.loading).toEqual(false);

jest.advanceTimersByTime(3000);
fireEvent.focus(window);
expect(hook.result.current.loading).toEqual(true);
hook.unmount();
})
})
50 changes: 0 additions & 50 deletions packages/use-request/src/__tests__/normal.test.js

This file was deleted.

Loading

0 comments on commit 524c270

Please sign in to comment.