Skip to content

Commit

Permalink
fix: correctly check for promise in useLoadData (#39)
Browse files Browse the repository at this point in the history
* fix: correctly check for promise in useLoadData

* simplification

* simplification

* add unit test to ensure useLoadData handles thenable objects as promises

* improved comment
  • Loading branch information
NielsJPeschel authored Jan 31, 2025
1 parent 093606b commit cfb6f28
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions hooks/useLoadData/useLoadData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,13 @@ describe('useLoadData', () => {
expect(getSuccess).toHaveBeenCalledTimes(2);
expect(getSuccess).toHaveBeenCalledWith('b');
});

it('should treat a thenable object as a Promise', async () => {
const getThenableSuccess = jest.fn(() => ({then: (resolve: any) => resolve(successResult)}));

const {result} = renderHook(() => useLoadData(getThenableSuccess));
expect(result.current.isInProgress).toBe(true);
await waitFor(() => expect(result.current.isInProgress).toBe(false));
expect(result.current.result).toBe(successResult);
});
});
13 changes: 11 additions & 2 deletions hooks/useLoadData/useLoadData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useEffect, useState, useMemo} from 'react';
import {ApiResponse, RetryResponse, ApiResponseBase, OptionalDependency, DependencyBase} from '../../types';
import {ApiResponse, RetryResponse, ApiResponseBase, OptionalDependency, DependencyBase, Promisable} from '../../types';

import {FetchData, NotUndefined} from './types';

Expand Down Expand Up @@ -30,6 +30,15 @@ function unboxApiResponse<T>(arg: ApiResponse<T> | T): T {
}
}

function isPromise<T>(promisable: Promisable<T>): promisable is Promise<T> {
/*
Simply checking promisable instanceof Promise is not sufficient.
Certain environments do not use native promises. Checking for promisable
to be thenable is a more comprehensive and conclusive test.
*/
return promisable && typeof promisable === 'object' && 'then' in promisable && typeof promisable.then === 'function';
}

export interface LoadDataConfig {
fetchWhenDepsChange?: boolean;
maxRetryCount?: number;
Expand Down Expand Up @@ -184,7 +193,7 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
}
}, [counter, localFetchWhenDepsChange]);

const nonPromiseResult = initialPromise.res instanceof Promise ? undefined : initialPromise.res;
const nonPromiseResult = isPromise(initialPromise.res) ? undefined : initialPromise.res;
const initialData = data || nonPromiseResult;

// Initialize our pending data to one of three possible states:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@optum/react-hooks",
"version": "1.0.4",
"version": "1.0.5-next.1",
"description": "A reusable set of React hooks",
"repository": "https://github.com/Optum/react-hooks",
"license": "Apache 2.0",
Expand Down

0 comments on commit cfb6f28

Please sign in to comment.