1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 04:46:10 +01:00
metamask-extension/test/stub/provider.js
2018-01-14 14:00:59 -08:00

38 lines
946 B
JavaScript

const JsonRpcEngine = require('json-rpc-engine')
const scaffoldMiddleware = require('eth-json-rpc-middleware/scaffold')
module.exports = {
createEngineForTestData,
providerFromEngine,
scaffoldMiddleware,
createEthJsQueryStub,
createStubedProvider,
}
function createEngineForTestData () {
return new JsonRpcEngine()
}
function providerFromEngine (engine) {
const provider = { sendAsync: engine.handle.bind(engine) }
return provider
}
function createEthJsQueryStub (stubProvider) {
return new Proxy({}, {
get: (obj, method) => {
return (...params) => {
return new Promise((resolve, reject) => {
stubProvider.sendAsync({ method: `eth_${method}`, params }, (err, ress) => resolve(ress.result))
})
}
},
})
}
function createStubedProvider (resultStub) {
const engine = createEngineForTestData()
engine.push(scaffoldMiddleware(resultStub))
return providerFromEngine(engine)
}