-
Notifications
You must be signed in to change notification settings - Fork 12
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 #492 from social-tw/feature/add-reputation-hooks-t…
…ests Feature/add reputation hooks tests
- Loading branch information
Showing
9 changed files
with
269 additions
and
24 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
71 changes: 71 additions & 0 deletions
71
.../features/reporting/hooks/useGetWaitForTransactReport/useWaitForTransactionReport.test.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,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() | ||
}) | ||
}) |
69 changes: 69 additions & 0 deletions
69
...res/reporting/hooks/useReportAdjudicatorReputation/useReportAdjudicatorReputation.test.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,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() | ||
}) | ||
}) |
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
75 changes: 75 additions & 0 deletions
75
.../features/reporting/hooks/useReportEpochKeyReputation/useReportEpochKeyReputation.test.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,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() | ||
}) | ||
}) |
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
25 changes: 25 additions & 0 deletions
25
packages/frontend/src/features/reporting/hooks/useReputationScore/useReputationScore.test.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,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) | ||
}) | ||
}) |
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