|
| 1 | +import { INestApplication } from '@nestjs/common'; |
| 2 | +import { |
| 3 | + ResultRpcFactoryPromise, |
| 4 | + ErrorCodeType, |
| 5 | + RpcError, |
| 6 | +} from '@klerick/nestjs-json-rpc-sdk'; |
| 7 | + |
| 8 | +import { creatWsRpcSdk, MapperRpc, run } from '../utils/run-application'; |
| 9 | + |
| 10 | +let app: INestApplication; |
| 11 | + |
| 12 | +beforeAll(async () => { |
| 13 | + app = await run(); |
| 14 | +}); |
| 15 | + |
| 16 | +afterAll(async () => { |
| 17 | + await app.close(); |
| 18 | +}); |
| 19 | + |
| 20 | +describe('Run ws json rpc:', () => { |
| 21 | + let rpc: ResultRpcFactoryPromise<MapperRpc>['rpc']; |
| 22 | + let rpcBatch: ResultRpcFactoryPromise<MapperRpc>['rpcBatch']; |
| 23 | + let rpcForBatch: ResultRpcFactoryPromise<MapperRpc>['rpcForBatch']; |
| 24 | + beforeEach(() => { |
| 25 | + ({ rpc, rpcBatch, rpcForBatch } = creatWsRpcSdk()); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('Should be correct response', () => { |
| 29 | + it('Should be call one method', async () => { |
| 30 | + const input = 1; |
| 31 | + const result = await rpc.RpcService.someMethode(input); |
| 32 | + expect(result).toBe(input); |
| 33 | + }); |
| 34 | + |
| 35 | + it('Should be correct response batch', async () => { |
| 36 | + const input = 1; |
| 37 | + const input2 = { |
| 38 | + a: 1, |
| 39 | + b: 2, |
| 40 | + }; |
| 41 | + const call1 = rpcForBatch.RpcService.someMethode(input); |
| 42 | + const call2 = rpcForBatch.RpcService.methodeWithObjectParams(input2); |
| 43 | + |
| 44 | + const [result1, result2] = await rpcBatch(call1, call2); |
| 45 | + expect(result1).toBe(input); |
| 46 | + if ('error' in result2) { |
| 47 | + throw Error('Return error'); |
| 48 | + } |
| 49 | + expect(result2.d).toEqual(`${input2.a}`); |
| 50 | + expect(result2.c).toEqual(`${input2.b}`); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('Check error', () => { |
| 55 | + it('Should throw an error ' + ErrorCodeType.MethodNotFound, async () => { |
| 56 | + const input = 1; |
| 57 | + expect.assertions(6); |
| 58 | + try { |
| 59 | + // @ts-ignore |
| 60 | + await rpc.IncorrectService.incorrectMethode(input); |
| 61 | + } catch (e) { |
| 62 | + expect(e).toBeInstanceOf(RpcError); |
| 63 | + expect((e as RpcError).code).toBe(-32601); |
| 64 | + expect((e as RpcError).message).toBe(ErrorCodeType.MethodNotFound); |
| 65 | + } |
| 66 | + try { |
| 67 | + // @ts-ignore |
| 68 | + await rpc.RpcService.incorrectMethode(input); |
| 69 | + } catch (e) { |
| 70 | + expect(e).toBeInstanceOf(RpcError); |
| 71 | + expect((e as RpcError).code).toBe(-32601); |
| 72 | + expect((e as RpcError).message).toBe(ErrorCodeType.MethodNotFound); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it('Should throw an error ' + ErrorCodeType.InvalidParams, async () => { |
| 77 | + const input = 'llll'; |
| 78 | + expect.assertions(3); |
| 79 | + try { |
| 80 | + // @ts-ignore |
| 81 | + await rpc.RpcService.someMethode(input); |
| 82 | + } catch (e) { |
| 83 | + expect(e).toBeInstanceOf(RpcError); |
| 84 | + expect((e as RpcError).code).toBe(-32602); |
| 85 | + expect((e as RpcError).message).toBe(ErrorCodeType.InvalidParams); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it('Should throw an error ' + ErrorCodeType.ServerError, async () => { |
| 90 | + const input = 5; |
| 91 | + expect.assertions(4); |
| 92 | + try { |
| 93 | + await rpc.RpcService.someMethode(input); |
| 94 | + } catch (e) { |
| 95 | + expect(e).toBeInstanceOf(RpcError); |
| 96 | + expect((e as RpcError).code).toBe(-32099); |
| 97 | + expect((e as RpcError).message).toBe(ErrorCodeType.ServerError); |
| 98 | + expect((e as RpcError).data.title).toBe('Custom Error'); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments