From 153ea1bf5657b59d555ee65e80cf8a42e6935e82 Mon Sep 17 00:00:00 2001 From: gohuygo Date: Mon, 26 Nov 2018 18:33:28 -0800 Subject: [PATCH 1/3] Use window.ethereum instead of window.web3.currentProvider --- src/Web3Provider.jsx | 14 +++++++------- test/.setup.js | 3 +++ test/Web3Provider.test.js | 15 +++++++++------ test/helpers/ethereum.mock.js | 9 +++++++++ test/helpers/web3.mock.js | 1 - 5 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 test/helpers/ethereum.mock.js diff --git a/src/Web3Provider.jsx b/src/Web3Provider.jsx index 9ccde11..61ed569 100644 --- a/src/Web3Provider.jsx +++ b/src/Web3Provider.jsx @@ -98,17 +98,17 @@ class Web3Provider extends React.Component { * @return {void} */ fetchAccounts() { - const { web3 } = window; + const { ethereum } = window; const ethAccounts = this.getAccounts(); if (isEmpty(ethAccounts)) { - web3 && web3.currentProvider && web3.currentProvider.enable() - .then(accounts => this.handleAccounts(accounts)) - .catch((err) => { - this.setState({ - accountsError: err + ethereum && ethereum.enable() + .then(accounts => this.handleAccounts(accounts)) + .catch((err) => { + this.setState({ + accountsError: err + }); }); - }); } else { this.handleAccounts(ethAccounts); } diff --git a/test/.setup.js b/test/.setup.js index 0b89e6b..83b1906 100644 --- a/test/.setup.js +++ b/test/.setup.js @@ -1,6 +1,7 @@ require('babel-polyfill'); const { JSDOM } = require('jsdom'); const web3 = require('./helpers/web3.mock.js'); +const ethereum = require('./helpers/ethereum.mock.js') const web3_v1 = require('./helpers/web3-v1.mock.js'); const jsdom = new JSDOM(''); @@ -13,6 +14,8 @@ function copyProps(src, target) { Object.defineProperties(target, props); } +window.ethereum = ethereum; +global.ethereum = ethereum; window.web3 = web3; global.web3 = web3; window.web3_v1 = web3_v1; diff --git a/test/Web3Provider.test.js b/test/Web3Provider.test.js index 91f5eb3..97f3173 100644 --- a/test/Web3Provider.test.js +++ b/test/Web3Provider.test.js @@ -24,6 +24,7 @@ function runTests(version) { if (version === 'v1') { window.web3 = window.web3_v1; } else { + console.log("yo", window.ethereum) window.web3 = web3_v0; } @@ -65,7 +66,7 @@ function runTests(version) { }); it('should set context.accounts if available', async () => { - window.web3.setAccounts(['0x987']); + window.ethereum.setAccounts(['0x987']); const wrapper = getMount(); const instance = wrapper.instance(); await instance.fetchAccounts() @@ -94,7 +95,7 @@ function runTests(version) { describe('Redux Integration', function () { describe('When accounts becomes available', () => { it('dispatches an action', async () => { - window.web3.setAccounts(['0x111']); + window.ethereum.setAccounts(['0x111']); const spy = sinon.spy(); const wrapper = mount( @@ -119,7 +120,7 @@ function runTests(version) { }); describe('When switching between accounts', () => { it('dispatches an action', async () => { - window.web3.setAccounts(['0x111']); + window.ethereum.setAccounts(['0x111']); const spy = sinon.spy(); const wrapper = mount( @@ -135,7 +136,8 @@ function runTests(version) { ); // simulate changing account - window.web3.setAccounts(['0x222']); + window.ethereum.setAccounts(['0x222']); + await wrapper.instance().fetchAccounts() clock.tick(1500); @@ -148,7 +150,8 @@ function runTests(version) { describe('When logging out', () => { it('dispatches an action', async () => { - window.web3.setAccounts(['0x111']); + window.ethereum.setAccounts(['0x111']); + const spy = sinon.spy(); const wrapper = mount( @@ -164,7 +167,7 @@ function runTests(version) { ); // simulate logging out - window.web3.setAccounts([]); + window.ethereum.setAccounts([]); await wrapper.instance().fetchAccounts() clock.tick(1500); diff --git a/test/helpers/ethereum.mock.js b/test/helpers/ethereum.mock.js new file mode 100644 index 0000000..af1e884 --- /dev/null +++ b/test/helpers/ethereum.mock.js @@ -0,0 +1,9 @@ +let defaultAccounts = []; +let accounts = defaultAccounts.slice(); + +module.exports = { + setAccounts: v => accounts = v, + enable: () => { + return Promise.resolve(accounts) + }, +}; diff --git a/test/helpers/web3.mock.js b/test/helpers/web3.mock.js index 84a8320..26b9bf3 100644 --- a/test/helpers/web3.mock.js +++ b/test/helpers/web3.mock.js @@ -17,7 +17,6 @@ module.exports = { }, }, setNetwork: v => network = v, - setAccounts: v => accounts = v, restore: () => { accounts = defaultAccounts.slice(); network = defaultNetwork; From 3797c4a91e33a8173ebf66bc48ec8291636fea45 Mon Sep 17 00:00:00 2001 From: gohuygo Date: Mon, 26 Nov 2018 18:40:13 -0800 Subject: [PATCH 2/3] Remove console.log --- test/Web3Provider.test.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/Web3Provider.test.js b/test/Web3Provider.test.js index 97f3173..f0c9e9f 100644 --- a/test/Web3Provider.test.js +++ b/test/Web3Provider.test.js @@ -10,8 +10,6 @@ import { wait, getWrapper, getMount } from './helpers/utils'; let clock; const { window } = global; -const web3_v0 = window.web3; - /** * We wrap all the tests in a function so we can run with web3 < 1.0 and again * with web3 >= 1.0 @@ -24,8 +22,7 @@ function runTests(version) { if (version === 'v1') { window.web3 = window.web3_v1; } else { - console.log("yo", window.ethereum) - window.web3 = web3_v0; + window.web3 = window.web3; } clock = sinon.useFakeTimers(); From d3c77f510b57c66c39f7a128b73677e4d70292ac Mon Sep 17 00:00:00 2001 From: gohuygo Date: Mon, 26 Nov 2018 19:11:53 -0800 Subject: [PATCH 3/3] Clean up conditional in tests --- test/Web3Provider.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/Web3Provider.test.js b/test/Web3Provider.test.js index f0c9e9f..e1744d9 100644 --- a/test/Web3Provider.test.js +++ b/test/Web3Provider.test.js @@ -21,8 +21,6 @@ function runTests(version) { if (version === 'v1') { window.web3 = window.web3_v1; - } else { - window.web3 = window.web3; } clock = sinon.useFakeTimers();