|
| 1 | +import test from 'ava' |
| 2 | +import sinon from 'sinon' |
| 3 | + |
| 4 | +import { Transaction, Ed25519Keypair } from '../../src' |
| 5 | +import * as makeTransaction from '../../src/transaction/makeTransaction' // eslint-disable-line |
| 6 | +import makeInputTemplate from '../../src/transaction/makeInputTemplate' |
| 7 | + |
| 8 | + |
| 9 | +// TODO: Find out if ava has something like conftest, if so put this there. |
| 10 | +const alice = new Ed25519Keypair() |
| 11 | +const aliceCondition = Transaction.makeEd25519Condition(alice.publicKey) |
| 12 | +const aliceOutput = Transaction.makeOutput(aliceCondition) |
| 13 | +const assetMessage = { assetMessage: 'assetMessage' } |
| 14 | +const metaDataMessage = { metaDataMessage: 'metaDataMessage' } |
| 15 | +const createTx = Transaction.makeCreateTransaction( |
| 16 | + assetMessage, |
| 17 | + metaDataMessage, |
| 18 | + [aliceOutput], |
| 19 | + alice.publicKey |
| 20 | +) |
| 21 | +const transferTx = Transaction.makeTransferTransaction( |
| 22 | + createTx, |
| 23 | + metaDataMessage, |
| 24 | + [aliceOutput], |
| 25 | + 0 |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +test('Create valid output with default amount', t => { |
| 30 | + const condition = { |
| 31 | + details: { |
| 32 | + public_key: 'abc' |
| 33 | + } |
| 34 | + } |
| 35 | + const expected = { |
| 36 | + amount: '1', |
| 37 | + condition, |
| 38 | + public_keys: ['abc'] |
| 39 | + } |
| 40 | + const res = Transaction.makeOutput(condition) |
| 41 | + t.deepEqual(res, expected) |
| 42 | +}) |
| 43 | + |
| 44 | + |
| 45 | +test('Create valid output with custom amount', t => { |
| 46 | + const condition = { |
| 47 | + details: { |
| 48 | + public_key: 'abc' |
| 49 | + } |
| 50 | + } |
| 51 | + const customAmount = '1337' |
| 52 | + const expected = { |
| 53 | + amount: customAmount, |
| 54 | + condition, |
| 55 | + public_keys: ['abc'] |
| 56 | + } |
| 57 | + const res = Transaction.makeOutput(condition, customAmount) |
| 58 | + t.deepEqual(res, expected) |
| 59 | +}) |
| 60 | + |
| 61 | +test('Pass condition not based on public_keys to makeOutput', t => { |
| 62 | + const condition = { |
| 63 | + details: { |
| 64 | + idea: 'just pretend this is e.g. a hashlock' |
| 65 | + } |
| 66 | + } |
| 67 | + const expected = { |
| 68 | + amount: '1', |
| 69 | + condition, |
| 70 | + public_keys: [] |
| 71 | + } |
| 72 | + const res = Transaction.makeOutput(condition) |
| 73 | + t.deepEqual(res, expected) |
| 74 | +}) |
| 75 | + |
| 76 | + |
| 77 | +test('makeOutput throws TypeError with incorrect amount type', t => { |
| 78 | + t.throws(() => Transaction.makeOutput({}, 1337), TypeError) |
| 79 | +}) |
| 80 | + |
| 81 | + |
| 82 | +test('Create TRANSFER transaction based on CREATE transaction', t => { |
| 83 | + sinon.spy(makeTransaction, 'default') |
| 84 | + |
| 85 | + Transaction.makeTransferTransaction( |
| 86 | + createTx, |
| 87 | + metaDataMessage, |
| 88 | + [aliceOutput], |
| 89 | + 0 |
| 90 | + ) |
| 91 | + const expected = [ |
| 92 | + 'TRANSFER', |
| 93 | + { id: createTx.id }, |
| 94 | + metaDataMessage, |
| 95 | + [aliceOutput], |
| 96 | + [makeInputTemplate( |
| 97 | + [alice.publicKey], |
| 98 | + { output: 0, transaction_id: createTx.id } |
| 99 | + )] |
| 100 | + ] |
| 101 | + |
| 102 | + // NOTE: `src/transaction/makeTransaction` is `export default`, hence we |
| 103 | + // can only mock `makeTransaction.default` with a hack: |
| 104 | + // See: https://stackoverflow.com/a/33676328/1263876 |
| 105 | + t.truthy(makeTransaction.default.calledWith(...expected)) |
| 106 | + makeTransaction.default.restore() |
| 107 | +}) |
| 108 | + |
| 109 | + |
| 110 | +test('Create TRANSFER transaction based on TRANSFER transaction', t => { |
| 111 | + sinon.spy(makeTransaction, 'default') |
| 112 | + |
| 113 | + Transaction.makeTransferTransaction( |
| 114 | + transferTx, |
| 115 | + metaDataMessage, |
| 116 | + [aliceOutput], |
| 117 | + 0 |
| 118 | + ) |
| 119 | + const expected = [ |
| 120 | + 'TRANSFER', |
| 121 | + { id: transferTx.asset.id }, |
| 122 | + metaDataMessage, |
| 123 | + [aliceOutput], |
| 124 | + [makeInputTemplate( |
| 125 | + [alice.publicKey], |
| 126 | + { output: 0, transaction_id: transferTx.id } |
| 127 | + )] |
| 128 | + ] |
| 129 | + |
| 130 | + t.truthy(makeTransaction.default.calledWith(...expected)) |
| 131 | + makeTransaction.default.restore() |
| 132 | +}) |
0 commit comments