Skip to content

Commit

Permalink
test: type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Sep 1, 2023
1 parent ea3143c commit 71d4a44
Showing 1 changed file with 97 additions and 62 deletions.
159 changes: 97 additions & 62 deletions src/middleware/createRpcWarningMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { Json, JsonRpcFailure, JsonRpcSuccess } from '@metamask/utils';
import {
Json,
JsonRpcFailure,
JsonRpcParams,
JsonRpcRequest,
JsonRpcSuccess,
} from '@metamask/utils';

import { createRpcWarningMiddleware } from './createRpcWarningMiddleware';
import messages from '../messages';

const affected = [
type Scenario = {
scenario: string;
method: string;
warning?: string;
params?: JsonRpcParams;
};

const affected: Scenario[] = [
{
scenario: 'eth_decrypt',
method: 'eth_decrypt',
Expand Down Expand Up @@ -35,7 +48,7 @@ const affected = [
},
];

const unaffected = [
const unaffected: Scenario[] = [
{
scenario: 'eth_chainId',
method: 'eth_chainId',
Expand All @@ -51,67 +64,84 @@ const unaffected = [
];

describe('createRpcWarningMiddleware', () => {
describe.each(affected)('$scenario', ({ method, params = {}, warning }) => {
it('should warn the first time the method is called', async () => {
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);

await engine.handle({ jsonrpc: '2.0', id: 1, method, params });

expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
});

it('should not warn the second time the method is called', async () => {
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);

await engine.handle({ jsonrpc: '2.0', id: 1, method, params });
await engine.handle({ jsonrpc: '2.0', id: 1, method, params });

expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
});

it('should allow the method to succeed', async () => {
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);
engine.push((_req, res, _next, end) => {
res.result = 'success!';
end();
describe.each(affected)(
'$scenario',
({ method, params = {}, warning }: Scenario) => {
it('should warn the first time the method is called', async () => {
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);

await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
params,
} as JsonRpcRequest);
expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
});

const response = (await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
})) as JsonRpcSuccess<Json>;

expect(response.result).toBe('success!');
});

it('should allow the method to fail', async () => {
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);
engine.push(() => {
throw new Error('Failure!');
it('should not warn the second time the method is called', async () => {
const consoleWarnSpy = jest.spyOn(globalThis.console, 'warn');
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);

await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
params,
} as JsonRpcRequest);
await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
params,
} as JsonRpcRequest);

expect(consoleWarnSpy).toHaveBeenCalledWith(warning);
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
});

const result = (await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
})) as JsonRpcFailure;
it('should allow the method to succeed', async () => {
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);
engine.push((_req, res, _next, end) => {
res.result = 'success!';
end();
});

const response = (await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
})) as JsonRpcSuccess<Json>;

expect(response.result).toBe('success!');
});

expect(result.error.message).toBe('Internal JSON-RPC error.');
});
});
it('should allow the method to fail', async () => {
const middleware = createRpcWarningMiddleware(globalThis.console);
const engine = new JsonRpcEngine();
engine.push(middleware);
engine.push(() => {
throw new Error('Failure!');
});

const result = (await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
})) as JsonRpcFailure;

expect(result.error.message).toBe('Internal JSON-RPC error.');
});
},
);

describe.each(unaffected)('$scenario', ({ method, params = {} }) => {
it('should not issue a warning', async () => {
Expand All @@ -120,7 +150,12 @@ describe('createRpcWarningMiddleware', () => {
const engine = new JsonRpcEngine();
engine.push(middleware);

await engine.handle({ jsonrpc: '2.0', id: 1, method, params });
await engine.handle({
jsonrpc: '2.0',
id: 1,
method,
params,
} as JsonRpcRequest);

expect(consoleWarnSpy).not.toHaveBeenCalled();
});
Expand All @@ -139,7 +174,7 @@ describe('createRpcWarningMiddleware', () => {
id: 1,
method,
params,
})) as JsonRpcSuccess<Json>;
} as JsonRpcRequest)) as JsonRpcSuccess<Json>;

expect(response.result).toBe('success!');
});
Expand All @@ -157,7 +192,7 @@ describe('createRpcWarningMiddleware', () => {
id: 1,
method,
params,
})) as JsonRpcFailure;
} as JsonRpcRequest)) as JsonRpcFailure;

expect(result.error.message).toBe('Internal JSON-RPC error.');
});
Expand Down

0 comments on commit 71d4a44

Please sign in to comment.