From 9117004a0ffa289c1ea47377fe192fe578200dc1 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Tue, 4 Sep 2018 14:14:17 +0200 Subject: [PATCH 1/2] fix(grpc): ensure connection errors show onscreen Fix an issue that was preventing grpc connection errors from showing after a failed connection to a remote node. Re-throw the error from `startLightningWallet` after logging it so that it can be handled properly afterwards. --- app/lib/zap/controller.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/lib/zap/controller.js b/app/lib/zap/controller.js index 794f867a..2387f280 100644 --- a/app/lib/zap/controller.js +++ b/app/lib/zap/controller.js @@ -284,6 +284,7 @@ class ZapController { this.sendMessage('lightningGrpcActive') } catch (err) { mainLog.warn('Unable to connect to Lighitnng gRPC interface: %o', err) + throw err } } From e8dd544bca75903abd1e519f6523deed7b21bd6b Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Tue, 4 Sep 2018 16:32:14 +0200 Subject: [PATCH 2/2] test(unit): test Controller.startLightningWallet Add unit tests to verify that ZapController.startLightningWallet() behaves as expected. --- test/unit/__mocks__/electron.js | 5 ++- test/unit/zap/controller.spec.js | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/unit/zap/controller.spec.js diff --git a/test/unit/__mocks__/electron.js b/test/unit/__mocks__/electron.js index e4cb4e68..1840a834 100644 --- a/test/unit/__mocks__/electron.js +++ b/test/unit/__mocks__/electron.js @@ -9,5 +9,8 @@ module.exports = { }, remote: jest.fn(), dialog: jest.fn(), - BrowserWindow: jest.fn() + BrowserWindow: jest.fn(), + ipcMain: { + on: jest.fn() + } } diff --git a/test/unit/zap/controller.spec.js b/test/unit/zap/controller.spec.js new file mode 100644 index 00000000..c9a6e18a --- /dev/null +++ b/test/unit/zap/controller.spec.js @@ -0,0 +1,54 @@ +import ZapController from 'lib/zap/controller' +import LndConfig from 'lib/lnd/config' + +jest.mock('lib/lnd/lightning') +const Lightning = require('lib/lnd/lightning') + +describe('ZapController', function() { + describe('Constructor', () => { + beforeAll(() => { + this.controller = new ZapController() + }) + + describe('initial values', () => { + it('should set the "lndConfig" property to a new LndConfig instance', () => { + expect(this.controller.lndConfig).toBeInstanceOf(LndConfig) + }) + it('should set the "splashScreenTime" property to 500', () => { + expect(this.controller.splashScreenTime).toEqual(500) + }) + it('should set the "mainWindow" property to undefined', () => { + expect(this.controller.mainWindow).toBeUndefined() + }) + }) + }) + + describe('.startLightningWallet', () => { + describe('successful connection', () => { + beforeEach(() => { + Lightning.mockImplementation(() => ({ + subscribe: jest.fn(), + connect: jest.fn().mockResolvedValue() + })) + this.controller = new ZapController() + }) + it('should resolve with undefined', async () => { + await expect(this.controller.startLightningWallet()).resolves.toBeUndefined() + expect(this.controller.lightning.subscribe).toHaveBeenCalled() + }) + }) + describe('unsuccessful connection', () => { + beforeEach(() => { + Lightning.mockImplementation(() => ({ + subscribe: jest.fn(), + connect: jest.fn().mockRejectedValue(new Error('Async error')) + })) + this.controller = new ZapController() + }) + it('should reject an error', async () => { + await expect(this.controller.startLightningWallet()).rejects.toThrow('Async error') + expect(this.controller.lightning.subscribe).not.toHaveBeenCalled() + }) + }) + }) +})