Browse Source

test(electron): simplify electron mocks

renovate/lint-staged-8.x
Tom Kirkpatrick 7 years ago
parent
commit
e786958b55
No known key found for this signature in database GPG Key ID: 72203A8EC5967EA8
  1. 8
      app/lib/lnd/config.js
  2. 13
      app/lib/lnd/util.js
  3. 2
      test/e2e/e2e.spec.js
  4. 13
      test/unit/__mocks__/electron.js
  5. 29
      test/unit/lnd/lnd-config.spec.js
  6. 11
      test/unit/lnd/neutrino.spec.js

8
app/lib/lnd/config.js

@ -141,7 +141,9 @@ class LndConfig {
},
binaryPath: {
enumerable: true,
value: binaryPath
get() {
return binaryPath()
}
},
dataDir: {
enumerable: true,
@ -152,13 +154,13 @@ class LndConfig {
configPath: {
enumerable: true,
get() {
return join(appRootPath, 'resources', 'lnd.conf')
return join(appRootPath(), 'resources', 'lnd.conf')
}
},
rpcProtoPath: {
enumerable: true,
get() {
return join(appRootPath, 'resources', 'rpc.proto')
return join(appRootPath(), 'resources', 'rpc.proto')
}
},

13
app/lib/lnd/util.js

@ -33,8 +33,9 @@ const dnsLookup = promisify(dns.lookup)
* And no, we can't just set our working directory to somewhere inside the asar. The OS can't handle that.
* @return {String} Path to the lnd binary.
*/
export const appRootPath =
app.getAppPath().indexOf('default_app.asar') < 0 ? normalize(`${app.getAppPath()}/..`) : ''
export const appRootPath = () => {
return app.getAppPath().indexOf('default_app.asar') < 0 ? normalize(`${app.getAppPath()}/..`) : ''
}
/**
* Get the OS specific lnd binary name.
@ -46,9 +47,11 @@ export const binaryName = platform() === 'win32' ? 'lnd.exe' : 'lnd'
* Get the OS specific path to the lnd binary.
* @return {String} Path to the lnd binary.
*/
export const binaryPath = isDev
? join(dirname(require.resolve('lnd-binary/package.json')), 'vendor', binaryName)
: join(appRootPath, 'bin', binaryName)
export const binaryPath = () => {
return isDev
? join(dirname(require.resolve('lnd-binary/package.json')), 'vendor', binaryName)
: join(appRootPath(), 'bin', binaryName)
}
// ------------------------------------
// Helpers

2
test/e2e/e2e.spec.js

@ -2,6 +2,8 @@ import { Application } from 'spectron'
import electronPath from 'electron'
import path from 'path'
jest.unmock('electron')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000
const delay = time => new Promise(resolve => setTimeout(resolve, time))

13
test/unit/__mocks__/electron.js

@ -0,0 +1,13 @@
const { normalize } = require('path')
module.exports = {
require: jest.fn(),
match: jest.fn(),
app: {
getPath: name => normalize(`/tmp/zap-test/${name}`),
getAppPath: () => normalize('/tmp/zap-test')
},
remote: jest.fn(),
dialog: jest.fn(),
BrowserWindow: jest.fn()
}

29
test/unit/lnd/lnd-config.spec.js

@ -4,46 +4,25 @@ import { join, normalize } from 'path'
import Store from 'electron-store'
import LndConfig from 'lib/lnd/config'
jest.mock('grpc')
jest.mock('electron', () => {
const { normalize } = require('path')
return {
app: {
getPath: name => normalize(`/tmp/zap-test/${name}`),
getAppPath: () => normalize('/tmp/zap-test')
}
}
})
jest.mock('electron-store')
jest.mock('lib/lnd/util', () => {
const { normalize } = require('path')
return {
...jest.requireActual('lib/lnd/util'),
appRootPath: normalize('/tmp/zap-test/app/root'),
binaryName: 'binaryName',
binaryPath: 'binaryPath'
binaryPath: () => 'binaryPath'
}
})
Store.prototype.set = jest.fn()
describe('LndConfig', function() {
const checkForStaticProperties = () => {
it('should have "binaryPath" set to the value returned by lib/lnd/util', () => {
expect(this.lndConfig.binaryPath).toEqual('binaryPath')
})
it('should have "configPath" set to "resources/lnd.conf" relative to app root from lib/lnd/util"', () => {
expect(this.lndConfig.configPath).toEqual(
normalize('/tmp/zap-test/app/root/resources/lnd.conf')
)
expect(this.lndConfig.configPath).toEqual(normalize('/tmp/resources/lnd.conf'))
})
it('should have "rpcProtoPath" set to "resources/rcp.proto" relative to app root from lib/lnd/util"', () => {
expect(this.lndConfig.rpcProtoPath).toEqual(
normalize('/tmp/zap-test/app/root/resources/rpc.proto')
)
expect(this.lndConfig.rpcProtoPath).toEqual(normalize('/tmp/resources/rpc.proto'))
})
}

11
test/unit/lnd/neutrino.spec.js

@ -1,15 +1,6 @@
import Neutrino from 'lib/lnd/neutrino'
jest.mock('electron', () => {
const { normalize } = require('path')
return {
app: {
getPath: name => normalize(`/tmp/zap-test/${name}`),
getAppPath: () => normalize('/tmp/zap-test')
}
}
})
jest.mock('electron-store')
describe('Neutrino', function() {
describe('Constructor', () => {

Loading…
Cancel
Save