1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00
metamask-extension/app/scripts/lib/createMetaRPCHandler.test.js
Mark Stacey 5076e5057f
Remove nodeify (#13003)
The `nodeify` utility is no longer needed for the background API.
Instead each method is assumed to be either synchronous or Promise-
returning.

The error handling was updated to at least log the error in the case
where a method fall fails after the connection is broken.
2021-12-08 18:06:53 -03:30

116 lines
3.0 KiB
JavaScript

import { obj as createThoughStream } from 'through2';
import createMetaRPCHandler from './createMetaRPCHandler';
describe('createMetaRPCHandler', () => {
it('can call the api when handler receives a JSON-RPC request', () => {
const api = {
foo: (param1) => {
expect(param1).toStrictEqual('bar');
},
};
const streamTest = createThoughStream();
const handler = createMetaRPCHandler(api, streamTest);
handler({
id: 1,
method: 'foo',
params: ['bar'],
});
});
it('can write the response to the outstream', () => {
const api = {
foo: (param1) => {
expect(param1).toStrictEqual('bar');
return 'foobarbaz';
},
};
const streamTest = createThoughStream();
const handler = createMetaRPCHandler(api, streamTest);
handler({
id: 1,
method: 'foo',
params: ['bar'],
});
streamTest.on('data', (data) => {
expect(data.result).toStrictEqual('foobarbaz');
streamTest.end();
});
});
it('can write an async response to the outstream', () => {
const api = {
foo: async (param1) => {
expect(param1).toStrictEqual('bar');
await new Promise((resolve) => setTimeout(() => resolve(), 100));
return 'foobarbaz';
},
};
const streamTest = createThoughStream();
const handler = createMetaRPCHandler(api, streamTest);
handler({
id: 1,
method: 'foo',
params: ['bar'],
});
streamTest.on('data', (data) => {
expect(data.result).toStrictEqual('foobarbaz');
streamTest.end();
});
});
it('can write the error to the outstream when method throws an error', () => {
const api = {
foo: (param1) => {
expect(param1).toStrictEqual('bar');
throw new Error('foo-error');
},
};
const streamTest = createThoughStream();
const handler = createMetaRPCHandler(api, streamTest);
handler({
id: 1,
method: 'foo',
params: ['bar'],
});
streamTest.on('data', (data) => {
expect(data.error.message).toStrictEqual('foo-error');
streamTest.end();
});
});
it('can not throw an error for writing an error after end', () => {
const api = {
foo: (param1) => {
expect(param1).toStrictEqual('bar');
throw new Error('foo-error');
},
};
const streamTest = createThoughStream();
const handler = createMetaRPCHandler(api, streamTest);
streamTest.end();
expect(() => {
handler({
id: 1,
method: 'foo',
params: ['bar'],
});
}).not.toThrow();
});
it('can not throw an error for write after end', () => {
const api = {
foo: (param1) => {
expect(param1).toStrictEqual('bar');
return {
foo: 'bar',
};
},
};
const streamTest = createThoughStream();
const handler = createMetaRPCHandler(api, streamTest);
streamTest.end();
expect(() => {
handler({
id: 1,
method: 'foo',
params: ['bar'],
});
}).not.toThrow();
});
});