-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(perf): improvements * refactor(score): improve readability * refactor(getBlockNum): simplify logic * test(utils): change tests for modified files
- Loading branch information
Showing
9 changed files
with
305 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
yarn lint-staged | ||
yarn test:unit |
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 |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { getVp } from './methods'; | ||
import snapshot from '@snapshot-labs/strategies'; | ||
import { getBlockNum } from './utils'; | ||
import * as redisModule from './redis'; | ||
|
||
jest.mock('@snapshot-labs/strategies'); | ||
jest.mock('./utils'); | ||
jest.mock('./redis', () => ({ | ||
__esModule: true, | ||
default: { | ||
hGetAll: jest.fn(), | ||
multi: jest.fn(() => ({ | ||
hSet: jest.fn(), | ||
exec: jest.fn() | ||
})), | ||
connect: jest.fn(), | ||
on: jest.fn() | ||
} | ||
})); | ||
|
||
const mockRedis = redisModule.default; | ||
|
||
describe('getVp function', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should set snapshot to "latest" if it is not a number', async () => { | ||
const expectedSnapshotNum = 'latest'; | ||
const params = { | ||
address: '0x123', | ||
network: '1', | ||
strategies: [], | ||
snapshot: 'not-a-number', | ||
space: 'testSpace' | ||
}; | ||
|
||
(snapshot.utils.getVp as jest.Mock).mockResolvedValue({ vp_state: 'pending', vp: 100 }); | ||
(getBlockNum as jest.Mock).mockResolvedValue(expectedSnapshotNum); | ||
|
||
// @ts-expect-error | ||
await getVp(params); | ||
|
||
expect(getBlockNum).toHaveBeenCalledWith(expectedSnapshotNum, params.network); | ||
expect(params.snapshot).toBe('latest'); | ||
}); | ||
|
||
it('should call getBlockNum if snapshot is not "latest"', async () => { | ||
const params = { | ||
address: '0x123', | ||
network: '1', | ||
strategies: [], | ||
snapshot: 1000, | ||
space: 'testSpace' | ||
}; | ||
|
||
(getBlockNum as jest.Mock).mockResolvedValue(900); | ||
|
||
await getVp(params); | ||
|
||
expect(getBlockNum).toHaveBeenCalledWith(1000, '1'); | ||
}); | ||
|
||
it('should throw an error for disabled networks or spaces', async () => { | ||
const params = { | ||
address: '0x123', | ||
network: '1319', | ||
strategies: [], | ||
snapshot: 12345, | ||
space: 'testSpace' | ||
}; | ||
|
||
try { | ||
await getVp(params); | ||
fail('Expected getVp to throw an error'); // This will fail the test if no error is thrown | ||
} catch (error) { | ||
expect(error).toMatch('something wrong with the strategies'); | ||
} | ||
}); | ||
|
||
it('should call snapshot.utils.getVp with correct parameters', async () => { | ||
const params = { | ||
address: '0x123', | ||
network: '1', | ||
strategies: [], | ||
snapshot: 12345, | ||
space: 'testSpace' | ||
}; | ||
|
||
(snapshot.utils.getVp as jest.Mock).mockResolvedValue({ vp_state: 'pending', vp: 100 }); | ||
(getBlockNum as jest.Mock).mockResolvedValue(params.snapshot); | ||
|
||
await getVp(params); | ||
|
||
expect(snapshot.utils.getVp).toHaveBeenCalledWith( | ||
'0x123', | ||
'1', | ||
[], | ||
12345, | ||
'testSpace', | ||
undefined | ||
); | ||
}); | ||
|
||
it('should use cache if conditions are met', async () => { | ||
const params = { | ||
address: '0x123', | ||
network: '1', | ||
strategies: [], | ||
snapshot: 1000, | ||
space: 'testSpace' | ||
}; | ||
|
||
(getBlockNum as jest.Mock).mockResolvedValue(900); | ||
mockRedis.hGetAll.mockResolvedValue({ vp_state: 'pending', vp: 100, vp_by_strategy: '{}' }); | ||
|
||
const result = await getVp(params); | ||
|
||
expect(mockRedis.hGetAll).toHaveBeenCalled(); | ||
expect(result.cache).toBe(true); | ||
expect(result.result.vp).toBe(100); | ||
}); | ||
|
||
it('should return correct result values', async () => { | ||
const params = { | ||
address: '0x123', | ||
network: '1', | ||
strategies: [], | ||
snapshot: 1000, | ||
space: 'testSpace' | ||
}; | ||
|
||
mockRedis.hGetAll.mockResolvedValue(); | ||
(snapshot.utils.getVp as jest.Mock).mockResolvedValue({ | ||
vp_state: 'pending', | ||
vp: 100, | ||
vp_by_strategy: '{}' | ||
}); | ||
(getBlockNum as jest.Mock).mockResolvedValue(params.snapshot); | ||
|
||
const result = await getVp(params); | ||
|
||
expect(result.cache).toBe(false); | ||
expect(result.result.vp).toBe(100); | ||
expect(result.result.vp_state).toBe('pending'); | ||
}); | ||
|
||
it('should handle the delegation parameter correctly', async () => { | ||
const params = { | ||
address: '0x123', | ||
network: '1', | ||
strategies: [], | ||
snapshot: 1000, | ||
space: 'testSpace', | ||
delegation: true | ||
}; | ||
|
||
(snapshot.utils.getVp as jest.Mock).mockResolvedValue({ vp_state: 'pending', vp: 100 }); | ||
(getBlockNum as jest.Mock).mockResolvedValue(params.snapshot); | ||
|
||
await getVp(params); | ||
|
||
expect(snapshot.utils.getVp).toHaveBeenCalledWith('0x123', '1', [], 1000, 'testSpace', 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
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.