1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/lib/createMetaRPCHandler.test.js
Thomas Huang d78cfebd43
Jestify app/scripts/lib/**/*.test.js (#12890)
* Jestify app/scripts/lib/*.test.js

* Mocha config on mocha test script

* Add app/scripts/lib to include subdirs
2021-12-06 10:40:39 -06:00

96 lines
2.5 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 when api callback is called', () => {
const api = {
foo: (param1, cb) => {
expect(param1).toStrictEqual('bar');
cb(null, '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 api callback is called with an error', () => {
const api = {
foo: (param1, cb) => {
expect(param1).toStrictEqual('bar');
cb(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, cb) => {
expect(param1).toStrictEqual('bar');
cb(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, cb) => {
expect(param1).toStrictEqual('bar');
cb(undefined, {
foo: 'bar',
});
},
};
const streamTest = createThoughStream();
const handler = createMetaRPCHandler(api, streamTest);
streamTest.end();
expect(() => {
handler({
id: 1,
method: 'foo',
params: ['bar'],
});
}).not.toThrow();
});
});