This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from JoinColony/feature/191-expose-purser-met…
…amask-account-change-hook Expose `purser-metamask` account change hook method
- Loading branch information
Showing
4 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import metamaskWallet from '@colony/purser-metamask'; | ||
import { | ||
detect as detectHelper, | ||
setStateEventObserver, | ||
} from '@colony/purser-metamask/helpers'; | ||
|
||
jest.dontMock('@colony/purser-metamask'); | ||
|
||
/* | ||
* @TODO Fix manual mocks | ||
* This is needed since Jest won't see our manual mocks (because of our custom monorepo structure) | ||
* and will replace them with automatic ones | ||
*/ | ||
jest.mock('@colony/purser-metamask/helpers', () => | ||
require('@mocks/purser-metamask/helpers'), | ||
); | ||
|
||
/* | ||
* Mock the global injected inpage provider | ||
*/ | ||
global.web3 = { | ||
currentProvider: { | ||
publicConfigStore: { | ||
_events: { | ||
update: [], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const mockedCallback = jest.fn(state => state); | ||
|
||
describe('Metamask` Wallet Module', () => { | ||
describe('`accountChangeHook()` static method', () => { | ||
test('Calls the correct helper method', async () => { | ||
await metamaskWallet.accountChangeHook(); | ||
/* | ||
* Call the helper method | ||
*/ | ||
expect(setStateEventObserver).toHaveBeenCalled(); | ||
}); | ||
test('Detects if Metamask is available', async () => { | ||
await metamaskWallet.accountChangeHook(); | ||
/* | ||
* Calls the `detect()` helper | ||
*/ | ||
expect(detectHelper).toHaveBeenCalled(); | ||
}); | ||
test('Adds a callback to the state observer', async () => { | ||
await metamaskWallet.accountChangeHook(mockedCallback); | ||
expect( | ||
/* eslint-disable-next-line no-underscore-dangle */ | ||
global.web3.currentProvider.publicConfigStore._events.update, | ||
).toContain(mockedCallback); | ||
}); | ||
test('Catches if something goes wrong', async () => { | ||
/* | ||
* We're re-mocking the helpers just for this test so we can simulate | ||
* an error along the way | ||
*/ | ||
setStateEventObserver.mockRejectedValueOnce(new Error()); | ||
expect(metamaskWallet.accountChangeHook()).rejects.toThrow(); | ||
}); | ||
}); | ||
}); |