From 94a9b2177bb1460749e9fb01c1f65050aca57bf2 Mon Sep 17 00:00:00 2001 From: xiawpohr Date: Tue, 10 Sep 2024 09:37:21 +0800 Subject: [PATCH 1/5] test: add useReputationScore test --- .../useReputationScore.test.ts | 25 +++++++++++++++++++ .../useReputationScore/useReputationScore.ts | 6 ++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts diff --git a/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts b/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts new file mode 100644 index 00000000..8726bcc4 --- /dev/null +++ b/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts @@ -0,0 +1,25 @@ +import { wrapper } from '@/utils/test-helpers/wrapper' +import { renderHook, waitFor } from '@testing-library/react' +import { useReputationScore } from './useReputationScore' + +jest.mock('@/features/core/hooks/useUserState/useUserState', () => ({ + useUserState: () => ({ + userState: { + id: '1', + getData: jest.fn().mockResolvedValue([10, 5, 0, 0]) + }, + }), +})) + +describe('useReputationScore', () => { + it('should return the reputation score', async () => { + const { result } = renderHook(() => useReputationScore(), { + wrapper: wrapper, + }) + + await waitFor(() => expect(result.current.isSuccess).toBe(true)) + + expect(result.current.reputationScore).toEqual(5) + expect(result.current.isValidReputationScore).toBe(true) + }) +}) diff --git a/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.ts b/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.ts index a9bbaa54..edf8cca5 100644 --- a/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.ts +++ b/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.ts @@ -4,7 +4,8 @@ import { useQuery } from '@tanstack/react-query' export function useReputationScore() { const { userState } = useUserState() - const { data: reputationScore } = useQuery({ + + const reputationScoreQuery = useQuery({ queryKey: [QueryKeys.ReputationScore, userState?.id.toString()], queryFn: async () => { if (!userState) { @@ -15,10 +16,13 @@ export function useReputationScore() { }, }) + const { data: reputationScore, ...otherProps } = reputationScoreQuery + const isValidReputationScore = reputationScore !== undefined && reputationScore >= 0 return { + ...otherProps, reputationScore, isValidReputationScore, } From 31344381d59a31f1bbc98863e79d319269536fa4 Mon Sep 17 00:00:00 2001 From: xiawpohr Date: Tue, 10 Sep 2024 10:17:43 +0800 Subject: [PATCH 2/5] test: add useReportEpochKeyReputation test --- .../useBackgroundReputationClaim.ts | 20 ++--- .../useReportEpochKeyReputation.test.ts | 75 +++++++++++++++++++ ...tion.ts => useReportEpochKeyReputation.ts} | 8 +- .../frontend/src/features/reporting/index.ts | 7 +- 4 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts rename packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/{useReportEpochKeyRepuation.ts => useReportEpochKeyReputation.ts} (97%) diff --git a/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts b/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts index eac3e96e..8e99b15f 100644 --- a/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts +++ b/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts @@ -1,17 +1,17 @@ import { useUserState } from '@/features/core' -import { RepUserType } from '@/types/Report' -import { useCallback, useEffect } from 'react' import { useWaitForTransactionReport } from '@/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport' import { useReportAdjucatorsReputation } from '@/features/reporting/hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation' -import { useReportEpochKeyRepuation } from '@/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyRepuation' -import { isMyEpochKey } from '@/utils/helpers/epochKey' +import { useReportEpochKeyReputation } from '@/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation' import { isMyAdjudicateNullifier } from '@/features/reporting/utils/helpers' +import { RepUserType } from '@/types/Report' +import { isMyEpochKey } from '@/utils/helpers/epochKey' +import { useCallback, useEffect } from 'react' export function useBackgroundReputationClaim() { const { data: reports } = useWaitForTransactionReport() - const { mutateAsync: claimAdjucatorRepuation } = + const { mutateAsync: claimAdjucatorReputation } = useReportAdjucatorsReputation() - const { mutateAsync: claimEpochKeyRepuation } = useReportEpochKeyRepuation() + const { mutateAsync: claimEpochKeyReputation } = useReportEpochKeyReputation() const { userState } = useUserState() const processReports = useCallback(async () => { @@ -27,7 +27,7 @@ export function useBackgroundReputationClaim() { ) ) { if (!report.reportorClaimedRep) { - await claimEpochKeyRepuation({ + await claimEpochKeyReputation({ reportId: report.reportId, reportedEpochKey: BigInt(report.reportorEpochKey), reportedEpoch: report.reportEpoch, @@ -42,7 +42,7 @@ export function useBackgroundReputationClaim() { ) ) { if (!report.respondentClaimedRep) { - await claimEpochKeyRepuation({ + await claimEpochKeyReputation({ reportId: report.reportId, reportedEpochKey: BigInt(report.respondentEpochKey), reportedEpoch: report.object.epoch, @@ -59,10 +59,10 @@ export function useBackgroundReputationClaim() { ) && !adj.claimed, ) ) { - await claimAdjucatorRepuation(report.reportId) + await claimAdjucatorReputation(report.reportId) } } - }, [reports, userState, claimEpochKeyRepuation, claimAdjucatorRepuation]) + }, [reports, userState, claimEpochKeyReputation, claimAdjucatorReputation]) useEffect(() => { processReports() diff --git a/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts b/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts new file mode 100644 index 00000000..02839676 --- /dev/null +++ b/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts @@ -0,0 +1,75 @@ +import { SERVER } from '@/constants/config' +import { RepUserType } from '@/types/Report' +import { wrapper } from '@/utils/test-helpers/wrapper' +import { act, renderHook } from '@testing-library/react' +import nock from 'nock' +import { useReportEpochKeyReputation } from './useReportEpochKeyReputation' + +jest.mock('@/features/core/hooks/useWeb3Provider/useWeb3Provider', () => ({ + useWeb3Provider: () => ({ + getGuaranteedProvider: () => ({ + waitForTransaction: jest.fn(), + }), + }), +})) + +jest.mock('@/features/core/hooks/useUserState/useUserState', () => ({ + useUserState: () => ({ + getGuaranteedUserState: () => ({ + waitForSync: jest.fn(), + latestTransitionedEpoch: jest.fn().mockResolvedValue(1), + genEpochKeyProof: jest.fn().mockResolvedValue({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + epoch: 0, + epochKey: 'mocked_epockKey', + }), + genUserStateTransitionProof: jest.fn().mockResolvedValue({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + epoch: 0, + epochKey: 'mocked_epockKey', + }), + sync: { + calcCurrentEpoch: jest.fn().mockReturnValue(2), + }, + }), + }), +})) + +jest.mock('@/features/core/utils/genReportNonNullifierProof', () => ({ + genReportNonNullifierProof: async () => ({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + }), +})) + +describe('useReportEpochKeyReputation', () => { + afterEach(() => { + nock.restore() + }) + + it('should claim reputation with ReportEpochKeyProof', async () => { + const expectation = nock(SERVER) + .post('/api/transition') + .reply(200, { hash: '0xhash' }) + .post('/api/reputation/claim') + .reply(200, { message: { txHash: '0xhash'} }) + + const { result } = renderHook(() => useReportEpochKeyReputation(), { + wrapper: wrapper, + }) + + await act(async () => { + await result.current.mutateAsync({ + reportId: '1', + reportedEpochKey: BigInt('1'), + reportedEpoch: 1, + repUserType: RepUserType.POSTER, + }) + }) + + expect(result.current.error).toBeFalsy() + expectation.done() + }) +}) diff --git a/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyRepuation.ts b/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.ts similarity index 97% rename from packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyRepuation.ts rename to packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.ts index eade61b2..e90087fa 100644 --- a/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyRepuation.ts +++ b/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.ts @@ -1,4 +1,3 @@ -import { useMutation, useQueryClient } from '@tanstack/react-query' import { MutationKeys, QueryKeys } from '@/constants/queryKeys' import { useActionCount, @@ -7,11 +6,12 @@ import { useWeb3Provider, } from '@/features/core' import { genReportNonNullifierProof } from '@/features/core/utils/genReportNonNullifierProof' -import { getEpochKeyNonce } from '@/utils/helpers/getEpochKeyNonce' -import { relayClaimReputation } from '@/utils/api' import { RepUserType } from '@/types/Report' +import { relayClaimReputation } from '@/utils/api' +import { getEpochKeyNonce } from '@/utils/helpers/getEpochKeyNonce' +import { useMutation, useQueryClient } from '@tanstack/react-query' -export function useReportEpochKeyRepuation() { +export function useReportEpochKeyReputation() { const { stateTransition } = useUserStateTransition() const { getGuaranteedUserState } = useUserState() const actionCount = useActionCount() diff --git a/packages/frontend/src/features/reporting/index.ts b/packages/frontend/src/features/reporting/index.ts index 549fdabf..c80da4ee 100644 --- a/packages/frontend/src/features/reporting/index.ts +++ b/packages/frontend/src/features/reporting/index.ts @@ -3,11 +3,12 @@ export { default as AdjudicationNotification } from './components/AdjudicateNoti export { default as CheckIn } from './components/CheckIn/CheckIn' export { default as CheckInNotification } from './components/CheckInNotification/CheckInNotification' export { useAdjudicate } from './hooks/useAdjudicate/useAdjudicate' +export { useBackgroundReputationClaim } from './hooks/useBackgroundReputationClaim/useBackgroundReputationClaim' export { useCheckIn } from './hooks/useCheckIn/useCheckIn' export { useFetchReportCategories } from './hooks/useFetchReportCategories/useFetchReportCategories' export { useNotifyCheckIn } from './hooks/useNotifyCheckIn/useNotifyCheckIn' export { usePendingReports } from './hooks/usePendingReports/usePendingReports' -export { useReputationScore } from './hooks/useReputationScore/useReputationScore' -export { useBackgroundReputationClaim } from './hooks/useBackgroundReputationClaim/useBackgroundReputationClaim' export { useReportAdjucatorsReputation } from './hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation' -export { useReportEpochKeyRepuation } from './hooks/useReportEpochKeyReputation/useReportEpochKeyRepuation' +export { useReportEpochKeyReputation } from './hooks/useReportEpochKeyReputation/useReportEpochKeyReputation' +export { useReputationScore } from './hooks/useReputationScore/useReputationScore' + From fe0cf6ec68352b889fbdc4c96c5acf68351f817d Mon Sep 17 00:00:00 2001 From: xiawpohr Date: Tue, 10 Sep 2024 10:23:03 +0800 Subject: [PATCH 3/5] test: add useReportAdjudicatorReputation test --- .../useBackgroundReputationClaim.ts | 4 +- .../useReportAdjudicatorReputation.test.ts | 69 +++++++++++++++++++ .../useReportAdjudicatorReputation.ts} | 8 +-- .../frontend/src/features/reporting/index.ts | 2 +- 4 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts rename packages/frontend/src/features/reporting/hooks/{useReportAdjicatorsReputation/useReportAdjucatorsReputation.ts => useReportAdjudicatorReputation/useReportAdjudicatorReputation.ts} (97%) diff --git a/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts b/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts index 8e99b15f..15304873 100644 --- a/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts +++ b/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts @@ -1,6 +1,6 @@ import { useUserState } from '@/features/core' import { useWaitForTransactionReport } from '@/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport' -import { useReportAdjucatorsReputation } from '@/features/reporting/hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation' +import { useReportAdjudicatorReputation } from '@/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation' import { useReportEpochKeyReputation } from '@/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation' import { isMyAdjudicateNullifier } from '@/features/reporting/utils/helpers' import { RepUserType } from '@/types/Report' @@ -10,7 +10,7 @@ import { useCallback, useEffect } from 'react' export function useBackgroundReputationClaim() { const { data: reports } = useWaitForTransactionReport() const { mutateAsync: claimAdjucatorReputation } = - useReportAdjucatorsReputation() + useReportAdjudicatorReputation() const { mutateAsync: claimEpochKeyReputation } = useReportEpochKeyReputation() const { userState } = useUserState() diff --git a/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts b/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts new file mode 100644 index 00000000..af74932b --- /dev/null +++ b/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts @@ -0,0 +1,69 @@ +import { SERVER } from '@/constants/config' +import { wrapper } from '@/utils/test-helpers/wrapper' +import { act, renderHook } from '@testing-library/react' +import nock from 'nock' +import { useReportAdjudicatorReputation } from './useReportAdjudicatorReputation' + +jest.mock('@/features/core/hooks/useWeb3Provider/useWeb3Provider', () => ({ + useWeb3Provider: () => ({ + getGuaranteedProvider: () => ({ + waitForTransaction: jest.fn(), + }), + }), +})) + +jest.mock('@/features/core/hooks/useUserState/useUserState', () => ({ + useUserState: () => ({ + getGuaranteedUserState: () => ({ + waitForSync: jest.fn(), + latestTransitionedEpoch: jest.fn().mockResolvedValue(1), + genEpochKeyProof: jest.fn().mockResolvedValue({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + epoch: 0, + epochKey: 'mocked_epockKey', + }), + genUserStateTransitionProof: jest.fn().mockResolvedValue({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + epoch: 0, + epochKey: 'mocked_epockKey', + }), + sync: { + calcCurrentEpoch: jest.fn().mockReturnValue(2), + }, + }), + }), +})) + +jest.mock('@/features/core/utils/genReportNullifierProof', () => ({ + genReportNullifierProof: async () => ({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + }), +})) + +describe('useReportAdjudicatorReputation', () => { + afterEach(() => { + nock.restore() + }) + + it('should claim reputation with ReportEpochKeyProof', async () => { + const expectation = nock(SERVER) + .post('/api/transition') + .reply(200, { hash: '0xhash' }) + .post('/api/reputation/claim') + .reply(200, { message: { txHash: '0xhash'} }) + + const { result } = renderHook(() => useReportAdjudicatorReputation(), { + wrapper: wrapper, + }) + + await act(async () => { + await result.current.mutateAsync('1') + }) + + expect(result.current.error).toBeFalsy() + expectation.done() + }) +}) diff --git a/packages/frontend/src/features/reporting/hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation.ts b/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.ts similarity index 97% rename from packages/frontend/src/features/reporting/hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation.ts rename to packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.ts index df8127fd..4ca87321 100644 --- a/packages/frontend/src/features/reporting/hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation.ts +++ b/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.ts @@ -1,4 +1,3 @@ -import { useMutation, useQueryClient } from '@tanstack/react-query' import { MutationKeys, QueryKeys } from '@/constants/queryKeys' import { useActionCount, @@ -7,11 +6,12 @@ import { useWeb3Provider, } from '@/features/core' import { genReportNullifierProof } from '@/features/core/utils/genReportNullifierProof' -import { getEpochKeyNonce } from '@/utils/helpers/getEpochKeyNonce' -import { relayClaimReputation } from '@/utils/api' import { RepUserType } from '@/types/Report' +import { relayClaimReputation } from '@/utils/api' +import { getEpochKeyNonce } from '@/utils/helpers/getEpochKeyNonce' +import { useMutation, useQueryClient } from '@tanstack/react-query' -export function useReportAdjucatorsReputation() { +export function useReportAdjudicatorReputation() { const { stateTransition } = useUserStateTransition() const { getGuaranteedUserState } = useUserState() const actionCount = useActionCount() diff --git a/packages/frontend/src/features/reporting/index.ts b/packages/frontend/src/features/reporting/index.ts index c80da4ee..ff149ced 100644 --- a/packages/frontend/src/features/reporting/index.ts +++ b/packages/frontend/src/features/reporting/index.ts @@ -8,7 +8,7 @@ export { useCheckIn } from './hooks/useCheckIn/useCheckIn' export { useFetchReportCategories } from './hooks/useFetchReportCategories/useFetchReportCategories' export { useNotifyCheckIn } from './hooks/useNotifyCheckIn/useNotifyCheckIn' export { usePendingReports } from './hooks/usePendingReports/usePendingReports' -export { useReportAdjucatorsReputation } from './hooks/useReportAdjicatorsReputation/useReportAdjucatorsReputation' +export { useReportAdjudicatorReputation } from './hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation' export { useReportEpochKeyReputation } from './hooks/useReportEpochKeyReputation/useReportEpochKeyReputation' export { useReputationScore } from './hooks/useReputationScore/useReputationScore' From 287946958d12c51e07d081a518671539a6a391da Mon Sep 17 00:00:00 2001 From: xiawpohr Date: Tue, 10 Sep 2024 10:24:44 +0800 Subject: [PATCH 4/5] chore: lint fix --- .../useBackgroundReputationClaim.ts | 3 ++- .../useReportAdjudicatorReputation.test.ts | 2 +- .../useReportEpochKeyReputation.test.ts | 2 +- .../hooks/useReputationScore/useReputationScore.test.ts | 2 +- packages/frontend/src/features/reporting/index.ts | 1 - 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts b/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts index 15304873..8ec1000d 100644 --- a/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts +++ b/packages/frontend/src/features/reporting/hooks/useBackgroundReputationClaim/useBackgroundReputationClaim.ts @@ -11,7 +11,8 @@ export function useBackgroundReputationClaim() { const { data: reports } = useWaitForTransactionReport() const { mutateAsync: claimAdjucatorReputation } = useReportAdjudicatorReputation() - const { mutateAsync: claimEpochKeyReputation } = useReportEpochKeyReputation() + const { mutateAsync: claimEpochKeyReputation } = + useReportEpochKeyReputation() const { userState } = useUserState() const processReports = useCallback(async () => { diff --git a/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts b/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts index af74932b..efbbb8c2 100644 --- a/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts +++ b/packages/frontend/src/features/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.ts @@ -53,7 +53,7 @@ describe('useReportAdjudicatorReputation', () => { .post('/api/transition') .reply(200, { hash: '0xhash' }) .post('/api/reputation/claim') - .reply(200, { message: { txHash: '0xhash'} }) + .reply(200, { message: { txHash: '0xhash' } }) const { result } = renderHook(() => useReportAdjudicatorReputation(), { wrapper: wrapper, diff --git a/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts b/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts index 02839676..c2a8159c 100644 --- a/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts +++ b/packages/frontend/src/features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.ts @@ -54,7 +54,7 @@ describe('useReportEpochKeyReputation', () => { .post('/api/transition') .reply(200, { hash: '0xhash' }) .post('/api/reputation/claim') - .reply(200, { message: { txHash: '0xhash'} }) + .reply(200, { message: { txHash: '0xhash' } }) const { result } = renderHook(() => useReportEpochKeyReputation(), { wrapper: wrapper, diff --git a/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts b/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts index 8726bcc4..169f8d0d 100644 --- a/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts +++ b/packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.ts @@ -6,7 +6,7 @@ jest.mock('@/features/core/hooks/useUserState/useUserState', () => ({ useUserState: () => ({ userState: { id: '1', - getData: jest.fn().mockResolvedValue([10, 5, 0, 0]) + getData: jest.fn().mockResolvedValue([10, 5, 0, 0]), }, }), })) diff --git a/packages/frontend/src/features/reporting/index.ts b/packages/frontend/src/features/reporting/index.ts index ff149ced..17ef9e5c 100644 --- a/packages/frontend/src/features/reporting/index.ts +++ b/packages/frontend/src/features/reporting/index.ts @@ -11,4 +11,3 @@ export { usePendingReports } from './hooks/usePendingReports/usePendingReports' export { useReportAdjudicatorReputation } from './hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation' export { useReportEpochKeyReputation } from './hooks/useReportEpochKeyReputation/useReportEpochKeyReputation' export { useReputationScore } from './hooks/useReputationScore/useReputationScore' - From 1852acd9d8dc9bf66f5a23cbe6b82e17a9d9d873 Mon Sep 17 00:00:00 2001 From: xiawpohr Date: Tue, 10 Sep 2024 10:45:12 +0800 Subject: [PATCH 5/5] test: add useWaitForTransactionReport test --- .../useWaitForTransactionReport.test.ts | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 packages/frontend/src/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport.test.ts diff --git a/packages/frontend/src/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport.test.ts b/packages/frontend/src/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport.test.ts new file mode 100644 index 00000000..8e6b0915 --- /dev/null +++ b/packages/frontend/src/features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport.test.ts @@ -0,0 +1,71 @@ +import { SERVER } from '@/constants/config' +import { wrapper } from '@/utils/test-helpers/wrapper' +import { renderHook, waitFor } from '@testing-library/react' +import nock from 'nock' +import { useWaitForTransactionReport } from './useWaitForTransactionReport' + +jest.mock('@/features/core/hooks/useUserState/useUserState', () => ({ + useUserState: () => ({ + getGuaranteedUserState: () => ({ + id: { + secret: '0x123', + }, + genProveReputationProof: jest.fn().mockResolvedValue({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + epoch: 0, + epochKey: 'mocked_epockKey', + }), + genEpochKeyLiteProof: jest.fn().mockResolvedValue({ + publicSignals: 'mocked_signals', + proof: 'mocked_proof', + epoch: 0, + epochKey: 'mocked_epockKey', + }), + }), + }), +})) + +jest.mock('@/features/core/hooks/useEpoch/useEpoch', () => ({ + useEpoch: () => ({ + currentEpoch: 2, + }), +})) + +describe('useWaitForTransactionReport', () => { + afterEach(() => { + nock.restore() + }) + + it('should fetch pending reports', async () => { + const reports = [ + { + reportId: '1', + objectId: '1', + category: 2, + reason: '偷偷置入性廣告,不OK餒!', + reportorEpochKey: '', + respondentEpochKey: '', + adjudicatorsNullifier: [], + createdAt: '2022-01-01T00:00:00.000Z', + updatedAt: '2022-01-01T00:00:00.000Z', + }, + ] + + const expectation = nock(SERVER) + .get( + '/api/report?status=1&publicSignals=%22mocked_signals%22&proof=%22mocked_proof%22', + ) + .reply(200, reports) + + const { result } = renderHook(useWaitForTransactionReport, { + wrapper, + }) + + await waitFor(() => expect(result.current.isSuccess).toBe(true)) + + expect(result.current.data).toEqual(reports) + + expectation.done() + }) +})