-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '4.9.0' into enhancement/6201-replace-plugins-configuration
- Loading branch information
Showing
11 changed files
with
178 additions
and
41 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
68 changes: 68 additions & 0 deletions
68
plugins/main/public/components/common/hooks/use-service.test.tsx
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,68 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useService } from './use-service'; | ||
|
||
const successfulService = async (_params?: any) => { | ||
return Promise.resolve('test data'); | ||
}; | ||
const failingService = async (_params?: any) => { | ||
return Promise.reject('Error occurred'); | ||
}; | ||
const successfulRefreshService = async (_params?: any) => { | ||
return Promise.resolve(Date.now()); | ||
}; | ||
|
||
const successfulServiceWithParams = jest.fn().mockResolvedValue('test data'); | ||
|
||
describe('useService hook', () => { | ||
it('should return data and success state when service call is successful', async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useService(successfulService), | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.data).toEqual('test data'); | ||
expect(result.current.isLoading).toEqual(false); | ||
expect(result.current.isSuccess).toEqual(true); | ||
expect(result.current.isError).toEqual(false); | ||
expect(result.current.error).toBeUndefined(); | ||
}); | ||
|
||
it('should return error state when service call fails', async () => { | ||
const { result, waitForNextUpdate } = renderHook(() => | ||
useService(failingService), | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.data).toBeUndefined(); | ||
expect(result.current.isLoading).toEqual(false); | ||
expect(result.current.isSuccess).toEqual(false); | ||
expect(result.current.isError).toEqual(true); | ||
expect(result.current.error).toEqual('Error occurred'); | ||
}); | ||
|
||
it('should fetch data again with new data when refresh value changes', async () => { | ||
const { result, rerender, waitForNextUpdate } = renderHook( | ||
({ refresh }) => useService(successfulRefreshService, undefined, refresh), | ||
{ initialProps: { refresh: 0 } }, | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
const data = result.current.data; | ||
|
||
rerender({ refresh: 1 }); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current.data).not.toEqual(data); | ||
}); | ||
|
||
it('should call the service with provided parameters', async () => { | ||
const params = { id: 123456 }; | ||
|
||
renderHook(() => useService(successfulServiceWithParams, params)); | ||
|
||
expect(successfulServiceWithParams).toHaveBeenCalledWith(params); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
plugins/main/public/components/common/hooks/use-service.ts
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,40 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
interface useServiceResponse<T> { | ||
data: T | undefined; | ||
isLoading: boolean; | ||
isSuccess: boolean; | ||
isError: boolean; | ||
error: any; | ||
} | ||
|
||
export function useService<T>( | ||
service: (params?: any | undefined) => Promise<T>, | ||
params?: any | undefined, | ||
refresh?: number | undefined, | ||
): useServiceResponse<T> { | ||
const [data, setData] = useState<T | undefined>(undefined); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
const [isSuccess, setIsSuccess] = useState<boolean>(false); | ||
const [isError, setIsError] = useState<boolean>(false); | ||
const [error, setError] = useState<any>(undefined); | ||
useEffect(() => { | ||
const handleService = async () => { | ||
setIsLoading(true); | ||
try { | ||
const response = await service(params); | ||
setData(response); | ||
setIsSuccess(true); | ||
} catch (error) { | ||
setIsError(true); | ||
setError(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
handleService(); | ||
}, [refresh]); | ||
|
||
return { data, isLoading, isSuccess, isError, error }; | ||
} |
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
Oops, something went wrong.