index.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import nock from 'nock';
  2. import { BankClient } from '../src/index';
  3. import { IWebClient } from '../src/webclient';
  4. import WebClientNode from '../src/webclient-node';
  5. import { Storage } from './../src/storage';
  6. import { IWebClientOptions } from './../src/webclient-options';
  7. describe('BankClient', () => {
  8. const storage = new Storage('bankConfig');
  9. test('should successfully bootstrap', async () => {
  10. class FakeWebClient implements IWebClient {
  11. public requestJSON(options: object): Promise<object> {
  12. throw new Error("Method not implemented.");
  13. }
  14. public request(options: IWebClientOptions): Promise<string> {
  15. throw new Error("Method not implemented.");
  16. }
  17. }
  18. const bankClient = new BankClient('/urlBase', storage, new FakeWebClient());
  19. const strapped = await bankClient.bootstrap();
  20. expect(strapped).toBeDefined();
  21. });
  22. test('should return hardcoded nonce', async () => {
  23. class FakeWebClient implements IWebClient {
  24. public requestJSON(options: object): Promise<object> {
  25. throw new Error("Method not implemented.");
  26. }
  27. public request(options: IWebClientOptions): Promise<string> {
  28. return Promise.resolve('42');
  29. }
  30. }
  31. const bankClient = new BankClient('/urlBase', storage, new FakeWebClient());
  32. await bankClient.bootstrap();
  33. const nonce = await bankClient.getNonce();
  34. expect(nonce).toEqual(42);
  35. });
  36. describe('get nonce', () => {
  37. test('should work with real http client', async () => {
  38. const webClient = new WebClientNode();
  39. const bankClient = new BankClient('http://127.0.0.1:8000', storage, webClient);
  40. await bankClient.bootstrap();
  41. nock('http://127.0.0.1:8000')
  42. .get('/bank/nonce')
  43. .reply(200, "42");
  44. const nonce = await bankClient.getNonce();
  45. expect(nonce).toEqual(42);
  46. });
  47. });
  48. describe('get balance', () => {
  49. test('should work with real http client', async () => {
  50. const webClient = new WebClientNode();
  51. const bankClient = new BankClient('http://127.0.0.1:8000', storage, webClient);
  52. await bankClient.bootstrap();
  53. nock('http://127.0.0.1:8000')
  54. .post('/bank/getbalance')
  55. .reply(200, {
  56. what: 'true'
  57. });
  58. nock('http://127.0.0.1:8000')
  59. .get('/bank/nonce')
  60. .reply(200, "42");
  61. const balance = await bankClient.getBalance();
  62. expect(balance).toEqual({what: 'true'});
  63. });
  64. });
  65. });