Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/lib/hooks/useEndpointData.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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';

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', () => {
Comment thread
dnlsilva marked this conversation as resolved.
it('should return data after fetch - hook', 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 return data after fetch - Component', 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 }
);
});
});
66 changes: 66 additions & 0 deletions app/lib/hooks/useEndpointData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import isEqual from 'lodash/isEqual';
import { useCallback, useEffect, useRef, useState } from 'react';

import { MatchPathPattern, OperationParams, PathFor, ResultFor, Serialized } from '../../definitions/rest/helpers';
import { showErrorAlert } from '../methods/helpers';
import sdk from '../services/sdk';

type TError = {
success: boolean;
error: string;
errorType: string;
details: {
method: string;
};
};

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 } => {
const [loading, setLoading] = useState(true);
const [result, setResult] = useState<Serialized<ResultFor<'GET', MatchPathPattern<TPath>>> | 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 {
// handle error
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
}
})
.catch((e: TError) => {
// handle error
setLoading(false);
showErrorAlert(e.error);
Comment thread
dnlsilva marked this conversation as resolved.
Outdated
});
}, [paramsRef.current]);

useEffect(() => {
fetchData();
}, [fetchData]);

return {
result,
loading,
reload: fetchData
};
};
2 changes: 1 addition & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const jestRN = require('react-native/jest-preset');
const jestRN = require('@testing-library/react-native/jest-preset/index.js');
const jestExpo = require('jest-expo/jest-preset.js');

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
"@storybook/addon-storyshots": "5.3.21",
"@storybook/react-native": "5.3.25",
"@testing-library/jest-native": "^4.0.4",
"@testing-library/react-hooks": "^8.0.1",
"@testing-library/react-native": "^9.0.0",
"@types/bytebuffer": "^5.0.43",
"@types/ejson": "^2.1.3",
Expand Down
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4542,6 +4542,14 @@
ramda "^0.26.1"
redent "^2.0.0"

"@testing-library/react-hooks@^8.0.1":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==
dependencies:
"@babel/runtime" "^7.12.5"
react-error-boundary "^3.1.0"

"@testing-library/react-native@^9.0.0":
version "9.0.0"
resolved "https://registry.yarnpkg.com/@testing-library/react-native/-/react-native-9.0.0.tgz#e9c63411e93d2e8e70d744b12aeb78c58025c5fc"
Expand Down Expand Up @@ -15033,6 +15041,13 @@ react-draggable@^4.0.3:
classnames "^2.2.5"
prop-types "^15.6.0"

react-error-boundary@^3.1.0:
version "3.1.4"
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
dependencies:
"@babel/runtime" "^7.12.5"

react-error-overlay@^6.0.3:
version "6.0.9"
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.9.tgz#3c743010c9359608c375ecd6bc76f35d93995b0a"
Expand Down