Skip to content

Commit

Permalink
Correct mocking of export default
Browse files Browse the repository at this point in the history
  • Loading branch information
tim committed Jun 20, 2017
1 parent 42b4002 commit ede11da
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
2 changes: 0 additions & 2 deletions src/transaction/makeCreateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
60 changes: 25 additions & 35 deletions src/transaction/makeTransferTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@ 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(
unspentTransaction,
metadata,
outputs,
...fulfilledOutputs
) {
const inputs = fulfilledOutputs.map((outputIndex) => {
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
const transactionLink = {
'output': outputIndex,
'transaction_id': unspentTransaction.id,
}

return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)
})

const assetLink = {
'id': unspentTransaction.operation === 'CREATE' ? unspentTransaction.id
: unspentTransaction.asset.id
}

return ['TRANSFER', assetLink, metadata, outputs, inputs]
}

/**
* @public
* Generate a `TRANSFER` transaction holding the `asset`, `metadata`, and `outputs`, that fulfills
Expand All @@ -40,20 +14,36 @@ export function _makeTransferTransaction(
* 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
* @param {...number} OutputIndices 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
* 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
// - 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))
export default function makeTransferTransaction(
unspentTransaction,
metadata,
outputs,
...outputIndices
) {
const inputs = outputIndices.map((outputIndex) => {
const fulfilledOutput = unspentTransaction.outputs[outputIndex]
const transactionLink = {
'output': outputIndex,
'transaction_id': unspentTransaction.id,
}

return makeInputTemplate(fulfilledOutput.public_keys, transactionLink)
})

const assetLink = {
'id': unspentTransaction.operation === 'CREATE' ? unspentTransaction.id
: unspentTransaction.asset.id
}

return makeTransaction('TRANSFER', assetLink, metadata, outputs, inputs)
}
21 changes: 16 additions & 5 deletions test/transaction/test_transaction.js
Original file line number Diff line number Diff line change
@@ -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'


Expand Down Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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()
})

0 comments on commit ede11da

Please sign in to comment.