Skip to content

Commit

Permalink
update specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jiexi committed Jan 27, 2025
1 parent 9ea43ee commit 2ab37dd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 75 deletions.
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const baseConfig = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 66.79,
functions: 68.69,
lines: 68.35,
statements: 68.38,
branches: 63.88,
functions: 66.08,
lines: 68.39,
statements: 66.46,
},
},

Expand Down
51 changes: 16 additions & 35 deletions src/MetaMaskInpageProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ describe('MetaMaskInpageProvider: RPC', () => {
});
});

it('handles chain changes with intermittent disconnection', async () => {
it('handles chain changes when the wallet is unable to resolve networkVersion', async () => {
const { provider, connectionStream } = await getInitializedProvider();

// We check this mostly for the readability of this test.
Expand All @@ -787,52 +787,33 @@ describe('MetaMaskInpageProvider: RPC', () => {

const emitSpy = jest.spyOn(provider, 'emit');

await new Promise<void>((resolve) => {
provider.once('disconnect', (error) => {
expect((error as any).code).toBe(1013);
resolve();
await new Promise<void>((resolve, reject) => {
provider.once('disconnect', () => {
reject();

Check failure on line 792 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Expected the Promise rejection reason to be an Error
});

connectionStream.notify(MetaMaskInpageProviderStreamName, {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
// A "loading" networkVersion indicates the network is changing.
// Although the chainId is different, chainChanged should not be
// emitted in this case.
params: { chainId: '0x1', networkVersion: 'loading' },
});
});

// Only once, for "disconnect".
expect(emitSpy).toHaveBeenCalledTimes(1);
emitSpy.mockClear(); // Clear the mock to avoid keeping a count.

expect(provider.isConnected()).toBe(false);
// These should be unchanged.
expect(provider.chainId).toBe('0x0');
expect(provider.networkVersion).toBe('0');

await new Promise<void>((resolve) => {
provider.once('chainChanged', (newChainId) => {
expect(newChainId).toBe('0x1');
resolve();
});
provider.once('chainChanged', (chainId) => {
expect(chainId).toStrictEqual('0x1')

Check failure on line 796 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Use `toBe` when expecting primitive literals

Check failure on line 796 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Insert `;`
resolve()

Check failure on line 797 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Insert `;`
})

Check failure on line 798 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Insert `;`

connectionStream.notify(MetaMaskInpageProviderStreamName, {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
params: { chainId: '0x1', networkVersion: '1' },
// A null networkVersion indicates that the network version could not
// be determined for the network. The chainChanged event should still be
// emitted in this case.
params: { chainId: '0x1', networkVersion: null },
});
});

expect(emitSpy).toHaveBeenCalledTimes(3);
expect(emitSpy).toHaveBeenNthCalledWith(1, 'connect', { chainId: '0x1' });
expect(emitSpy).toHaveBeenCalledWith('chainChanged', '0x1');
expect(emitSpy).toHaveBeenCalledWith('networkChanged', '1');
expect(emitSpy).toHaveBeenCalledTimes(2);
expect(emitSpy).toHaveBeenCalledWith('chainChanged', '0x1')

Check failure on line 811 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Insert `;`
expect(emitSpy).toHaveBeenCalledWith('networkChanged', null)

Check failure on line 812 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Insert `;`

expect(provider.isConnected()).toBe(true);
expect(provider.chainId).toBe('0x1');
expect(provider.networkVersion).toBe('1');
expect(provider.networkVersion).toBe(null);

Check failure on line 816 in src/MetaMaskInpageProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Use `toBeNull` instead
});
});

Expand Down
50 changes: 14 additions & 36 deletions src/StreamProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('StreamProvider', () => {
});
});

it('handles chain changes with intermittent disconnection', async () => {
it('handles chain changes when the wallet is unable to resolve networkVersion', async () => {
const mockStream = new MockConnectionStream();
const mux = new ObjectMultiplex();
pipeline(mockStream, mux, mockStream, (error: Error | null) => {
Expand Down Expand Up @@ -434,50 +434,28 @@ describe('StreamProvider', () => {

const emitSpy = jest.spyOn(streamProvider, 'emit');

await new Promise<void>((resolve) => {
streamProvider.once('disconnect', (error) => {
expect(error.code).toBe(1013);
resolve();
});

mockStream.notify(mockStreamName, {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
// A "loading" networkVersion indicates the network is changing.
// Although the chainId is different, chainChanged should not be
// emitted in this case.
params: { chainId: '0x1', networkVersion: 'loading' },
await new Promise<void>((resolve, reject) => {
streamProvider.once('disconnect', () => {
reject();

Check failure on line 439 in src/StreamProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Expected the Promise rejection reason to be an Error
});
});

// Only once, for "disconnect".
expect(emitSpy).toHaveBeenCalledTimes(1);
emitSpy.mockClear(); // Clear the mock to avoid keeping a count.

expect(streamProvider.isConnected()).toBe(false);
// These should be unchanged.
expect(streamProvider.chainId).toBe('0x0');

await new Promise<void>((resolve) => {
streamProvider.once('chainChanged', (newChainId) => {
expect(newChainId).toBe('0x1');
resolve();
});
streamProvider.once('chainChanged', (chainId) => {
expect(chainId).toStrictEqual('0x1')

Check failure on line 443 in src/StreamProvider.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Use `toBe` when expecting primitive literals
resolve()
})

mockStream.notify(mockStreamName, {
jsonrpc: '2.0',
method: 'metamask_chainChanged',
// The networkVersion will be ignored here, we're just setting it
// to something other than 'loading'.
params: { chainId: '0x1', networkVersion: '1' },
// A null networkVersion indicates that the network version could not
// be determined for the network. The chainChanged event should still be
// emitted in this case.
params: { chainId: '0x1', networkVersion: null },
});
});

expect(emitSpy).toHaveBeenCalledTimes(2);
expect(emitSpy).toHaveBeenNthCalledWith(1, 'connect', {
chainId: '0x1',
});
expect(emitSpy).toHaveBeenCalledWith('chainChanged', '0x1');
expect(emitSpy).toHaveBeenCalledTimes(1);
expect(emitSpy).toHaveBeenCalledWith('chainChanged', '0x1')

expect(streamProvider.isConnected()).toBe(true);
expect(streamProvider.chainId).toBe('0x1');
Expand Down

0 comments on commit 2ab37dd

Please sign in to comment.