From 133e34dea2a9bce630e2c0f07f902a06576263dc Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Mon, 27 Aug 2018 16:02:32 +0200 Subject: [PATCH] test(neutrino): basic neutrino process spawn test --- package.json | 1 + test/unit/lnd/neutrino.spec.js | 46 ++++++++++++++++++++++++++-------- yarn.lock | 8 +++++- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 6288f37b..097e1564 100644 --- a/package.json +++ b/package.json @@ -258,6 +258,7 @@ "lint-staged": "^7.2.0", "lnd-binary": "^0.3.5", "minimist": "^1.2.0", + "mock-spawn": "^0.2.6", "node-sass": "^4.9.0", "prettier": "^1.13.5", "react-addons-test-utils": "^15.6.2", diff --git a/test/unit/lnd/neutrino.spec.js b/test/unit/lnd/neutrino.spec.js index 2b7dcaba..7c645ee6 100644 --- a/test/unit/lnd/neutrino.spec.js +++ b/test/unit/lnd/neutrino.spec.js @@ -1,10 +1,22 @@ +// @flow + import Neutrino from 'lib/lnd/neutrino' +import LndConfig from 'lib/lnd/config' +import mockSpawn from 'mock-spawn' jest.mock('electron-store') +jest.mock('child_process', () => { + var mockSpawn = require('mock-spawn') + return { + spawn: mockSpawn() + } +}) describe('Neutrino', function() { describe('Constructor', () => { - beforeAll(() => (this.neutrino = new Neutrino())) + beforeAll(() => { + this.neutrino = new Neutrino(new LndConfig()) + }) describe('initial values', () => { it('should set the "process" property to null', () => { @@ -37,7 +49,7 @@ describe('Neutrino', function() { describe('.setState', () => { describe('called with new state', () => { beforeEach(() => { - this.neutrino = new Neutrino() + this.neutrino = new Neutrino(new LndConfig()) this.callback = jest.fn() this.newVal = 'chain-sync-finished' this.neutrino.on('chain-sync-finished', this.callback) @@ -53,7 +65,7 @@ describe('Neutrino', function() { }) describe('called with current state', () => { beforeEach(() => { - this.neutrino = new Neutrino() + this.neutrino = new Neutrino(new LndConfig()) this.callback = jest.fn() this.newVal = 'chain-sync-pending' this.neutrino.on('chain-sync-pending', this.callback) @@ -72,7 +84,7 @@ describe('Neutrino', function() { describe('.setCurrentBlockHeight', () => { describe('called with higher height', () => { beforeEach(() => { - this.neutrino = new Neutrino() + this.neutrino = new Neutrino(new LndConfig()) this.callback = jest.fn() this.newVal = 100 this.neutrino.on('got-current-block-height', this.callback) @@ -89,7 +101,7 @@ describe('Neutrino', function() { }) describe('called with lower height', () => { beforeEach(() => { - this.neutrino = new Neutrino() + this.neutrino = new Neutrino(new LndConfig()) this.callback = jest.fn() this.newVal = -1 this.neutrino.on('got-current-block-height', this.callback) @@ -108,7 +120,7 @@ describe('Neutrino', function() { describe('.setLndBlockHeight', () => { describe('called with higher height', () => { beforeEach(() => { - this.neutrino = new Neutrino() + this.neutrino = new Neutrino(new LndConfig()) this.callback = jest.fn() this.newVal = 100 this.neutrino.on('got-lnd-block-height', this.callback) @@ -130,7 +142,7 @@ describe('Neutrino', function() { }) describe('called with lower height', () => { beforeEach(() => { - this.neutrino = new Neutrino() + this.neutrino = new Neutrino(new LndConfig()) this.callback = jest.fn() this.newVal = -1 this.neutrino.on('got-lnd-block-height', this.callback) @@ -152,7 +164,9 @@ describe('Neutrino', function() { describe('.is', () => { describe('called with current state', () => { - beforeEach(() => (this.neutrino = new Neutrino())) + beforeEach(() => { + this.neutrino = new Neutrino(new LndConfig()) + }) it('should returnn true if the current state matches', () => { expect(this.neutrino.is('chain-sync-pending')).toEqual(true) @@ -164,10 +178,20 @@ describe('Neutrino', function() { }) describe('.start', () => { + describe('called when neutrino is not running', () => { + beforeEach(() => { + this.neutrino = new Neutrino(new LndConfig()) + this.neutrino.start() + }) + it('should set the subprocess object on the `process` property', () => { + expect(this.neutrino.process.pid).toBeDefined() + }) + }) + describe('called when neutrino is already running', () => { beforeEach(() => { - this.neutrino = new Neutrino() - this.neutrino.process = 123 + this.neutrino = new Neutrino(new LndConfig()) + this.neutrino.process = mockSpawn() }) it('should throw an error', () => { expect(() => { @@ -180,7 +204,7 @@ describe('Neutrino', function() { describe('.kill', () => { describe('called when neutrino is already running', () => { beforeEach(() => { - this.neutrino = new Neutrino() + this.neutrino = new Neutrino(new LndConfig()) this.neutrino.process = { kill: jest.fn() } diff --git a/yarn.lock b/yarn.lock index ea73a0a3..39fd055c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8084,6 +8084,12 @@ mkdirp@0.5.0: dependencies: minimist "0.0.8" +mock-spawn@^0.2.6: + version "0.2.6" + resolved "https://registry.yarnpkg.com/mock-spawn/-/mock-spawn-0.2.6.tgz#b39c15a1c067504310144151f2c1de344d03937f" + dependencies: + through "2.3.x" + moment@^2.22.2: version "2.22.2" resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.2.tgz#3c257f9839fc0e93ff53149632239eb90783ff66" @@ -11648,7 +11654,7 @@ through2@~0.2.3: readable-stream "~1.1.9" xtend "~2.1.1" -through@2, "through@>=2.2.7 <3", through@^2.3.6, through@^2.3.8: +through@2, through@2.3.x, "through@>=2.2.7 <3", through@^2.3.6, through@^2.3.8: version "2.3.8" resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"