Changeset View
Changeset View
Standalone View
Standalone View
apps/ecash-herald/test/mocks/chronikMock.js
Show All 16 Lines | MockChronikClient: class { | ||||
self._url = `https://${url}`; | self._url = `https://${url}`; | ||||
self._wsUrl = `wss://${url}`; | self._wsUrl = `wss://${url}`; | ||||
// API call mock return objects | // API call mock return objects | ||||
// Can be set with self.setMock | // Can be set with self.setMock | ||||
self.mockedResponses = { | self.mockedResponses = { | ||||
block: {}, | block: {}, | ||||
txHistory: [], | txHistory: [], | ||||
tx: {}, | |||||
}; | }; | ||||
self.mockedMethods = { p2pkh: {}, p2sh: {} }; | self.mockedMethods = { p2pkh: {}, p2sh: {} }; | ||||
// API call mock functions | // API call mock functions | ||||
// Return assigned block mocks | // Return assigned block mocks | ||||
self.block = function (blockHashOrHeight) { | self.block = function (blockHashOrHeight) { | ||||
return self.mockedResponses.block[blockHashOrHeight]; | return self.mockedResponses.block[blockHashOrHeight]; | ||||
}; | }; | ||||
// Return assigned tx mocks | |||||
self.tx = async function (txid) { | |||||
const mockedTxResponse = self.mockedResponses.tx[txid]; | |||||
// If the user set this response to be an error, throw it | |||||
if (mockedTxResponse instanceof Error) { | |||||
throw mockedTxResponse; | |||||
} | |||||
return self.mockedResponses.tx[txid]; | |||||
}; | |||||
// Return assigned script mocks | // Return assigned script mocks | ||||
self.script = function (type, hash) { | self.script = function (type, hash) { | ||||
return self.mockedMethods[type][hash]; | return self.mockedMethods[type][hash]; | ||||
}; | }; | ||||
// Flags to check if methods have been called | // Flags to check if methods have been called | ||||
self.wsSubscribeCalled = false; | self.wsSubscribeCalled = false; | ||||
self.wsWaitForOpenCalled = false; | self.wsWaitForOpenCalled = false; | ||||
Show All 23 Lines | MockChronikClient: class { | ||||
}; | }; | ||||
return returnedWs; | return returnedWs; | ||||
} | } | ||||
}; | }; | ||||
// Allow user to set expected chronik call response | // Allow user to set expected chronik call response | ||||
self.setMock = function (call, options) { | self.setMock = function (call, options) { | ||||
// e.g. ('block', {input: '', output: ''}) | // e.g. ('block', {input: '', output: ''}) | ||||
// e.g. ('tx', {input: <txid>, output: {<expectedObj>}}) | |||||
const { input, output } = options; | const { input, output } = options; | ||||
self.mockedResponses[call][input] = output; | self.mockedResponses[call][input] = output; | ||||
}; | }; | ||||
// script calls need to be set differently | // script calls need to be set differently | ||||
self.setTxHistory = function (txHistory) { | self.setTxHistory = function (txHistory) { | ||||
self.mockedResponses.txHistory = txHistory; | self.mockedResponses.txHistory = txHistory; | ||||
}; | }; | ||||
// Allow users to set expected chronik script call responses | // Allow users to set expected chronik script call responses | ||||
Show All 30 Lines |