* Pull api-* packages into lerna repo * Bootstrap with hoist * Update jest alias config * Add api-jsonrpc * Pull in jsonrpc * Update badges * Don't hoist, use workspaces * .eslintrc to root * Cleanups, build from root * Shorthand commands * Update to @polkadot/dev 0.9.1 * Update description
78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
// ISC, Copyright 2017 Jaco Greeff
|
|
|
|
const JsonRpcCoder = require('./jsonRpcCoder');
|
|
|
|
describe('JsonRpcCoder', () => {
|
|
let coder;
|
|
|
|
beforeEach(() => {
|
|
coder = new JsonRpcCoder();
|
|
});
|
|
|
|
describe('id', () => {
|
|
it('starts with id === 0 (nothing sent)', () => {
|
|
expect(coder.id).toEqual(0);
|
|
});
|
|
});
|
|
|
|
describe('decodeResponse', () => {
|
|
it('expects a non-empty input object', () => {
|
|
expect(
|
|
() => coder.decodeResponse()
|
|
).toThrow(/Empty response/);
|
|
});
|
|
|
|
it('expects a valid jsonrpc field', () => {
|
|
expect(
|
|
() => coder.decodeResponse({})
|
|
).toThrow(/Invalid jsonrpc/);
|
|
});
|
|
|
|
it('expects a valid id field', () => {
|
|
expect(
|
|
() => coder.decodeResponse({ jsonrpc: '2.0' })
|
|
).toThrow(/Invalid id/);
|
|
});
|
|
|
|
it('expects a valid result field', () => {
|
|
expect(
|
|
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' })
|
|
).toThrow(/No result/);
|
|
});
|
|
|
|
it('throws any error found', () => {
|
|
expect(
|
|
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0', error: { code: 123, message: 'test error' } })
|
|
).toThrow(/\[123\]: test error/);
|
|
});
|
|
|
|
it('returns the result', () => {
|
|
expect(
|
|
coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' })
|
|
).toEqual('some result');
|
|
});
|
|
});
|
|
|
|
describe('encodeObject', () => {
|
|
it('encodes a valid JsonRPC object', () => {
|
|
expect(
|
|
coder.encodeObject('method', 'params')
|
|
).toEqual({
|
|
id: 1,
|
|
jsonrpc: '2.0',
|
|
method: 'method',
|
|
params: 'params'
|
|
});
|
|
expect(coder.id).toEqual(1);
|
|
});
|
|
});
|
|
|
|
describe('encodeJson', () => {
|
|
it('encodes a valid JsonRPC JSON string', () => {
|
|
expect(
|
|
coder.encodeJson('method', 'params')
|
|
).toEqual('{"id":1,"jsonrpc":"2.0","method":"method","params":"params"}');
|
|
});
|
|
});
|
|
});
|