UNPKG

1.26 kBJavaScriptView Raw
1// Copyright 2017-2018 @polkadot/api-provider authors & contributors
2// This software may be modified and distributed under the terms
3// of the ISC license. See the LICENSE file for details.
4
5import createCoder from './index';
6
7describe('decodeResponse', () => {
8 let coder;
9
10 beforeEach(() => {
11 coder = createCoder();
12 });
13
14 it('expects a non-empty input object', () => {
15 expect(
16 () => coder.decodeResponse()
17 ).toThrow(/Empty response/);
18 });
19
20 it('expects a valid jsonrpc field', () => {
21 expect(
22 () => coder.decodeResponse({})
23 ).toThrow(/Invalid jsonrpc/);
24 });
25
26 it('expects a valid id field', () => {
27 expect(
28 () => coder.decodeResponse({ jsonrpc: '2.0' })
29 ).toThrow(/Invalid id/);
30 });
31
32 it('expects a valid result field', () => {
33 expect(
34 () => coder.decodeResponse({ id: 1, jsonrpc: '2.0' })
35 ).toThrow(/No result/);
36 });
37
38 it('throws any error found', () => {
39 expect(
40 () => coder.decodeResponse({ id: 1, jsonrpc: '2.0', error: { code: 123, message: 'test error' } })
41 ).toThrow(/\[123\]: test error/);
42 });
43
44 it('returns the result', () => {
45 expect(
46 coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' })
47 ).toEqual('some result');
48 });
49});