Skip to content

Commit

Permalink
Merge pull request #4697 from LiskHQ/4638-Reward-height
Browse files Browse the repository at this point in the history
Implement Reward at height Query Hooks
  • Loading branch information
Masoud Soroush authored Jan 5, 2023
2 parents f7825ba + 63f12ad commit 12236da
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const config = {
'@blockchainApplication': resolve('./src/modules/blockchainApplication'),
'@block': resolve('./src/modules/block'),
'@bookmark': resolve('./src/modules/bookmark'),
'@reward': resolve('./src/modules/reward'),
'@search': resolve('./src/modules/search'),
'@common': resolve('./src/modules/common'),
'@legacy': resolve('./src/modules/legacy'),
'@message': resolve('./src/modules/message'),
'@auth': resolve('./src/modules/auth'),
'@wallet': resolve('./src/modules/wallet'),
'@pos': resolve('./src/modules/pos'),
'@reward': resolve('./src/modules/reward'),
'@network': resolve('./src/modules/network'),
'@settings': resolve('./src/modules/settings'),
'@token': resolve('./src/modules/token'),
Expand Down
1 change: 1 addition & 0 deletions src/const/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export const BLOCKCHAIN_APPS_META_TOKENS = 'GET_BLOCKCHAIN_APPS_META_TOKENS';
export const POS_CONSTANTS = 'GET_POS_CONSTANTS';
export const POS_REWARDS_LOCKED = 'GET_POS_REWARDS_LOCKED';
export const POS_REWARDS_CLAIMABLE = 'GET_POS_REWARDS_CLAIMABLE';
export const REWARD_DEFAULT = 'GET_REWARD_DEFAULT';
export const REWARD_INFLATION = 'GET_REWARD_INFLATION';
export const REWARD_CONSTANTS = 'GET_REWARD_CONSTANTS';
1 change: 1 addition & 0 deletions src/modules/reward/__fixtures__/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './mockRewardConstants';
export * from './mockRewardInflation';
export * from './mockRewardDefault';
7 changes: 7 additions & 0 deletions src/modules/reward/__fixtures__/mockRewardDefault.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const mockRewardDefault = {
data: {
defaultReward: '0',
tokenID: '0000000000000000',
},
meta: {},
};
2 changes: 2 additions & 0 deletions src/modules/reward/hooks/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './useRewardConstants';
export * from './useRewardInflation';
export * from './useRewardHeight';

31 changes: 31 additions & 0 deletions src/modules/reward/hooks/queries/useRewardHeight.js
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,
});
};
35 changes: 35 additions & 0 deletions src/modules/reward/hooks/queries/useRewardHeight.test.js
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,
}),
})
);
});
});
6 changes: 5 additions & 1 deletion src/modules/reward/mocks/index.js
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))
);
2 changes: 1 addition & 1 deletion src/service/mock/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as network from '@network/mocks';
import * as tokens from '@token/fungible/mocks';
import * as transactions from '@transaction/mocks';
import * as posValidators from '@pos/validator/mocks';
import * as posRewards from '@pos/reward/mocks';
import * as reward from '@reward/mocks';
import * as posRewards from '@pos/reward/mocks';
import * as blockchainApplicationExplore from '@blockchainApplication/explore/mocks';
import * as blockchainApplicationManage from '@blockchainApplication/manage/mocks';

Expand Down

0 comments on commit 12236da

Please sign in to comment.