1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-12-28 15:47:50 +01:00

Correct mocking of export default

This commit is contained in:
tim 2017-06-20 14:55:58 +02:00
parent 42b400253b
commit ede11da4c7
3 changed files with 42 additions and 43 deletions

View File

@ -21,8 +21,6 @@ import makeTransaction from './makeTransaction'
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
// TODO: `outputs` should throw or include output in array if no array was
// passed
export default function makeCreateTransaction(asset, metadata, outputs, ...issuers) {
const assetDefinition = {
'data': asset || null,

View File

@ -2,15 +2,35 @@ import makeInputTemplate from './makeInputTemplate'
import makeTransaction from './makeTransaction'
// TODO: Can we remove `export` here somehow, but still be able to import the
// function for tests?
export function _makeTransferTransaction(
/**
* @public
* Generate a `TRANSFER` transaction holding the `asset`, `metadata`, and `outputs`, that fulfills
* the `fulfilledOutputs` of `unspentTransaction`.
* @param {object} unspentTransaction Previous Transaction you have control over (i.e. can fulfill
* its Output Condition)
* @param {object} metadata Metadata for the Transaction
* @param {object[]} outputs Array of Output objects to add to the Transaction.
* Think of these as the recipients of the asset after the transaction.
* For `TRANSFER` Transactions, this should usually just be a list of
* Outputs wrapping Ed25519 Conditions generated from the public keys of
* the recipients.
* @param {...number} OutputIndices Indices of the Outputs in `unspentTransaction` that this
* Transaction fulfills.
* Note that listed public keys listed must be used (and in
* the same order) to sign the Transaction
* (`signTransaction()`).
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
// TODO:
// - Make `metadata` optional argument
export default function makeTransferTransaction(
unspentTransaction,
metadata,
outputs,
...fulfilledOutputs
...outputIndices
) {
const inputs = fulfilledOutputs.map((outputIndex) => {
const inputs = outputIndices.map((outputIndex) => {
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
const transactionLink = {
'output': outputIndex,
@ -25,35 +45,5 @@ export function _makeTransferTransaction(
: unspentTransaction.asset.id
}
return ['TRANSFER', assetLink, metadata, outputs, inputs]
}
/**
* @public
* Generate a `TRANSFER` transaction holding the `asset`, `metadata`, and `outputs`, that fulfills
* the `fulfilledOutputs` of `unspentTransaction`.
* @param {object} unspentTransaction Previous Transaction you have control over (i.e. can fulfill
* its Output Condition)
* @param {object} metadata Metadata for the Transaction
* @param {object[]} outputs Array of Output objects to add to the Transaction.
* Think of these as the recipients of the asset after the transaction.
* For `TRANSFER` Transactions, this should usually just be a list of
* Outputs wrapping Ed25519 Conditions generated from the public keys of
* the recipients.
* @param {...number} fulfilledOutputs Indices of the Outputs in `unspentTransaction` that this
* Transaction fulfills.
* Note that the public keys listed in the fulfilled Outputs
* must be used (and in the same order) to sign the Transaction
* (`signTransaction()`).
* @returns {object} Unsigned transaction -- make sure to call signTransaction() on it before
* sending it off!
*/
// TODO:
// - Make `metadata` optional argument
// - Rename `fulfilledOutputs`, e.g. inputs
// TODO: `outputs` should throw or include output in array if no array was
// passed
export default function makeTransferTransaction(...args) {
return makeTransaction(..._makeTransferTransaction(...args))
return makeTransaction('TRANSFER', assetLink, metadata, outputs, inputs)
}

View File

@ -1,6 +1,8 @@
import test from 'ava'
import sinon from 'sinon'
import { Transaction, Ed25519Keypair } from '../../src'
import { _makeTransferTransaction } from '../../src/transaction/makeTransferTransaction'
import * as makeTransaction from '../../src/transaction/makeTransaction' // eslint-disable-line
import makeInputTemplate from '../../src/transaction/makeInputTemplate'
@ -78,7 +80,9 @@ test('makeOutput throws TypeError with incorrect amount type', t => {
test('Create TRANSFER transaction based on CREATE transaction', t => {
const testTx = _makeTransferTransaction(
sinon.spy(makeTransaction, 'default')
Transaction.makeTransferTransaction(
createTx,
metaDataMessage,
[aliceOutput],
@ -95,12 +99,18 @@ test('Create TRANSFER transaction based on CREATE transaction', t => {
)]
]
t.deepEqual(testTx, expected)
// NOTE: `src/transaction/makeTransaction` is `export default`, hence we
// can only mock `makeTransaction.default` with a hack:
// See: https://stackoverflow.com/a/33676328/1263876
t.truthy(makeTransaction.default.calledWith(...expected))
makeTransaction.default.restore()
})
test('Create TRANSFER transaction based on TRANSFER transaction', t => {
const testTx = _makeTransferTransaction(
sinon.spy(makeTransaction, 'default')
Transaction.makeTransferTransaction(
transferTx,
metaDataMessage,
[aliceOutput],
@ -117,5 +127,6 @@ test('Create TRANSFER transaction based on TRANSFER transaction', t => {
)]
]
t.deepEqual(testTx, expected)
t.truthy(makeTransaction.default.calledWith(...expected))
makeTransaction.default.restore()
})