Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mock web3.js using jest
/* Mock web3 as __tests__/__mocks__/web3.js */
// const web3 = jest.genMockFromModule('web3')
/* Mock web3-eth-contract */
let mockWeb3EthContract = function() {}
function __setMockContract(mock) {
mockWeb3EthContract = mock
}
let blockNumber = 0
function __setBlockNumber(number) {
blockNumber = number
}
var eth = {
Contract: jest.fn().mockImplementation(() => mockWeb3EthContract),
getBlockNumber: () => blockNumber
}
var web3 = function(provider) {
return {
provider: provider,
eth: eth
}
}
web3.providers = {
HttpProvider: function() {
return {
send: (payload, cb) => {cb(null, "{}")}
}
}
}
web3.__setMockContract = __setMockContract
web3.__setBlockNumber = __setBlockNumber
module.exports = web3
const Faucet = require('../src/model/faucet')
const Web3 = require('web3')
const CONTRACT_ADDRESS = '0xadDf5bC8b45571D94e2FD46Bfb81f8dD6D6f9FA0'
const FROM_ADDRESS = '0x259de638f0d6d79dfa084a810c1356d4d575b62e'
const mockGetPastEvents = jest.fn().mockImplementation(() => {
return [{
returnValues: {
from: '0xe62d87b603dc9b8a5535d41aa464f583cc476aa8',
to: '0x259de638f0d6d79dfa084a810c1356d4d575b62e',
value: 100
}
}]
})
const mockTransfer = jest.fn().mockImplementation(() => {
return {
send: jest.fn().mockImplementation(() => {
return true
})
}
})
const mockContract = {
getPastEvents: mockGetPastEvents,
methods: {
transfer: mockTransfer
}
}
Web3.__setMockContract(mockContract)
describe('Faucet', function () {
describe('init', function () {
it('should be return instance', async () => {
const ins = new Faucet(CONTRACT_ADDRESS, FROM_ADDRESS)
expect(ins).toBeDefined()
})
})
describe('test functions', function () {
beforeEach(() => {
mockTransfer.mockClear()
})
it('getPastEventOption when block 100', async () => {
const ins = new Faucet(CONTRACT_ADDRESS, FROM_ADDRESS)
const option = await ins.getPastEventOption()
expect(option).toEqual({fromBlock: 0, toBlock: 'latest'})
})
it('getPastEventOption when block 65536', async () => {
Web3.__setBlockNumber(65535)
const ins = new Faucet(CONTRACT_ADDRESS, FROM_ADDRESS)
const option = await ins.getPastEventOption()
expect(option).toEqual({fromBlock: (65535 - (24 * 60 * 4)), toBlock: 'latest'})
})
it('isAlreadyWitdrawed', async () => {
const ins = new Faucet(CONTRACT_ADDRESS, FROM_ADDRESS)
await ins.loadLatestDayTransfers()
const result = await ins.isAlreadyWitdrawed('0x259de638f0d6d79dfa084a810c1356d4d575b62e')
expect(result).toBeTruthy()
})
it('sendToken', async () => {
const ins = new Faucet(CONTRACT_ADDRESS, FROM_ADDRESS)
const toAddress = '0xe62d87b603dc9b8a5535d41aa464f583cc476aa8'
await ins.sendToken(toAddress)
// check that send just 100 token.
expect(mockTransfer).toHaveBeenCalledWith(toAddress, 100)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment