-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4697 from LiskHQ/4638-Reward-height
Implement Reward at height Query Hooks
- Loading branch information
Showing
9 changed files
with
84 additions
and
3 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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './mockRewardConstants'; | ||
export * from './mockRewardInflation'; | ||
export * from './mockRewardDefault'; |
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 @@ | ||
export const mockRewardDefault = { | ||
data: { | ||
defaultReward: '0', | ||
tokenID: '0000000000000000', | ||
}, | ||
meta: {}, | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './useRewardConstants'; | ||
export * from './useRewardInflation'; | ||
export * from './useRewardHeight'; | ||
|
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,31 @@ | ||
import { REWARD_DEFAULT } from 'src/const/queries'; | ||
import { | ||
API_VERSION, | ||
} from 'src/const/config'; | ||
import { useCustomQuery } from '@common/hooks'; | ||
|
||
/** | ||
* Creates a custom hook for reward height queries | ||
* | ||
* @param {object} configuration - the custom query configuration object | ||
* @param {object} configuration.config - the query config | ||
* @param {object} configuration.config.params - the query config params | ||
* @param {number} [configuration.config.params.height] - the query height | ||
* @param {string} configuration.options - the query options | ||
* | ||
* @returns the query object | ||
*/ | ||
|
||
export const useRewardHeight = ({ config: customConfig = {}, options } = { }) => { | ||
const config = { | ||
url: `/api/${API_VERSION}/reward/default`, | ||
method: 'get', | ||
event: 'get.reward.default', | ||
...customConfig, | ||
}; | ||
return useCustomQuery({ | ||
keys: [REWARD_DEFAULT], | ||
config, | ||
options, | ||
}); | ||
}; |
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,35 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { queryWrapper as wrapper } from 'src/utils/test/queryWrapper'; | ||
import { mockRewardDefault } from '@reward/__fixtures__'; | ||
import * as useCustomQuerySpy from '@common/hooks/useCustomQuery'; | ||
import { useCustomQuery } from '@common/hooks'; | ||
import { useRewardHeight } from './useRewardHeight'; | ||
|
||
jest.useRealTimers(); | ||
jest.spyOn(useCustomQuerySpy, 'useCustomQuery'); | ||
|
||
describe('useRewardHeight hook', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('fetching data correctly', async () => { | ||
const { result, waitFor } = renderHook(() => useRewardHeight(), { wrapper }); | ||
expect(result.current.isLoading).toBeTruthy(); | ||
await waitFor(() => result.current.isFetched); | ||
expect(result.current.isSuccess).toBeTruthy(); | ||
expect(result.current.data).toEqual(mockRewardDefault); | ||
}); | ||
|
||
it('should call useCustomQuery with height params', async () => { | ||
const params = { height: 2 }; | ||
renderHook(() => useRewardHeight({ config: { params } }), { wrapper }); | ||
expect(useCustomQuery).toBeCalledWith( | ||
expect.objectContaining({ | ||
config: expect.objectContaining({ | ||
params, | ||
}), | ||
}) | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import { rest } from 'msw'; | ||
import { API_VERSION } from 'src/const/config'; | ||
import { mockRewardConstants, mockRewardInflation } from 'src/modules/reward/__fixtures__'; | ||
import { mockRewardConstants, mockRewardInflation, mockRewardDefault } from '../__fixtures__'; | ||
|
||
export const rewardConstants = rest.get(`*/api/${API_VERSION}/reward/constants`, async (req, res, ctx) => res(ctx.delay(20), ctx.json(mockRewardConstants))); | ||
|
||
export const rewardInflation = rest.get(`*/api/${API_VERSION}/reward/inflation`, async (req, res, ctx) => res(ctx.delay(20), ctx.json(mockRewardInflation))); | ||
|
||
export const rewardDefault = rest.get(`*/api/${API_VERSION}/reward/default`, async (req, res, ctx) => | ||
res(ctx.delay(20), ctx.json(mockRewardDefault)) | ||
); |
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