-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[NEW] Create useEndpointData for call endpoints using hooks #4342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
42ea915
create basic useEndpointData for call endpoints using hooks
dnlsilva 548ef9e
remove cache
dnlsilva 7800223
create base for useEndpointData test
dnlsilva 9f6c540
create basic useEndpointData for call endpoints using hooks
dnlsilva 24f056d
remove cache
dnlsilva 46655d4
create base for useEndpointData test
dnlsilva 189b02d
Merge branch 'use-endpoint-data' of github.com:RocketChat/Rocket.Chat…
dnlsilva 3f177c6
fix preset
dnlsilva 1a14ab6
update tests
dnlsilva 82a2ffe
change order
dnlsilva 791231c
create ErrorResult and add error to return
dnlsilva f3d85e1
update tests
dnlsilva 1a614bb
Merge branch 'develop' into use-endpoint-data
dnlsilva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,87 @@ | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { render, waitFor } from '@testing-library/react-native'; | ||
| import React from 'react'; | ||
| import { View, Text } from 'react-native'; | ||
|
|
||
| import { useEndpointData } from './useEndpointData'; | ||
| import sdk from '../services/sdk'; | ||
|
|
||
| const url = 'chat.getMessage'; | ||
|
|
||
| export const message = { | ||
| _id: '9tYkmJ67wMwmvQouD', | ||
| t: 'uj', | ||
| rid: 'GENERAL', | ||
| ts: '2022-07-05T19:34:30.146Z', | ||
| msg: 'xdani', | ||
| u: { | ||
| _id: 'ombax8oEZnE7N3Mtt', | ||
| username: 'xdani', | ||
| name: 'xdani' | ||
| }, | ||
| groupable: false, | ||
| _updatedAt: '2022-07-05T19:34:30.146Z' | ||
| }; | ||
|
|
||
| // mock sdk | ||
| jest.mock('../services/sdk', () => ({ | ||
| get: jest.fn(() => new Promise(resolve => setTimeout(() => resolve({ success: true, message }), 1000))) | ||
| })); | ||
|
|
||
| function Render() { | ||
| const { loading } = useEndpointData(url, { msgId: message._id }); | ||
| if (loading) { | ||
| return ( | ||
| <View> | ||
| <Text testID='loading'>loading</Text> | ||
| </View> | ||
| ); | ||
| } | ||
| return ( | ||
| <View> | ||
| <Text testID='load complete'>load complete</Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| describe('useFetch', () => { | ||
| it('should return data after fetch', async () => { | ||
| const { result, waitForNextUpdate } = renderHook(() => useEndpointData(url, { msgId: message._id })); | ||
| expect(result.current.loading).toEqual(true); | ||
| expect(result.current.result).toEqual(undefined); | ||
| await waitForNextUpdate(); | ||
| expect(result.current.loading).toEqual(false); | ||
| expect(result.current.result).toEqual({ success: true, message }); | ||
| }); | ||
|
|
||
| it('should component load correctly', async () => { | ||
| const renderComponent = render(<Render />); | ||
| const loading = await renderComponent.findByTestId('loading'); | ||
| expect(loading.props.children).toBe('loading'); | ||
| await waitFor( | ||
| () => { | ||
| expect(renderComponent.getByText('load complete')).toBeTruthy(); | ||
| }, | ||
| { timeout: 2000 } | ||
| ); | ||
| }); | ||
|
|
||
| it('should return error after fetch', async () => { | ||
| const spy = jest | ||
| .spyOn(sdk, 'get') | ||
| .mockImplementation( | ||
| jest.fn(() => new Promise(resolve => setTimeout(() => resolve({ success: false, error: null }), 1000))) | ||
| ); | ||
|
|
||
| const { result, waitForNextUpdate } = renderHook(() => useEndpointData(url, { msgId: message._id })); | ||
| expect(result.current.loading).toEqual(true); | ||
| expect(result.current.result).toEqual(undefined); | ||
| expect(result.current.error).toEqual(undefined); | ||
| await waitForNextUpdate(); | ||
| expect(result.current.loading).toEqual(false); | ||
| expect(result.current.result).toEqual(undefined); | ||
| expect(result.current.error).toEqual({ success: false, error: null }); | ||
|
|
||
| spy.mockRestore(); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,62 @@ | ||
| import isEqual from 'lodash/isEqual'; | ||
| import { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
|
||
| import { ErrorResult, MatchPathPattern, OperationParams, PathFor, ResultFor, Serialized } from '../../definitions/rest/helpers'; | ||
| import sdk from '../services/sdk'; | ||
|
|
||
| export const useEndpointData = <TPath extends PathFor<'GET'>>( | ||
| endpoint: TPath, | ||
| params: void extends OperationParams<'GET', MatchPathPattern<TPath>> | ||
| ? void | ||
| : Serialized<OperationParams<'GET', MatchPathPattern<TPath>>> = undefined as void extends OperationParams< | ||
| 'GET', | ||
| MatchPathPattern<TPath> | ||
| > | ||
| ? void | ||
| : Serialized<OperationParams<'GET', MatchPathPattern<TPath>>> | ||
| ): { | ||
| result: Serialized<ResultFor<'GET', MatchPathPattern<TPath>>> | undefined; | ||
| loading: boolean; | ||
| reload: Function; | ||
| error: ErrorResult | undefined; | ||
| } => { | ||
| const [loading, setLoading] = useState(true); | ||
| const [result, setResult] = useState<Serialized<ResultFor<'GET', MatchPathPattern<TPath>>> | undefined>(); | ||
| const [error, setError] = useState<ErrorResult | undefined>(); | ||
|
|
||
| const paramsRef = useRef(params); | ||
|
|
||
| if (!isEqual(paramsRef.current, params)) { | ||
| paramsRef.current = params; | ||
| } | ||
|
|
||
| const fetchData = useCallback(() => { | ||
| if (!endpoint) return; | ||
| setLoading(true); | ||
| sdk | ||
| .get(endpoint, params) | ||
| .then(e => { | ||
| setLoading(false); | ||
| if (e.success) { | ||
| setResult(e); | ||
| } else { | ||
| setError(e as ErrorResult); | ||
| } | ||
| }) | ||
| .catch((e: ErrorResult) => { | ||
| setLoading(false); | ||
| setError(e); | ||
| }); | ||
| }, [paramsRef.current]); | ||
|
|
||
| useEffect(() => { | ||
| fetchData(); | ||
| }, [fetchData]); | ||
|
|
||
| return { | ||
| result, | ||
| loading, | ||
| reload: fetchData, | ||
| error | ||
| }; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.