Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { inspect, isDeepStrictEqual, promisify } from 'util';
import { v4 } from 'uuid';

import { FakeBlockTracker } from '../../../tests/fake-block-tracker';
import type { FakeProviderStub } from '../../../tests/fake-provider';
import { FakeProvider } from '../../../tests/fake-provider';
import { NetworkStatus } from '../src/constants';
import type { NetworkClient } from '../src/create-network-client';
import { createNetworkClient } from '../src/create-network-client';
Expand All @@ -29,8 +31,6 @@ import type {
import { NetworkController } from '../src/NetworkController';
import type { Provider } from '../src/types';
import { NetworkClientType } from '../src/types';
import type { FakeProviderStub } from './fake-provider';
import { FakeProvider } from './fake-provider';

jest.mock('../src/create-network-client');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,52 +96,52 @@ export class FakeProvider extends SafeEventEmitterProvider {
* @param options.stubs - A set of objects that allow specifying the behavior
* of specific invocations of `sendAsync` matching a `method`.
*/
constructor({ stubs = [] }: FakeProviderEngineOptions) {
constructor({ stubs = [] }: FakeProviderEngineOptions = {}) {
super({ engine: new JsonRpcEngine() });
this.#originalStubs = stubs;
this.#stubs = this.#originalStubs.slice();
this.calledStubs = [];
}

send = (
payload: JsonRpcRequest<any>,
callback: (error: unknown, response?: JsonRpcResponse<any>) => void,
sendAsync = (
payload: JsonRpcRequest,
callback: (error: unknown, providerRes?: any) => void,
) => {
return this.#handleSend(payload, callback);
};

sendAsync = (
payload: JsonRpcRequest<any>,
callback: (error: unknown, response?: JsonRpcResponse<any>) => void,
send = (
req: JsonRpcRequest,
callback: (error: unknown, providerRes?: any) => void,
) => {
return this.#handleSend(payload, callback);
return this.#handleSend(req, callback);
};

#handleSend(
payload: JsonRpcRequest<any>,
callback: (error: unknown, response?: JsonRpcResponse<any>) => void,
req: JsonRpcRequest,
callback: (error: unknown, providerRes?: any) => void,
) {
if (Array.isArray(payload)) {
if (Array.isArray(req)) {
throw new Error("Arrays aren't supported");
}

const index = this.#stubs.findIndex((stub) => {
return (
stub.request.method === payload.method &&
stub.request.method === req.method &&
(!('params' in stub.request) ||
isDeepStrictEqual(stub.request.params, payload.params))
isDeepStrictEqual(stub.request.params, req.params))
);
});

if (index === -1) {
const matchingCalledStubs = this.calledStubs.filter((stub) => {
return (
stub.request.method === payload.method &&
stub.request.method === req.method &&
(!('params' in stub.request) ||
isDeepStrictEqual(stub.request.params, payload.params))
isDeepStrictEqual(stub.request.params, req.params))
);
});
let message = `Could not find any stubs matching: ${inspect(payload, {
let message = `Could not find any stubs matching: ${inspect(req, {
depth: null,
})}`;
if (matchingCalledStubs.length > 0) {
Expand All @@ -153,7 +153,9 @@ export class FakeProvider extends SafeEventEmitterProvider {

throw new Error(message);
} else {
const stub = this.#stubs[index];
// We are already checking that this stub exists above.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const stub = this.#stubs[index]!;

if (stub.discardAfterMatching !== false) {
this.#stubs.splice(index, 1);
Expand Down