-
-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement EIP-6963 #263
Implement EIP-6963 #263
Changes from 11 commits
753329c
0c7f43f
2715047
2aa85d5
117ae12
8eb8c8e
7636950
c7fb13b
2056aab
80d80ef
ab7f946
a60df89
11a15a9
2de3189
e9e9d39
c1d5dc6
60be395
82abfbf
06ce8e5
c38d4e4
8641b8f
5690876
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
Object.assign(global, require('jest-chrome')); | ||
|
||
// Clean up jsdom between tests. | ||
// Courtesy @jhildenbiddle | ||
// https://github.com/jestjs/jest/issues/1224#issuecomment-716075260 | ||
const sideEffects = { | ||
document: { | ||
addEventListener: { | ||
fn: document.addEventListener, | ||
refs: [], | ||
}, | ||
keys: Object.keys(document), | ||
}, | ||
window: { | ||
addEventListener: { | ||
fn: window.addEventListener, | ||
refs: [], | ||
}, | ||
keys: Object.keys(window), | ||
}, | ||
}; | ||
|
||
// Lifecycle Hooks | ||
// ----------------------------------------------------------------------------- | ||
beforeAll(async () => { | ||
// Spy addEventListener | ||
['document', 'window'].forEach((obj) => { | ||
const { fn, refs } = sideEffects[obj].addEventListener; | ||
|
||
/** | ||
* | ||
* @param type | ||
* @param listener | ||
* @param options | ||
*/ | ||
function addEventListenerSpy(type, listener, options) { | ||
// Store listener reference so it can be removed during reset | ||
refs.push({ type, listener, options }); | ||
// Call original window.addEventListener | ||
fn(type, listener, options); | ||
} | ||
|
||
// Add to default key array to prevent removal during reset | ||
sideEffects[obj].keys.push('addEventListener'); | ||
|
||
// Replace addEventListener with mock | ||
global[obj].addEventListener = addEventListenerSpy; | ||
}); | ||
}); | ||
|
||
// Reset JSDOM. This attempts to remove side effects from tests, however it does | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran into JSDOM state issues when working on the phishing warning page too. In that case, I found it easiest to just use a real browser. See MetaMask/phishing-warning#62 Likewise for Something to consider, if this needs further maintenance or continues to be a burden. |
||
// not reset all changes made to globals like the window and document | ||
// objects. Tests requiring a full JSDOM reset should be stored in separate | ||
// files, which is only way to do a complete JSDOM reset with Jest. | ||
beforeEach(async () => { | ||
const rootElm = document.documentElement; | ||
|
||
// Remove attributes on root element | ||
[...rootElm.attributes].forEach((attr) => rootElm.removeAttribute(attr.name)); | ||
|
||
// Remove elements (faster than setting innerHTML) | ||
while (rootElm.firstChild) { | ||
rootElm.removeChild(rootElm.firstChild); | ||
} | ||
|
||
// Remove global listeners and keys | ||
['document', 'window'].forEach((obj) => { | ||
const { refs } = sideEffects[obj].addEventListener; | ||
|
||
// Listeners | ||
while (refs.length) { | ||
const { type, listener, options } = refs.pop(); | ||
global[obj].removeEventListener(type, listener, options); | ||
} | ||
|
||
// This breaks coverage. | ||
// // Keys | ||
// Object.keys(global[obj]) | ||
// .filter((key) => !sideEffects[obj].keys.includes(key)) | ||
// .forEach((key) => { | ||
// delete global[obj][key]; | ||
// }); | ||
}); | ||
|
||
// Restore base elements | ||
rootElm.innerHTML = '<head></head><body></body>'; | ||
}); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import { announceProvider, requestProvider } from './EIP6963'; | ||
|
||
const getProviderInfo = () => ({ | ||
name: 'test', | ||
icon: 'https://wallet.io/icon.svg', | ||
uuid: '1449211e-5560-4235-9ab1-582cbe2b165f', | ||
}); | ||
|
||
const providerInfoValidationError = () => | ||
new Error( | ||
'Invalid EIP-6963 ProviderDetail object. See https://eips.ethereum.org/EIPS/eip-6963 for requirements.', | ||
); | ||
|
||
describe('EIP6963', () => { | ||
describe('announceProvider', () => { | ||
describe('provider info validation', () => { | ||
it('throws if the provider info is not a plain object', () => { | ||
[null, undefined, Symbol('bar'), []].forEach((invalidInfo) => { | ||
expect(() => announceProvider(invalidInfo as any)).toThrow( | ||
providerInfoValidationError(), | ||
); | ||
}); | ||
}); | ||
|
||
it('throws if the `icon` field is invalid', () => { | ||
[null, undefined, '', 'not-a-url', Symbol('bar')].forEach( | ||
(invalidIcon) => { | ||
const provider: any = { name: 'test' }; | ||
const providerDetail = { info: getProviderInfo(), provider }; | ||
providerDetail.info.icon = invalidIcon as any; | ||
|
||
expect(() => announceProvider(providerDetail)).toThrow( | ||
providerInfoValidationError(), | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
it('throws if the `name` field is invalid', () => { | ||
[null, undefined, '', {}, [], Symbol('bar')].forEach((invalidName) => { | ||
const provider: any = { name: 'test' }; | ||
const providerDetail = { info: getProviderInfo(), provider }; | ||
providerDetail.info.name = invalidName as any; | ||
|
||
expect(() => announceProvider(providerDetail)).toThrow( | ||
providerInfoValidationError(), | ||
); | ||
}); | ||
}); | ||
|
||
it('throws if the `uuid` field is invalid', () => { | ||
[null, undefined, '', 'foo', Symbol('bar')].forEach((invalidUuid) => { | ||
const provider: any = { name: 'test' }; | ||
const providerDetail = { info: getProviderInfo(), provider }; | ||
providerDetail.info.uuid = invalidUuid as any; | ||
|
||
expect(() => announceProvider(providerDetail)).toThrow( | ||
providerInfoValidationError(), | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('provider validation', () => { | ||
it('throws if the provider is not a plain object', () => { | ||
[null, undefined, Symbol('bar'), []].forEach((invalidProvider) => { | ||
const provider: any = invalidProvider; | ||
const providerDetail = { info: getProviderInfo(), provider }; | ||
|
||
expect(() => announceProvider(providerDetail)).toThrow( | ||
providerInfoValidationError(), | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should announce a provider', () => { | ||
jiexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const provider: any = { name: 'test' }; | ||
const providerDetail = { info: getProviderInfo(), provider }; | ||
const dispatchEvent = jest.spyOn(window, 'dispatchEvent'); | ||
const addEventListener = jest.spyOn(window, 'addEventListener'); | ||
|
||
announceProvider(providerDetail); | ||
|
||
expect(dispatchEvent).toHaveBeenCalledTimes(1); | ||
expect(dispatchEvent).toHaveBeenCalledWith( | ||
new CustomEvent('eip6963:announceProvider', { | ||
detail: { | ||
info: getProviderInfo(), | ||
provider, | ||
}, | ||
}), | ||
); | ||
expect(addEventListener).toHaveBeenCalledTimes(1); | ||
expect(addEventListener).toHaveBeenCalledWith( | ||
'eip6963:requestProvider', | ||
expect.any(Function), | ||
); | ||
}); | ||
}); | ||
|
||
describe('requestProvider', () => { | ||
it('should receive an announced provider (called before announceProvider)', async () => { | ||
const provider: any = { name: 'test' }; | ||
const providerDetail = { info: getProviderInfo(), provider }; | ||
const handleProvider = jest.fn(); | ||
const dispatchEvent = jest.spyOn(window, 'dispatchEvent'); | ||
const addEventListener = jest.spyOn(window, 'addEventListener'); | ||
jiexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
requestProvider(handleProvider); | ||
announceProvider(providerDetail); | ||
await delay(); | ||
|
||
expect(dispatchEvent).toHaveBeenCalledTimes(2); | ||
expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
1, | ||
new Event('eip6963:requestProvider'), | ||
); | ||
expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
2, | ||
new CustomEvent('eip6963:announceProvider'), | ||
); | ||
jiexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expect(addEventListener).toHaveBeenCalledTimes(2); | ||
expect(addEventListener).toHaveBeenCalledWith( | ||
'eip6963:announceProvider', | ||
expect.any(Function), | ||
); | ||
expect(addEventListener).toHaveBeenCalledWith( | ||
'eip6963:requestProvider', | ||
expect.any(Function), | ||
); | ||
|
||
expect(handleProvider).toHaveBeenCalledTimes(1); | ||
expect(handleProvider).toHaveBeenCalledWith({ | ||
info: getProviderInfo(), | ||
provider, | ||
}); | ||
}); | ||
|
||
it('should receive an announced provider (called after announceProvider)', async () => { | ||
const provider: any = { name: 'test' }; | ||
const providerDetail = { info: getProviderInfo(), provider }; | ||
const handleProvider = jest.fn(); | ||
const dispatchEvent = jest.spyOn(window, 'dispatchEvent'); | ||
const addEventListener = jest.spyOn(window, 'addEventListener'); | ||
|
||
announceProvider(providerDetail); | ||
requestProvider(handleProvider); | ||
await delay(); | ||
|
||
// Notice that 3 events are dispatched in total when requestProvider is | ||
// called after announceProvider. | ||
expect(dispatchEvent).toHaveBeenCalledTimes(3); | ||
expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
1, | ||
new CustomEvent('eip6963:announceProvider'), | ||
); | ||
expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
2, | ||
new Event('eip6963:requestProvider'), | ||
); | ||
expect(dispatchEvent).toHaveBeenNthCalledWith( | ||
3, | ||
new CustomEvent('eip6963:announceProvider'), | ||
); | ||
jiexi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expect(addEventListener).toHaveBeenCalledTimes(2); | ||
expect(addEventListener).toHaveBeenCalledWith( | ||
'eip6963:announceProvider', | ||
expect.any(Function), | ||
); | ||
expect(addEventListener).toHaveBeenCalledWith( | ||
'eip6963:requestProvider', | ||
expect.any(Function), | ||
); | ||
|
||
expect(handleProvider).toHaveBeenCalledTimes(1); | ||
expect(handleProvider).toHaveBeenCalledWith({ | ||
info: getProviderInfo(), | ||
provider, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Delay for a number of milliseconds by awaiting a promise | ||
* resolved after the specified number of milliseconds. | ||
* | ||
* @param ms - The number of milliseconds to delay for. | ||
*/ | ||
async function delay(ms = 1) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely need to ignore
jest/require-top-level-describe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you feel it's worthwhile to add jsdoc to the rest? I can go either way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, this was me btw. It seems strictly not worth it, as I have a hard time imagining a lot of work being done in that file, but not my call of course!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with this, just thought it was funny :)