From e9b11a430b8f447e9c6f21c1b639d150976f98cf Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 10 May 2017 17:26:51 -0700 Subject: Add an attempt at a unit test for reproducing #1407 --- test/unit/components/pending-tx-test.js | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 test/unit/components/pending-tx-test.js (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js new file mode 100644 index 000000000..e0f02a5bb --- /dev/null +++ b/test/unit/components/pending-tx-test.js @@ -0,0 +1,61 @@ +var assert = require('assert') +var PendingTx = require('../../../ui/app/components/pending-tx') + +describe('PendingTx', function () { + let pendingTxComponent + + const identities = { + '0xfdea65c8e26263f6d9a1b5de9555d2931a33b826': { + name: 'Main Account 1', + balance: '0x00000000000000056bc75e2d63100000', + }, + } + + const gasPrice = '0x4A817C800' // 20 Gwei + const txData = { + 'id':5021615666270214, + 'time':1494458763011, + 'status':'unapproved', + 'metamaskNetworkId':'1494442339676', + 'txParams':{ + 'from':'0xfdea65c8e26263f6d9a1b5de9555d2931a33b826', + 'to':'0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb', + 'value':'0xde0b6b3a7640000', + gasPrice, + 'gas':'0x7b0c'}, + 'gasLimitSpecified':false, + 'estimatedGas':'0x5208', + } + + + it('should use updated values when edited.', function (done) { + + const props = { + identities, + accounts: identities, + txData, + sendTransaction: (txMeta, event) => { + assert.notEqual(txMeta.txParams.gasPrice, gasPrice, 'gas price should change') + done() + }, + } + + pendingTxComponent = new PendingTx(props) + + const noop = () => {} + + pendingTxComponent.componentDidMount = () => { + + const newGasPrice = '0x451456' + pendingTxComponent.gasPriceChanged(newGasPrice) + + setTimeout(() => { + pendingTxComponent.onSubmit({ preventDefault: noop }) + }, 20) + } + + pendingTxComponent.props = props + pendingTxComponent.render() + }) + +}) -- cgit v1.2.3 From 113f7d67f1973d1468b95517895701af8ca16f95 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 11 May 2017 14:29:44 -0700 Subject: Fix tests add logs --- test/unit/components/pending-tx-test.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index e0f02a5bb..3e26fc6f5 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -1,7 +1,7 @@ var assert = require('assert') var PendingTx = require('../../../ui/app/components/pending-tx') -describe('PendingTx', function () { +describe.only('PendingTx', function () { let pendingTxComponent const identities = { @@ -44,17 +44,21 @@ describe('PendingTx', function () { const noop = () => {} - pendingTxComponent.componentDidMount = () => { + setTimeout(() => { + console.log('component mounted') const newGasPrice = '0x451456' pendingTxComponent.gasPriceChanged(newGasPrice) setTimeout(() => { + console.log('hitting submit') pendingTxComponent.onSubmit({ preventDefault: noop }) }, 20) - } + }, 200) + console.log('calling render') pendingTxComponent.props = props + pendingTxComponent.checkValidity = () => { return true } pendingTxComponent.render() }) -- cgit v1.2.3 From 16005ebd3a7db5c48f9d81d5a1c77ace7ff92958 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 11 May 2017 15:28:33 -0700 Subject: Got test failing --- test/unit/components/pending-tx-test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index 3e26fc6f5..b798865cc 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -30,12 +30,15 @@ describe.only('PendingTx', function () { it('should use updated values when edited.', function (done) { + const newGasPrice = '0x451456' + const props = { identities, accounts: identities, txData, sendTransaction: (txMeta, event) => { assert.notEqual(txMeta.txParams.gasPrice, gasPrice, 'gas price should change') + assert.equal(txMeta.txParams.gasPrice, newGasPrice, 'gas price assigned.') done() }, } @@ -47,7 +50,6 @@ describe.only('PendingTx', function () { setTimeout(() => { console.log('component mounted') - const newGasPrice = '0x451456' pendingTxComponent.gasPriceChanged(newGasPrice) setTimeout(() => { -- cgit v1.2.3 From 60746a985997693612af0c8b43aac95b2a6e56e6 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 11 May 2017 17:09:23 -0700 Subject: Use react test utils to start composing test --- test/unit/components/pending-tx-test.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index b798865cc..caaf66b49 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -1,5 +1,13 @@ var assert = require('assert') +const h = require('react-hyperscript') var PendingTx = require('../../../ui/app/components/pending-tx') +const createReactFactory = require('create-react-factory').createReactFactory +const React = require('react') +console.dir(createReactFactory) +const shallow = require('enzyme').shallow +const Factory = createReactFactory(PendingTx) +const ReactTestUtils = require('react-addons-test-utils') +const renderer = ReactTestUtils.createRenderer(); describe.only('PendingTx', function () { let pendingTxComponent @@ -43,14 +51,21 @@ describe.only('PendingTx', function () { }, } - pendingTxComponent = new PendingTx(props) + const pendingTxComponent = h(PendingTx, props) + renderer.render(pendingTxComponent) + console.dir(pendingTxComponent) const noop = () => {} setTimeout(() => { - console.log('component mounted') + console.log('timeout finished') - pendingTxComponent.gasPriceChanged(newGasPrice) + // Get the gas price input + // Set it to the newGasPrice value + // Wait for the value to change + // Get the submit button + // Click the submit button + // Get the output of the submit event. setTimeout(() => { console.log('hitting submit') @@ -59,9 +74,7 @@ describe.only('PendingTx', function () { }, 200) console.log('calling render') - pendingTxComponent.props = props - pendingTxComponent.checkValidity = () => { return true } - pendingTxComponent.render() }) }) + -- cgit v1.2.3 From de5cf2526ca3b3791545afc32d90a3bb88a8b8e3 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 11 May 2017 17:15:45 -0700 Subject: Fix test up a bit --- test/unit/components/pending-tx-test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index caaf66b49..5ddea7b23 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -7,7 +7,6 @@ console.dir(createReactFactory) const shallow = require('enzyme').shallow const Factory = createReactFactory(PendingTx) const ReactTestUtils = require('react-addons-test-utils') -const renderer = ReactTestUtils.createRenderer(); describe.only('PendingTx', function () { let pendingTxComponent @@ -38,6 +37,7 @@ describe.only('PendingTx', function () { it('should use updated values when edited.', function (done) { + const renderer = ReactTestUtils.createRenderer(); const newGasPrice = '0x451456' const props = { @@ -53,7 +53,9 @@ describe.only('PendingTx', function () { const pendingTxComponent = h(PendingTx, props) renderer.render(pendingTxComponent) - console.dir(pendingTxComponent) + const result = renderer.getRenderOutput() + assert.equal(result.type, 'div', 'should create a div') + console.dir(result) const noop = () => {} @@ -67,10 +69,6 @@ describe.only('PendingTx', function () { // Click the submit button // Get the output of the submit event. - setTimeout(() => { - console.log('hitting submit') - pendingTxComponent.onSubmit({ preventDefault: noop }) - }, 20) }, 200) console.log('calling render') -- cgit v1.2.3 From f0eeb1e1620b9c607390505c2e443979c9b44b1a Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 11 May 2017 17:43:40 -0700 Subject: Got a useful error message for next step --- test/unit/components/pending-tx-test.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index 5ddea7b23..2594a1a26 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -1,4 +1,5 @@ -var assert = require('assert') +const assert = require('assert') +const additions = require('react-testutils-additions') const h = require('react-hyperscript') var PendingTx = require('../../../ui/app/components/pending-tx') const createReactFactory = require('create-react-factory').createReactFactory @@ -52,10 +53,28 @@ describe.only('PendingTx', function () { } const pendingTxComponent = h(PendingTx, props) + var component = additions.renderIntoDocument(pendingTxComponent); renderer.render(pendingTxComponent) const result = renderer.getRenderOutput() + const form = result.props.children + console.log('FORM children') + console.dir(form.props.children) + const children = form.props.children[form.props.children.length - 1] assert.equal(result.type, 'div', 'should create a div') - console.dir(result) + console.dir(children) + + console.log('finding input') + + try{ + + const input = additions.find(component, '.cell.row input[type="number"]') + console.log('input') + console.dir(input) + + } catch (e) { + console.log("WHAAAA") + console.error(e) + } const noop = () => {} @@ -68,6 +87,7 @@ describe.only('PendingTx', function () { // Get the submit button // Click the submit button // Get the output of the submit event. + // Assert that the value was updated. }, 200) -- cgit v1.2.3 From 4b341e6a955d1fa71decfb021a86e7da09a933b0 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 15 May 2017 15:07:38 -0700 Subject: Got test failing nearly correctly --- test/lib/mock-store.js | 18 ++++++++++++++++++ test/unit/components/pending-tx-test.js | 29 +++++++++++++++-------------- 2 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 test/lib/mock-store.js (limited to 'test') diff --git a/test/lib/mock-store.js b/test/lib/mock-store.js new file mode 100644 index 000000000..4714c3485 --- /dev/null +++ b/test/lib/mock-store.js @@ -0,0 +1,18 @@ +const createStore = require('redux').createStore +const applyMiddleware = require('redux').applyMiddleware +const thunkMiddleware = require('redux-thunk') +const createLogger = require('redux-logger') +const rootReducer = function() {} + +module.exports = configureStore + +const loggerMiddleware = createLogger() + +const createStoreWithMiddleware = applyMiddleware( + thunkMiddleware, + loggerMiddleware +)(createStore) + +function configureStore (initialState) { + return createStoreWithMiddleware(rootReducer, initialState) +} diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index 2594a1a26..d7825d40e 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -1,11 +1,10 @@ const assert = require('assert') const additions = require('react-testutils-additions') const h = require('react-hyperscript') -var PendingTx = require('../../../ui/app/components/pending-tx') +const PendingTx = require('../../../ui/app/components/pending-tx') const createReactFactory = require('create-react-factory').createReactFactory const React = require('react') -console.dir(createReactFactory) -const shallow = require('enzyme').shallow +const shallow = require('react-test-renderer/shallow') const Factory = createReactFactory(PendingTx) const ReactTestUtils = require('react-addons-test-utils') @@ -53,23 +52,27 @@ describe.only('PendingTx', function () { } const pendingTxComponent = h(PendingTx, props) - var component = additions.renderIntoDocument(pendingTxComponent); + const component = additions.renderIntoDocument(pendingTxComponent); renderer.render(pendingTxComponent) const result = renderer.getRenderOutput() const form = result.props.children - console.log('FORM children') - console.dir(form.props.children) const children = form.props.children[form.props.children.length - 1] assert.equal(result.type, 'div', 'should create a div') - console.dir(children) - - console.log('finding input') try{ - const input = additions.find(component, '.cell.row input[type="number"]') - console.log('input') - console.dir(input) + const input = additions.find(component, '.cell.row input[type="number"]')[1] + ReactTestUtils.Simulate.change(input, { + target: { + value: 2, + checkValidity() { return true }, + } + }) + + let form = additions.find(component, 'form')[0] + form.checkValidity = () => true + form.getFormEl = () => { return { checkValidity() { return true } } } + ReactTestUtils.Simulate.submit(form, { preventDefault() {}, target: { checkValidity() {return true} } }) } catch (e) { console.log("WHAAAA") @@ -79,7 +82,6 @@ describe.only('PendingTx', function () { const noop = () => {} setTimeout(() => { - console.log('timeout finished') // Get the gas price input // Set it to the newGasPrice value @@ -91,7 +93,6 @@ describe.only('PendingTx', function () { }, 200) - console.log('calling render') }) }) -- cgit v1.2.3 From 75d9b5619c1b7e0949136702e7301ed0bb648f09 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 15 May 2017 15:21:28 -0700 Subject: Verify updating gas value updates --- test/unit/components/pending-tx-test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index d7825d40e..57fccba71 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -7,6 +7,7 @@ const React = require('react') const shallow = require('react-test-renderer/shallow') const Factory = createReactFactory(PendingTx) const ReactTestUtils = require('react-addons-test-utils') +const ethUtil = require('ethereumjs-util') describe.only('PendingTx', function () { let pendingTxComponent @@ -38,15 +39,16 @@ describe.only('PendingTx', function () { it('should use updated values when edited.', function (done) { const renderer = ReactTestUtils.createRenderer(); - const newGasPrice = '0x451456' + const newGasPrice = '0x77359400' const props = { identities, accounts: identities, txData, sendTransaction: (txMeta, event) => { - assert.notEqual(txMeta.txParams.gasPrice, gasPrice, 'gas price should change') - assert.equal(txMeta.txParams.gasPrice, newGasPrice, 'gas price assigned.') + const result = ethUtil.addHexPrefix(txMeta.txParams.gasPrice) + assert.notEqual(result, gasPrice, 'gas price should change') + assert.equal(result, newGasPrice, 'gas price assigned.') done() }, } -- cgit v1.2.3 From f9c0fc0e8cb04f371ce8e99c41c74989841c2c24 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 15 May 2017 15:23:38 -0700 Subject: Clean up test --- test/unit/components/pending-tx-test.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'test') diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js index 57fccba71..fe8290003 100644 --- a/test/unit/components/pending-tx-test.js +++ b/test/unit/components/pending-tx-test.js @@ -46,6 +46,8 @@ describe.only('PendingTx', function () { accounts: identities, txData, sendTransaction: (txMeta, event) => { + + // Assert changes: const result = ethUtil.addHexPrefix(txMeta.txParams.gasPrice) assert.notEqual(result, gasPrice, 'gas price should change') assert.equal(result, newGasPrice, 'gas price assigned.') @@ -81,20 +83,6 @@ describe.only('PendingTx', function () { console.error(e) } - const noop = () => {} - - setTimeout(() => { - - // Get the gas price input - // Set it to the newGasPrice value - // Wait for the value to change - // Get the submit button - // Click the submit button - // Get the output of the submit event. - // Assert that the value was updated. - - }, 200) - }) }) -- cgit v1.2.3 From 01b6d9c374476bd8c59fc0ba342639ddcea7ca8d Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 15 May 2017 23:54:05 -0700 Subject: test - format test data 001 --- test/lib/migrations/001.json | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/lib/migrations/001.json b/test/lib/migrations/001.json index 2fe6dd836..7bd55a50e 100644 --- a/test/lib/migrations/001.json +++ b/test/lib/migrations/001.json @@ -1 +1,14 @@ -{"version":0,"data":{"wallet":"{\"encSeed\":{\"encStr\":\"rT1C1jjkFRfmrwefscFcwZohl4f+HfIFlBZ9AM4ZD8atJmfKDIQCVK11NYDKYv8ZMIY03f3t8MuoZvfzBL8IJsWnZUhpzVTNNiARQJD2WpGA19eNBzgZm4vd0GwkIUruUDeJXu0iv2j9wU8hOQUqPbOePPy2Am5ro97iuvMAroRTnEKD60qFVg==\",\"nonce\":\"YUY2mwNq2v3FV0Fi94QnSiKFOLYfDR95\"},\"ksData\":{\"m/44'/60'/0'/0\":{\"info\":{\"curve\":\"secp256k1\",\"purpose\":\"sign\"},\"encHdPathPriv\":{\"encStr\":\"Iyi7ft4JQ9UtwrSXRT6ZIHPtZqJhe99rh0uWhNc6QLan6GanY2ZQeU0tt76CBealEWJyrJReSxGQdqDmSDYjpjH3m4JO5l0DfPLPseCqzXV/W+dzM0ubJ8lztLwpwi0L+vULNMqCx4dQtoNbNBq1QZUnjtpm6O8mWpScspboww==\",\"nonce\":\"Z7RqtjNjC6FrLUj5wVW1+HkjOW6Hib6K\"},\"hdIndex\":3,\"encPrivKeys\":{\"edb81c10122f34040cc4bef719a272fbbb1cf897\":{\"key\":\"8ab81tKBd4+CLAbzvS7SBFRTd6VWXBs86uBE43lgcmBu2U7UB22xdH64Q2hUf9eB\",\"nonce\":\"aGUEqI033FY39zKjWmZSI6PQrCLvkiRP\"},\"8bd7d5c000cf05284e98356370dc5ccaa3dbfc38\":{\"key\":\"+i3wmf4b+B898QtlOBfL0Ixirjg59/LLPX61vQ2L0xRPjXzNog0O4Wn15RemM5mY\",\"nonce\":\"imKrlkuoC5uuFkzJBbuDBluGCPJXNTKm\"},\"2340695474656e3124b8eba1172fbfb00eeac8f8\":{\"key\":\"pi+H9D8LYKsdCQKrfaJtsGFjE+X9s74xN675tsoIKrbPXhtpxMLOIQVtSqYveF62\",\"nonce\":\"49g80wDTovHwbguVVYf2FsYbp7Db5OAR\"}},\"addresses\":[\"edb81c10122f34040cc4bef719a272fbbb1cf897\",\"8bd7d5c000cf05284e98356370dc5ccaa3dbfc38\",\"2340695474656e3124b8eba1172fbfb00eeac8f8\"]}},\"version\":2}","config":{"provider":{"type":"etherscan"}}},"meta":{"version":0}} \ No newline at end of file +{ + "version": 0, + "data": { + "wallet": "{\"encSeed\":{\"encStr\":\"rT1C1jjkFRfmrwefscFcwZohl4f+HfIFlBZ9AM4ZD8atJmfKDIQCVK11NYDKYv8ZMIY03f3t8MuoZvfzBL8IJsWnZUhpzVTNNiARQJD2WpGA19eNBzgZm4vd0GwkIUruUDeJXu0iv2j9wU8hOQUqPbOePPy2Am5ro97iuvMAroRTnEKD60qFVg==\",\"nonce\":\"YUY2mwNq2v3FV0Fi94QnSiKFOLYfDR95\"},\"ksData\":{\"m/44'/60'/0'/0\":{\"info\":{\"curve\":\"secp256k1\",\"purpose\":\"sign\"},\"encHdPathPriv\":{\"encStr\":\"Iyi7ft4JQ9UtwrSXRT6ZIHPtZqJhe99rh0uWhNc6QLan6GanY2ZQeU0tt76CBealEWJyrJReSxGQdqDmSDYjpjH3m4JO5l0DfPLPseCqzXV/W+dzM0ubJ8lztLwpwi0L+vULNMqCx4dQtoNbNBq1QZUnjtpm6O8mWpScspboww==\",\"nonce\":\"Z7RqtjNjC6FrLUj5wVW1+HkjOW6Hib6K\"},\"hdIndex\":3,\"encPrivKeys\":{\"edb81c10122f34040cc4bef719a272fbbb1cf897\":{\"key\":\"8ab81tKBd4+CLAbzvS7SBFRTd6VWXBs86uBE43lgcmBu2U7UB22xdH64Q2hUf9eB\",\"nonce\":\"aGUEqI033FY39zKjWmZSI6PQrCLvkiRP\"},\"8bd7d5c000cf05284e98356370dc5ccaa3dbfc38\":{\"key\":\"+i3wmf4b+B898QtlOBfL0Ixirjg59/LLPX61vQ2L0xRPjXzNog0O4Wn15RemM5mY\",\"nonce\":\"imKrlkuoC5uuFkzJBbuDBluGCPJXNTKm\"},\"2340695474656e3124b8eba1172fbfb00eeac8f8\":{\"key\":\"pi+H9D8LYKsdCQKrfaJtsGFjE+X9s74xN675tsoIKrbPXhtpxMLOIQVtSqYveF62\",\"nonce\":\"49g80wDTovHwbguVVYf2FsYbp7Db5OAR\"}},\"addresses\":[\"edb81c10122f34040cc4bef719a272fbbb1cf897\",\"8bd7d5c000cf05284e98356370dc5ccaa3dbfc38\",\"2340695474656e3124b8eba1172fbfb00eeac8f8\"]}},\"version\":2}", + "config": { + "provider": { + "type": "etherscan" + } + } + }, + "meta": { + "version": 0 + } +} \ No newline at end of file -- cgit v1.2.3 From 28aba6e9dea52b66534d6ecb9713a7d20947c57c Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 15 May 2017 23:56:13 -0700 Subject: migration 13 - change provider from testnet to ropsten --- test/unit/migrations-test.js | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test') diff --git a/test/unit/migrations-test.js b/test/unit/migrations-test.js index 324e4d056..5bad25a45 100644 --- a/test/unit/migrations-test.js +++ b/test/unit/migrations-test.js @@ -16,6 +16,7 @@ const migration9 = require(path.join('..', '..', 'app', 'scripts', 'migrations', const migration10 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '010')) const migration11 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '011')) const migration12 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '012')) +const migration13 = require(path.join('..', '..', 'app', 'scripts', 'migrations', '013')) const oldTestRpc = 'https://rawtestrpc.metamask.io/' @@ -97,6 +98,11 @@ describe('wallet1 is migrated successfully', () => { }).then((twelfthResult) => { assert.equal(twelfthResult.data.NoticeController.noticesList[0].body, '', 'notices that have been read should have an empty body.') assert.equal(twelfthResult.data.NoticeController.noticesList[1].body, 'nonempty', 'notices that have not been read should not have an empty body.') + + assert.equal(twelfthResult.data.config.provider.type, 'testnet', 'network is originally testnet.') + return migration13.migrate(twelfthResult) + }).then((thirteenthResult) => { + assert.equal(thirteenthResult.data.config.provider.type, 'ropsten', 'network has been changed to ropsten.') }) }) }) -- cgit v1.2.3 From 2df9344be5bf1c65daae2ca7ea47982fe2a1c2fb Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 16 May 2017 10:27:41 -0700 Subject: Rename tx manager to tx controller --- test/unit/tx-controller-test.js | 237 ++++++++++++++++++++++++++++++++++++++++ test/unit/tx-manager-test.js | 237 ---------------------------------------- 2 files changed, 237 insertions(+), 237 deletions(-) create mode 100644 test/unit/tx-controller-test.js delete mode 100644 test/unit/tx-manager-test.js (limited to 'test') diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js new file mode 100644 index 000000000..d0b32ff41 --- /dev/null +++ b/test/unit/tx-controller-test.js @@ -0,0 +1,237 @@ +const assert = require('assert') +const EventEmitter = require('events') +const ethUtil = require('ethereumjs-util') +const EthTx = require('ethereumjs-tx') +const ObservableStore = require('obs-store') +const TransactionController = require('../../app/scripts/controllers/transactions') +const noop = () => true +const currentNetworkId = 42 +const otherNetworkId = 36 +const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex') + +describe('Transaction Manager', function () { + let txController + + beforeEach(function () { + txController = new TransactionController({ + networkStore: new ObservableStore({ network: currentNetworkId }), + txHistoryLimit: 10, + blockTracker: new EventEmitter(), + signTransaction: (ethTx) => new Promise((resolve) => { + ethTx.sign(privKey) + resolve() + }), + }) + }) + + describe('#validateTxParams', function () { + it('returns null for positive values', function () { + var sample = { + value: '0x01', + } + txController.txProviderUtils.validateTxParams(sample, (err) => { + assert.equal(err, null, 'no error') + }) + }) + + it('returns error for negative values', function () { + var sample = { + value: '-0x01', + } + txController.txProviderUtils.validateTxParams(sample, (err) => { + assert.ok(err, 'error') + }) + }) + }) + + describe('#getTxList', function () { + it('when new should return empty array', function () { + var result = txController.getTxList() + assert.ok(Array.isArray(result)) + assert.equal(result.length, 0) + }) + it('should also return transactions from local storage if any', function () { + + }) + }) + + describe('#addTx', function () { + it('adds a tx returned in getTxList', function () { + var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(tx, noop) + var result = txController.getTxList() + assert.ok(Array.isArray(result)) + assert.equal(result.length, 1) + assert.equal(result[0].id, 1) + }) + + it('does not override txs from other networks', function () { + var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } + var tx2 = { id: 2, status: 'confirmed', metamaskNetworkId: otherNetworkId, txParams: {} } + txController.addTx(tx, noop) + txController.addTx(tx2, noop) + var result = txController.getFullTxList() + var result2 = txController.getTxList() + assert.equal(result.length, 2, 'txs were deleted') + assert.equal(result2.length, 1, 'incorrect number of txs on network.') + }) + + it('cuts off early txs beyond a limit', function () { + const limit = txController.txHistoryLimit + for (let i = 0; i < limit + 1; i++) { + const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(tx, noop) + } + var result = txController.getTxList() + assert.equal(result.length, limit, `limit of ${limit} txs enforced`) + assert.equal(result[0].id, 1, 'early txs truncted') + }) + + it('cuts off early txs beyond a limit whether or not it is confirmed or rejected', function () { + const limit = txController.txHistoryLimit + for (let i = 0; i < limit + 1; i++) { + const tx = { id: i, time: new Date(), status: 'rejected', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(tx, noop) + } + var result = txController.getTxList() + assert.equal(result.length, limit, `limit of ${limit} txs enforced`) + assert.equal(result[0].id, 1, 'early txs truncted') + }) + + it('cuts off early txs beyond a limit but does not cut unapproved txs', function () { + var unconfirmedTx = { id: 0, time: new Date(), status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(unconfirmedTx, noop) + const limit = txController.txHistoryLimit + for (let i = 1; i < limit + 1; i++) { + const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(tx, noop) + } + var result = txController.getTxList() + assert.equal(result.length, limit, `limit of ${limit} txs enforced`) + assert.equal(result[0].id, 0, 'first tx should still be there') + assert.equal(result[0].status, 'unapproved', 'first tx should be unapproved') + assert.equal(result[1].id, 2, 'early txs truncted') + }) + }) + + describe('#setTxStatusSigned', function () { + it('sets the tx status to signed', function () { + var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(tx, noop) + txController.setTxStatusSigned(1) + var result = txController.getTxList() + assert.ok(Array.isArray(result)) + assert.equal(result.length, 1) + assert.equal(result[0].status, 'signed') + }) + + it('should emit a signed event to signal the exciton of callback', (done) => { + this.timeout(10000) + var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } + const noop = function () { + assert(true, 'event listener has been triggered and noop executed') + done() + } + txController.addTx(tx) + txController.on('1:signed', noop) + txController.setTxStatusSigned(1) + }) + }) + + describe('#setTxStatusRejected', function () { + it('sets the tx status to rejected', function () { + var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(tx) + txController.setTxStatusRejected(1) + var result = txController.getTxList() + assert.ok(Array.isArray(result)) + assert.equal(result.length, 1) + assert.equal(result[0].status, 'rejected') + }) + + it('should emit a rejected event to signal the exciton of callback', (done) => { + this.timeout(10000) + var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } + txController.addTx(tx) + const noop = function (err, txId) { + assert(true, 'event listener has been triggered and noop executed') + done() + } + txController.on('1:rejected', noop) + txController.setTxStatusRejected(1) + }) + }) + + describe('#updateTx', function () { + it('replaces the tx with the same id', function () { + txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) + txController.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) + txController.updateTx({ id: '1', status: 'blah', hash: 'foo', metamaskNetworkId: currentNetworkId, txParams: {} }) + var result = txController.getTx('1') + assert.equal(result.hash, 'foo') + }) + }) + + describe('#getUnapprovedTxList', function () { + it('returns unapproved txs in a hash', function () { + txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) + txController.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) + const result = txController.getUnapprovedTxList() + assert.equal(typeof result, 'object') + assert.equal(result['1'].status, 'unapproved') + assert.equal(result['2'], undefined) + }) + }) + + describe('#getTx', function () { + it('returns a tx with the requested id', function () { + txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) + txController.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) + assert.equal(txController.getTx('1').status, 'unapproved') + assert.equal(txController.getTx('2').status, 'confirmed') + }) + }) + + describe('#getFilteredTxList', function () { + it('returns a tx with the requested data', function () { + const txMetas = [ + { id: 0, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, + { id: 1, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, + { id: 2, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, + { id: 3, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, + { id: 4, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, + { id: 5, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, + { id: 6, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, + { id: 7, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, + { id: 8, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, + { id: 9, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, + ] + txMetas.forEach((txMeta) => txController.addTx(txMeta, noop)) + let filterParams + + filterParams = { status: 'unapproved', from: '0xaa' } + assert.equal(txController.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`) + filterParams = { status: 'unapproved', to: '0xaa' } + assert.equal(txController.getFilteredTxList(filterParams).length, 2, `getFilteredTxList - ${JSON.stringify(filterParams)}`) + filterParams = { status: 'confirmed', from: '0xbb' } + assert.equal(txController.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`) + filterParams = { status: 'confirmed' } + assert.equal(txController.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`) + filterParams = { from: '0xaa' } + assert.equal(txController.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`) + filterParams = { to: '0xaa' } + assert.equal(txController.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`) + }) + }) + + describe('#sign replay-protected tx', function () { + it('prepares a tx with the chainId set', function () { + txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) + txController.signTransaction('1', (err, rawTx) => { + if (err) return assert.fail('it should not fail') + const ethTx = new EthTx(ethUtil.toBuffer(rawTx)) + assert.equal(ethTx.getChainId(), currentNetworkId) + }) + }) + }) +}) diff --git a/test/unit/tx-manager-test.js b/test/unit/tx-manager-test.js deleted file mode 100644 index b5d148723..000000000 --- a/test/unit/tx-manager-test.js +++ /dev/null @@ -1,237 +0,0 @@ -const assert = require('assert') -const EventEmitter = require('events') -const ethUtil = require('ethereumjs-util') -const EthTx = require('ethereumjs-tx') -const ObservableStore = require('obs-store') -const TransactionManager = require('../../app/scripts/transaction-manager') -const noop = () => true -const currentNetworkId = 42 -const otherNetworkId = 36 -const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex') - -describe('Transaction Manager', function () { - let txManager - - beforeEach(function () { - txManager = new TransactionManager({ - networkStore: new ObservableStore({ network: currentNetworkId }), - txHistoryLimit: 10, - blockTracker: new EventEmitter(), - signTransaction: (ethTx) => new Promise((resolve) => { - ethTx.sign(privKey) - resolve() - }), - }) - }) - - describe('#validateTxParams', function () { - it('returns null for positive values', function () { - var sample = { - value: '0x01', - } - txManager.txProviderUtils.validateTxParams(sample, (err) => { - assert.equal(err, null, 'no error') - }) - }) - - it('returns error for negative values', function () { - var sample = { - value: '-0x01', - } - txManager.txProviderUtils.validateTxParams(sample, (err) => { - assert.ok(err, 'error') - }) - }) - }) - - describe('#getTxList', function () { - it('when new should return empty array', function () { - var result = txManager.getTxList() - assert.ok(Array.isArray(result)) - assert.equal(result.length, 0) - }) - it('should also return transactions from local storage if any', function () { - - }) - }) - - describe('#addTx', function () { - it('adds a tx returned in getTxList', function () { - var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(tx, noop) - var result = txManager.getTxList() - assert.ok(Array.isArray(result)) - assert.equal(result.length, 1) - assert.equal(result[0].id, 1) - }) - - it('does not override txs from other networks', function () { - var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } - var tx2 = { id: 2, status: 'confirmed', metamaskNetworkId: otherNetworkId, txParams: {} } - txManager.addTx(tx, noop) - txManager.addTx(tx2, noop) - var result = txManager.getFullTxList() - var result2 = txManager.getTxList() - assert.equal(result.length, 2, 'txs were deleted') - assert.equal(result2.length, 1, 'incorrect number of txs on network.') - }) - - it('cuts off early txs beyond a limit', function () { - const limit = txManager.txHistoryLimit - for (let i = 0; i < limit + 1; i++) { - const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(tx, noop) - } - var result = txManager.getTxList() - assert.equal(result.length, limit, `limit of ${limit} txs enforced`) - assert.equal(result[0].id, 1, 'early txs truncted') - }) - - it('cuts off early txs beyond a limit whether or not it is confirmed or rejected', function () { - const limit = txManager.txHistoryLimit - for (let i = 0; i < limit + 1; i++) { - const tx = { id: i, time: new Date(), status: 'rejected', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(tx, noop) - } - var result = txManager.getTxList() - assert.equal(result.length, limit, `limit of ${limit} txs enforced`) - assert.equal(result[0].id, 1, 'early txs truncted') - }) - - it('cuts off early txs beyond a limit but does not cut unapproved txs', function () { - var unconfirmedTx = { id: 0, time: new Date(), status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(unconfirmedTx, noop) - const limit = txManager.txHistoryLimit - for (let i = 1; i < limit + 1; i++) { - const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(tx, noop) - } - var result = txManager.getTxList() - assert.equal(result.length, limit, `limit of ${limit} txs enforced`) - assert.equal(result[0].id, 0, 'first tx should still be there') - assert.equal(result[0].status, 'unapproved', 'first tx should be unapproved') - assert.equal(result[1].id, 2, 'early txs truncted') - }) - }) - - describe('#setTxStatusSigned', function () { - it('sets the tx status to signed', function () { - var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(tx, noop) - txManager.setTxStatusSigned(1) - var result = txManager.getTxList() - assert.ok(Array.isArray(result)) - assert.equal(result.length, 1) - assert.equal(result[0].status, 'signed') - }) - - it('should emit a signed event to signal the exciton of callback', (done) => { - this.timeout(10000) - var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } - const noop = function () { - assert(true, 'event listener has been triggered and noop executed') - done() - } - txManager.addTx(tx) - txManager.on('1:signed', noop) - txManager.setTxStatusSigned(1) - }) - }) - - describe('#setTxStatusRejected', function () { - it('sets the tx status to rejected', function () { - var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(tx) - txManager.setTxStatusRejected(1) - var result = txManager.getTxList() - assert.ok(Array.isArray(result)) - assert.equal(result.length, 1) - assert.equal(result[0].status, 'rejected') - }) - - it('should emit a rejected event to signal the exciton of callback', (done) => { - this.timeout(10000) - var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} } - txManager.addTx(tx) - const noop = function (err, txId) { - assert(true, 'event listener has been triggered and noop executed') - done() - } - txManager.on('1:rejected', noop) - txManager.setTxStatusRejected(1) - }) - }) - - describe('#updateTx', function () { - it('replaces the tx with the same id', function () { - txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - txManager.updateTx({ id: '1', status: 'blah', hash: 'foo', metamaskNetworkId: currentNetworkId, txParams: {} }) - var result = txManager.getTx('1') - assert.equal(result.hash, 'foo') - }) - }) - - describe('#getUnapprovedTxList', function () { - it('returns unapproved txs in a hash', function () { - txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - const result = txManager.getUnapprovedTxList() - assert.equal(typeof result, 'object') - assert.equal(result['1'].status, 'unapproved') - assert.equal(result['2'], undefined) - }) - }) - - describe('#getTx', function () { - it('returns a tx with the requested id', function () { - txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - assert.equal(txManager.getTx('1').status, 'unapproved') - assert.equal(txManager.getTx('2').status, 'confirmed') - }) - }) - - describe('#getFilteredTxList', function () { - it('returns a tx with the requested data', function () { - const txMetas = [ - { id: 0, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, - { id: 1, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, - { id: 2, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, - { id: 3, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, - { id: 4, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, - { id: 5, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, - { id: 6, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId }, - { id: 7, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, - { id: 8, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, - { id: 9, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId }, - ] - txMetas.forEach((txMeta) => txManager.addTx(txMeta, noop)) - let filterParams - - filterParams = { status: 'unapproved', from: '0xaa' } - assert.equal(txManager.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`) - filterParams = { status: 'unapproved', to: '0xaa' } - assert.equal(txManager.getFilteredTxList(filterParams).length, 2, `getFilteredTxList - ${JSON.stringify(filterParams)}`) - filterParams = { status: 'confirmed', from: '0xbb' } - assert.equal(txManager.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`) - filterParams = { status: 'confirmed' } - assert.equal(txManager.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`) - filterParams = { from: '0xaa' } - assert.equal(txManager.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`) - filterParams = { to: '0xaa' } - assert.equal(txManager.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`) - }) - }) - - describe('#sign replay-protected tx', function () { - it('prepares a tx with the chainId set', function () { - txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - txManager.signTransaction('1', (err, rawTx) => { - if (err) return assert.fail('it should not fail') - const ethTx = new EthTx(ethUtil.toBuffer(rawTx)) - assert.equal(ethTx.getChainId(), currentNetworkId) - }) - }) - }) -}) -- cgit v1.2.3