remote-electron-store.spec.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2017-2020 @polkadot/apps authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { RemoteElectronStore } from './remote-electron-store';
  5. describe('Remote Electron Store', () => {
  6. const accountStore = {
  7. all: jest.fn(),
  8. get: jest.fn(),
  9. remove: jest.fn(),
  10. set: jest.fn()
  11. };
  12. const remoteStore = new RemoteElectronStore(accountStore);
  13. beforeEach(() => {
  14. accountStore.all.mockClear();
  15. accountStore.get.mockClear();
  16. accountStore.remove.mockClear();
  17. accountStore.set.mockClear();
  18. });
  19. describe('all', () => {
  20. it('calls callback for each returned account', async () => {
  21. accountStore.all.mockResolvedValue([{
  22. key: 1,
  23. value: 'a'
  24. }, {
  25. key: 2,
  26. value: 'b'
  27. }]);
  28. const cb = jest.fn();
  29. remoteStore.all(cb);
  30. await Promise.resolve();
  31. expect(cb).nthCalledWith(1, 1, 'a');
  32. expect(cb).nthCalledWith(2, 2, 'b');
  33. });
  34. });
  35. describe('get', () => {
  36. it('calls callback with returned account', async () => {
  37. accountStore.get.mockResolvedValue('a');
  38. const cb = jest.fn();
  39. remoteStore.get('1', cb);
  40. await Promise.resolve();
  41. expect(accountStore.get).toBeCalledWith('1');
  42. expect(cb).toBeCalledWith('a');
  43. });
  44. it('calls callback with null if no accounts found', async () => {
  45. accountStore.get.mockResolvedValue(null);
  46. const cb = jest.fn();
  47. remoteStore.get('1', cb);
  48. await Promise.resolve();
  49. expect(cb).toBeCalledWith(null);
  50. });
  51. });
  52. describe('remove', () => {
  53. it('calls callback after success', async () => {
  54. accountStore.remove.mockResolvedValue(null);
  55. const cb = jest.fn();
  56. remoteStore.remove('1', cb);
  57. await Promise.resolve();
  58. expect(accountStore.remove).toBeCalledWith('1');
  59. expect(cb).toBeCalledTimes(1);
  60. });
  61. });
  62. describe('set', () => {
  63. it('calls callback after success', async () => {
  64. accountStore.set.mockResolvedValue(null);
  65. const cb = jest.fn();
  66. remoteStore.set('1', 'a' as any, cb);
  67. await Promise.resolve();
  68. expect(accountStore.set).toBeCalledWith('1', 'a');
  69. expect(cb).toBeCalledTimes(1);
  70. });
  71. });
  72. });