Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Web3Provider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions test/.setup.js
Original file line number Diff line number Diff line change
@@ -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('<!doctype html><html><body></body></html>');
Expand All @@ -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;
Expand Down
18 changes: 8 additions & 10 deletions test/Web3Provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,8 +21,6 @@ function runTests(version) {

if (version === 'v1') {
window.web3 = window.web3_v1;
} else {
window.web3 = web3_v0;
}

clock = sinon.useFakeTimers();
Expand Down Expand Up @@ -65,7 +61,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()
Expand Down Expand Up @@ -94,7 +90,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(
<Web3Provider>
Expand All @@ -119,7 +115,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(
<Web3Provider>
Expand All @@ -135,7 +131,8 @@ function runTests(version) {
);

// simulate changing account
window.web3.setAccounts(['0x222']);
window.ethereum.setAccounts(['0x222']);

await wrapper.instance().fetchAccounts()
clock.tick(1500);

Expand All @@ -148,7 +145,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(
<Web3Provider>
Expand All @@ -164,7 +162,7 @@ function runTests(version) {
);

// simulate logging out
window.web3.setAccounts([]);
window.ethereum.setAccounts([]);
await wrapper.instance().fetchAccounts()
clock.tick(1500);

Expand Down
9 changes: 9 additions & 0 deletions test/helpers/ethereum.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let defaultAccounts = [];
let accounts = defaultAccounts.slice();

module.exports = {
setAccounts: v => accounts = v,
enable: () => {
return Promise.resolve(accounts)
},
};
1 change: 0 additions & 1 deletion test/helpers/web3.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = {
},
},
setNetwork: v => network = v,
setAccounts: v => accounts = v,
restore: () => {
accounts = defaultAccounts.slice();
network = defaultNetwork;
Expand Down