-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move /hooks package into /react pkg
- Loading branch information
Showing
29 changed files
with
705 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@data-client/react': patch | ||
--- | ||
|
||
Add [useLoading()](https://dataclient.io/docs/api/useLoading), [useDebounce()](https://dataclient.io/docs/api/useDebounce), [useCancelling()](https://dataclient.io/docs/api/useCancelling) | ||
|
||
These are taken from the hooks package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@data-client/hooks': minor | ||
--- | ||
|
||
**This repository has been deprecated and is no longer actively maintained.** | ||
|
||
All hooks moved to [@data-client/react](https://www.npmjs.com/package/@data-client/react) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { ArticleResource } from '__tests__/new'; | ||
import nock from 'nock'; | ||
|
||
import { renderHook, act } from '../../../../test'; | ||
import useCancelling from '../useCancelling'; | ||
|
||
describe('useCancelling()', () => { | ||
const payload = { | ||
id: '6', | ||
title: 'lala', | ||
}; | ||
const payload2 = { | ||
id: '7', | ||
title: 'second one', | ||
}; | ||
beforeAll(() => { | ||
jest.useFakeTimers({ | ||
legacyFakeTimers: true, | ||
}); | ||
const mynock = nock(/.*/) | ||
.persist() | ||
.defaultReplyHeaders({ | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Headers': 'Access-Token', | ||
'Content-Type': 'application/json', | ||
}) | ||
.options(/.*/) | ||
.reply(200); | ||
|
||
mynock | ||
.get(`/article/${payload.id}`) | ||
.delay(2000) | ||
.reply(200, payload) | ||
.get(`/article/${payload2.id}`) | ||
.delay(2000) | ||
.reply(200, payload2); | ||
}); | ||
afterAll(() => { | ||
jest.useRealTimers(); | ||
nock.cleanAll(); | ||
}); | ||
|
||
it('should abort when props change and resolve when kept the same', async () => { | ||
const { result, rerender } = renderHook( | ||
({ id }: { id: string }) => { | ||
return useCancelling(ArticleResource.get, { id }); | ||
}, | ||
{ initialProps: { id: '6' } }, | ||
); | ||
const firstendpoint = result.current; | ||
const ogPromise = result.current({ id: '6' }); | ||
jest.advanceTimersByTime(10); | ||
act(() => rerender({ id: '7' })); | ||
expect(result.current).not.toBe(firstendpoint); | ||
expect(ogPromise).rejects.toMatchInlineSnapshot(`[AbortError: Aborted]`); | ||
const nextPromise = result.current({ id: '7' }); | ||
jest.advanceTimersByTime(2000); | ||
await expect(nextPromise).resolves.toMatchInlineSnapshot(` | ||
{ | ||
"id": "7", | ||
"title": "second one", | ||
} | ||
`); | ||
act(() => rerender({ id: '7' })); | ||
}); | ||
|
||
it('should remain === if params does not change', () => { | ||
const { result, rerender } = renderHook( | ||
({ id }: { id: string }) => { | ||
return useCancelling(ArticleResource.get, { id }); | ||
}, | ||
{ initialProps: { id: '6' } }, | ||
); | ||
let lastendpoint = result.current; | ||
act(() => rerender({ id: '6' })); | ||
expect(result.current).toBe(lastendpoint); | ||
lastendpoint = result.current; | ||
act(() => rerender({ id: '6' })); | ||
expect(result.current).toBe(lastendpoint); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { renderHook, act } from '../../../../test'; | ||
import useDebounce from '../useDebounce'; | ||
|
||
describe('useDebounce()', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should not update until delay has passed', () => { | ||
const { result, rerender } = renderHook( | ||
({ value }: { value: string }) => { | ||
return useDebounce(value, 100); | ||
}, | ||
{ initialProps: { value: 'initial' } }, | ||
); | ||
expect(result.current).toBe('initial'); | ||
jest.advanceTimersByTime(10); | ||
rerender({ value: 'next' }); | ||
rerender({ value: 'third' }); | ||
expect(result.current).toBe('initial'); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
expect(result.current).toBe('third'); | ||
}); | ||
|
||
it('should never update when updatable is false', () => { | ||
const { result, rerender } = renderHook( | ||
({ value, updatable }: { value: string; updatable: boolean }) => { | ||
return useDebounce(value, 100, updatable); | ||
}, | ||
{ initialProps: { value: 'initial', updatable: false } }, | ||
); | ||
expect(result.current).toBe('initial'); | ||
jest.advanceTimersByTime(10); | ||
rerender({ value: 'next', updatable: false }); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
expect(result.current).toBe('initial'); | ||
rerender({ value: 'third', updatable: true }); | ||
expect(result.current).toBe('initial'); | ||
jest.advanceTimersByTime(10); | ||
expect(result.current).toBe('initial'); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
expect(result.current).toBe('third'); | ||
}); | ||
}); |
Oops, something went wrong.