-
Notifications
You must be signed in to change notification settings - Fork 44
test: unit tests for get-account-info #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { address } from '@solana/kit' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { getAccountInfo } from '../src/get-account-info.ts' | ||
| import type { SolanaClient } from '../src/solana-client.ts' | ||
|
|
||
| describe('get-account-info', () => { | ||
| describe('expected behavior', () => { | ||
| it('should call rpc.getAccountInfo with the correct address', async () => { | ||
| // ARRANGE | ||
| expect.assertions(2) | ||
| const testAddress = 'So11111111111111111111111111111111111111112' | ||
| const AccountInfo = { | ||
| value: { | ||
| data: new Uint8Array([1, 2, 3]), | ||
| executable: false, | ||
| lamports: 1000000n, | ||
| owner: address('11111111111111111111111111111111'), | ||
| rentEpoch: 0n, | ||
| }, | ||
| } | ||
| const mockSend = vi.fn().mockResolvedValue(AccountInfo) | ||
| const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) | ||
| const mockClient = { | ||
| rpc: { | ||
| getAccountInfo: mockGetAccountInfo, | ||
| }, | ||
| } as unknown as SolanaClient | ||
|
|
||
| // ACT | ||
| const result = await getAccountInfo(mockClient, { address: testAddress }) | ||
|
|
||
| // ASSERT | ||
| expect(mockGetAccountInfo).toHaveBeenCalledWith(address(testAddress)) | ||
| expect(result).toEqual(AccountInfo) | ||
| }) | ||
|
|
||
| it('should handle account with no data', async () => { | ||
| // ARRANGE | ||
| expect.assertions(1) | ||
| const testAddress = 'So11111111111111111111111111111111111111112' | ||
| const AccountInfo = { | ||
| value: null, | ||
| } | ||
| const mockSend = vi.fn().mockResolvedValue(AccountInfo) | ||
| const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) | ||
| const mockClient = { | ||
| rpc: { | ||
| getAccountInfo: mockGetAccountInfo, | ||
| }, | ||
| } as unknown as SolanaClient | ||
|
|
||
| // ACT | ||
| const result = await getAccountInfo(mockClient, { address: testAddress }) | ||
|
|
||
| // ASSERT | ||
| expect(result).toEqual(AccountInfo) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not very useful because you are just testing the mock. What else can we assert here? |
||
| }) | ||
|
|
||
| it('should handle account with executable program', async () => { | ||
| // ARRANGE | ||
| expect.assertions(2) | ||
| const testAddress = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' | ||
| const AccountInfo = { | ||
| value: { | ||
| data: new Uint8Array([1, 2, 3, 4, 5]), | ||
| executable: true, | ||
| lamports: 5000000n, | ||
| owner: address('11111111111111111111111111111111'), | ||
| rentEpoch: 100n, | ||
| }, | ||
| } | ||
| const mockSend = vi.fn().mockResolvedValue(AccountInfo) | ||
| const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) | ||
| const mockClient = { | ||
| rpc: { | ||
| getAccountInfo: mockGetAccountInfo, | ||
| }, | ||
| } as unknown as SolanaClient | ||
|
|
||
| // ACT | ||
| const result = await getAccountInfo(mockClient, { address: testAddress }) | ||
|
|
||
| // ASSERT | ||
| expect(mockGetAccountInfo).toHaveBeenCalledWith(address(testAddress)) | ||
| expect(result.value?.executable).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('unexpected behavior', () => { | ||
| beforeEach(() => { | ||
| vi.spyOn(console, 'log').mockImplementation(() => {}) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it('should throw an error when rpc call fails', async () => { | ||
| // ARRANGE | ||
| expect.assertions(1) | ||
| const testAddress = 'So11111111111111111111111111111111111111112' | ||
| const mockError = new Error('RPC request failed') | ||
| const mockSend = vi.fn().mockRejectedValue(mockError) | ||
| const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) | ||
| const mockClient = { | ||
| rpc: { | ||
| getAccountInfo: mockGetAccountInfo, | ||
| }, | ||
| } as unknown as SolanaClient | ||
|
|
||
| // ACT & ASSERT | ||
| await expect(getAccountInfo(mockClient, { address: testAddress })).rejects.toThrow('RPC request failed') | ||
| }) | ||
|
|
||
| it('should throw an error with invalid address format', () => { | ||
| // ARRANGE | ||
| expect.assertions(1) | ||
| const invalidAddress = 'invalid-address' | ||
| const mockSend = vi.fn() | ||
| const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) | ||
| const mockClient = { | ||
| rpc: { | ||
| getAccountInfo: mockGetAccountInfo, | ||
| }, | ||
| } as unknown as SolanaClient | ||
|
|
||
| // ACT & ASSERT | ||
| expect(() => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if |
||
| getAccountInfo(mockClient, { | ||
| address: invalidAddress, | ||
| }), | ||
| ).toThrow() | ||
| }) | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should mock only what's needed but create the actual client https://vitest.dev/guide/mocking.html