-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add test case for handlers (#13)
- Loading branch information
1 parent
968df5f
commit 722d051
Showing
4 changed files
with
454 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import ApplicationCreated from '../../src/handlers/ApplicationCreated'; | ||
import { block, ctx, logs } from '../stubs/params'; | ||
|
||
vi.mock('../../src/model/', async (importOriginal) => { | ||
const actualMods = await importOriginal; | ||
const Application = vi.fn(); | ||
const ApplicationFactory = vi.fn(); | ||
return { | ||
...actualMods!, | ||
Application, | ||
ApplicationFactory, | ||
}; | ||
}); | ||
|
||
describe('ApplicationCreated', () => { | ||
let applicationCreated: ApplicationCreated; | ||
const mockFactoryStorage = new Map(); | ||
const mockApplicationStorage = new Map(); | ||
beforeEach(() => { | ||
applicationCreated = new ApplicationCreated( | ||
mockFactoryStorage, | ||
mockApplicationStorage, | ||
); | ||
mockFactoryStorage.clear(); | ||
mockApplicationStorage.clear(); | ||
vi.clearAllMocks(); | ||
}); | ||
describe('handle', async () => { | ||
test('call with correct params', async () => { | ||
vi.spyOn(applicationCreated, 'handle'); | ||
applicationCreated.handle(logs[0], block, ctx); | ||
expect(applicationCreated.handle).toHaveBeenCalledWith( | ||
logs[0], | ||
block, | ||
ctx, | ||
); | ||
}); | ||
test('wrong contract address', async () => { | ||
await applicationCreated.handle(logs[0], block, ctx); | ||
expect(mockFactoryStorage.size).toBe(0); | ||
expect(mockApplicationStorage.size).toBe(0); | ||
}); | ||
test('correct contract address', async () => { | ||
await applicationCreated.handle(logs[1], block, ctx); | ||
const applicationId = '0x0be010fa7e70d74fa8b6729fe1ae268787298f54'; | ||
expect(mockFactoryStorage.size).toBe(1); | ||
expect(mockApplicationStorage.size).toBe(1); | ||
expect(mockFactoryStorage.has(logs[1].address)).toBe(true); | ||
expect(mockApplicationStorage.has(applicationId)).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { dataSlice, getUint } from 'ethers'; | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { Contract } from '../../src/abi/ERC20'; | ||
import InputAdded from '../../src/handlers/InputAdded'; | ||
import { Erc20Deposit, Token } from '../../src/model'; | ||
import { block, ctx, input, logs } from '../stubs/params'; | ||
|
||
vi.mock('../../src/abi/ERC20', async (importOriginal) => { | ||
const actualMods = await importOriginal; | ||
const Contract = vi.fn(); | ||
Contract.prototype.name = vi.fn(); | ||
Contract.prototype.symbol = vi.fn(); | ||
Contract.prototype.decimals = vi.fn(); | ||
return { | ||
...actualMods!, | ||
Contract, | ||
}; | ||
}); | ||
vi.mock('../../src/model/', async (importOriginal) => { | ||
const actualMods = await importOriginal; | ||
const Token = vi.fn(); | ||
const Erc20Deposit = vi.fn(); | ||
const Application = vi.fn(); | ||
const Input = vi.fn(); | ||
return { | ||
...actualMods!, | ||
Application, | ||
Token, | ||
Erc20Deposit, | ||
Input, | ||
}; | ||
}); | ||
const tokenAddress = dataSlice(input.payload, 1, 21).toLowerCase(); // 20 bytes for address | ||
const from = dataSlice(input.payload, 21, 41).toLowerCase(); // 20 bytes for address | ||
const amount = getUint(dataSlice(input.payload, 41, 73)); // 32 bytes for uint256 | ||
describe('InputAdded', () => { | ||
let inputAdded: InputAdded; | ||
let erc20; | ||
const mockTokenStorage = new Map(); | ||
const mockDepositStorage = new Map(); | ||
const mockInputStorage = new Map(); | ||
const mockApplicationStorage = new Map(); | ||
beforeEach(() => { | ||
inputAdded = new InputAdded( | ||
mockTokenStorage, | ||
mockDepositStorage, | ||
mockApplicationStorage, | ||
mockInputStorage, | ||
); | ||
erc20 = new Contract(ctx, block.header, tokenAddress); | ||
mockTokenStorage.clear(); | ||
mockDepositStorage.clear(); | ||
mockApplicationStorage.clear(); | ||
mockInputStorage.clear(); | ||
vi.clearAllMocks(); | ||
}); | ||
describe('handlePayload(log)', async () => { | ||
test('call with the correct params', async () => { | ||
vi.spyOn(inputAdded, 'handlePayload'); | ||
inputAdded.handlePayload(input, block, ctx); | ||
expect(inputAdded.handlePayload).toHaveBeenCalledWith( | ||
input, | ||
block, | ||
ctx, | ||
); | ||
}); | ||
test('call the ERC20 module', async () => { | ||
await inputAdded.handlePayload(input, block, ctx); | ||
expect(erc20.name).toBeCalledTimes(1); | ||
expect(erc20.symbol).toBeCalledTimes(1); | ||
expect(erc20.decimals).toBeCalledTimes(1); | ||
}); | ||
test('return the correct deposit value', async () => { | ||
const name = 'SimpleERC20'; | ||
const symbol = 'SIM20'; | ||
const decimals = 18; | ||
const token = new Token({ | ||
id: tokenAddress, | ||
name, | ||
symbol, | ||
decimals, | ||
}); | ||
const deposit = new Erc20Deposit({ | ||
id: input.id, | ||
amount, | ||
from, | ||
token, | ||
}); | ||
erc20.name.mockResolvedValueOnce('SimpleERC20'); | ||
erc20.symbol.mockResolvedValue('SIM20'); | ||
erc20.decimals.mockResolvedValue(18); | ||
const handlePayload = await inputAdded.handlePayload( | ||
input, | ||
block, | ||
ctx, | ||
); | ||
expect(handlePayload).toStrictEqual(deposit); | ||
}); | ||
test('msgSender is not the correct ERC20PortalAddress', async () => { | ||
input.msgSender = '0x42985af528673AF020811c339Bf62497160Fe087'; | ||
const handlePayload = await inputAdded.handlePayload( | ||
input, | ||
block, | ||
ctx, | ||
); | ||
expect(handlePayload).toBe(undefined); | ||
}); | ||
}); | ||
describe('handle', async () => { | ||
test('call with the correct params', async () => { | ||
vi.spyOn(inputAdded, 'handle'); | ||
inputAdded.handle(logs[0], block, ctx); | ||
expect(inputAdded.handle).toBeCalledWith(logs[0], block, ctx); | ||
}); | ||
test('wrong contract address', async () => { | ||
await inputAdded.handle(logs[1], block, ctx); | ||
expect(mockInputStorage.size).toBe(0); | ||
expect(mockApplicationStorage.size).toBe(0); | ||
expect(mockDepositStorage.size).toBe(0); | ||
}); | ||
test('correct contract address', async () => { | ||
await inputAdded.handle(logs[0], block, ctx); | ||
expect(mockApplicationStorage.size).toBe(1); | ||
expect(mockInputStorage.size).toBe(1); | ||
}); | ||
test('Erc20Deposit Stored', async () => { | ||
const name = 'SimpleERC20'; | ||
const symbol = 'SIM20'; | ||
const decimals = 18; | ||
const token = new Token({ | ||
id: tokenAddress, | ||
name, | ||
symbol, | ||
decimals, | ||
}); | ||
erc20.name.mockResolvedValueOnce('SimpleERC20'); | ||
erc20.symbol.mockResolvedValue('SIM20'); | ||
erc20.decimals.mockResolvedValue(18); | ||
const deposit = new Erc20Deposit({ | ||
id: input.id, | ||
amount, | ||
from, | ||
token, | ||
}); | ||
vi.spyOn(inputAdded, 'handlePayload').mockImplementation( | ||
(input, block, ctx) => { | ||
return new Promise((resolve) => { | ||
resolve(deposit); | ||
}); | ||
}, | ||
); | ||
await inputAdded.handle(logs[0], block, ctx); | ||
expect(mockDepositStorage.size).toBe(1); | ||
}); | ||
}); | ||
}); |
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,54 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
|
||
import OwnerShipTransferred from '../../src/handlers/OwnershipTransferred'; | ||
import { block, ctx, logs } from '../stubs/params'; | ||
|
||
vi.mock('../../src/model/', async (importOriginal) => { | ||
const actualMods = await importOriginal; | ||
const Application = vi.fn(); | ||
return { | ||
...actualMods!, | ||
Application, | ||
}; | ||
}); | ||
|
||
describe('ApplicationCreated', () => { | ||
let ownershipTransferred: OwnerShipTransferred; | ||
const mockApplicationStorage = new Map(); | ||
beforeEach(() => { | ||
ownershipTransferred = new OwnerShipTransferred(mockApplicationStorage); | ||
mockApplicationStorage.clear(); | ||
vi.clearAllMocks(); | ||
}); | ||
describe('handle', async () => { | ||
test('call with correct params', async () => { | ||
vi.spyOn(ownershipTransferred, 'handle'); | ||
ownershipTransferred.handle(logs[2], block, ctx); | ||
expect(ownershipTransferred.handle).toHaveBeenCalledWith( | ||
logs[2], | ||
block, | ||
ctx, | ||
); | ||
}); | ||
test('wrong contract address', async () => { | ||
await ownershipTransferred.handle(logs[0], block, ctx); | ||
expect(mockApplicationStorage.size).toBe(0); | ||
}); | ||
test('Ownership Transferred', async () => { | ||
const mockApplicationStorage2 = new Map(); | ||
const appId = logs[2].transaction.to; | ||
mockApplicationStorage2.set(appId, { | ||
id: appId, | ||
owner: '0xf05d57a5bed2d1b529c56001fc5810cc9afc0335', | ||
factory: { | ||
id: '0x7122cd1221c20892234186facfe8615e6743ab02', | ||
}, | ||
}); | ||
const ownerMock = new OwnerShipTransferred(mockApplicationStorage2); | ||
await ownerMock.handle(logs[2], block, ctx); | ||
expect(mockApplicationStorage2.get(appId).owner).not.toBe( | ||
'0xf05d57a5bed2d1b529c56001fc5810cc9afc0335', | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.