Skip to content

Commit

Permalink
fix: pr fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed May 12, 2021
1 parent 89037b7 commit cdab561
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 82 deletions.
34 changes: 18 additions & 16 deletions src/BaseProvider.rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,25 @@ describe('BaseProvider: RPC', () => {
});
});
describe('provider events', () => {
// eslint-disable-next-line jest/no-test-callback
it('calls chainChanged when it chainId changes ', (done) => {

it('calls chainChanged when it chainId changes ', async () => {
const mockStream = new MockDuplexStream();
const p = new BaseProvider(mockStream);
(p as any)._state.initialized = true;
p.on('chainChanged', (changed) => {
expect(changed).toBeDefined();
done();
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1' },
},
});
const baseProvider = new BaseProvider(mockStream);
(baseProvider as any)._state.initialized = true;
await new Promise((resolve) => {
baseProvider.on('chainChanged', (changed) => {
expect(changed).toBeDefined();
resolve(undefined);
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
});
})
});
});
});
25 changes: 18 additions & 7 deletions src/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,32 @@ export default class BaseProvider extends SafeEventEmitter {
*/
protected _handleChainChanged({
chainId,
networkVersion,
}: { chainId?: string; networkVersion?: string } = {}) {
if (!chainId || typeof chainId !== 'string' || !chainId.startsWith('0x')) {
if (
!chainId ||
typeof chainId !== 'string' ||
!chainId.startsWith('0x') ||
!networkVersion ||
typeof networkVersion !== 'string'
) {
this._log.error(
'MetaMask: Received invalid network parameters. Please report this bug.',
{ chainId },
{ chainId, networkVersion },
);
return;
}

this._handleConnect(chainId);
if (networkVersion === 'loading') {
this._handleDisconnect(true);
} else {
this._handleConnect(chainId);

if (chainId !== this.chainId) {
this.chainId = chainId;
if (this._state.initialized) {
this.emit('chainChanged', this.chainId);
if (chainId !== this.chainId) {
this.chainId = chainId;
if (this._state.initialized) {
this.emit('chainChanged', this.chainId);
}
}
}
}
Expand Down
64 changes: 34 additions & 30 deletions src/MetaMaskInpageProvider.rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,41 +636,45 @@ describe('MetaMaskInpageProvider: RPC', () => {
);
});
});

describe('provider events', () => {
// eslint-disable-next-line jest/no-test-callback
it('calls chainChanged when it chainId changes ', (done) => {
it('calls chainChanged when it chainId changes ', async () => {
const mockStream = new MockDuplexStream();
const p = new MetaMaskInpageProvider(mockStream);
(p as any)._state.initialized = true;
p.on('chainChanged', (changed) => {
expect(changed).toBe('0x1');
done();
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
const inpageProvider = new MetaMaskInpageProvider(mockStream);
(inpageProvider as any)._state.initialized = true;
await new Promise((resolve) => {
inpageProvider.on('chainChanged', (changed) => {
expect(changed).toBe('0x1');
resolve(undefined);
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
});
});
});
// eslint-disable-next-line jest/no-test-callback
it('calls networkChanged when it networkVersion changes ', (done) => {

it('calls networkChanged when it networkVersion changes ', async () => {
const mockStream = new MockDuplexStream();
const p = new MetaMaskInpageProvider(mockStream);
(p as any)._state.initialized = true;
p.on('networkChanged', (changed) => {
expect(changed).toBe('0x1');
done();
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
const inpageProvider = new MetaMaskInpageProvider(mockStream);
(inpageProvider as any)._state.initialized = true;
await new Promise((resolve) => {
inpageProvider.on('networkChanged', (changed) => {
expect(changed).toBe('0x1');
resolve(undefined);
});
mockStream.push({
name: 'metamask-provider',
data: {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '0x1' },
},
});
});
});
});
Expand Down
37 changes: 8 additions & 29 deletions src/MetaMaskInpageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,37 +412,16 @@ export default class MetaMaskInpageProvider extends BaseProvider {
chainId,
networkVersion,
}: { chainId?: string; networkVersion?: string } = {}) {
super._handleChainChanged({ chainId, networkVersion });

if (
!chainId ||
typeof chainId !== 'string' ||
!chainId.startsWith('0x') ||
!networkVersion ||
typeof networkVersion !== 'string'
networkVersion &&
networkVersion !== 'loading' &&
networkVersion !== this.networkVersion
) {
this._log.error(
'MetaMask: Received invalid network parameters. Please report this bug.',
{ chainId, networkVersion },
);
return;
}

if (networkVersion === 'loading') {
this._handleDisconnect(true);
} else {
this._handleConnect(chainId);

if (chainId !== this.chainId) {
this.chainId = chainId;
if (this._state.initialized) {
this.emit('chainChanged', this.chainId);
}
}

if (networkVersion !== this.networkVersion) {
this.networkVersion = networkVersion;
if (this._state.initialized) {
this.emit('networkChanged', this.networkVersion);
}
this.networkVersion = networkVersion;
if (this._state.initialized) {
this.emit('networkChanged', this.networkVersion);
}
}
}
Expand Down

0 comments on commit cdab561

Please sign in to comment.