Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeke committed Apr 19, 2018
1 parent a076403 commit 074f751
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const assert = require('assert')
const isURL = require('is-url')
const isDev = require('electron-is-dev')
const ms = require('ms')
const {app, autoUpdater, dialog} = require('electron')

module.exports = function updater (opts = {}) {
// check for bad input early, so it will be logged during development
Expand All @@ -16,13 +15,14 @@ module.exports = function updater (opts = {}) {
return
}

app.isReady()
opts.electron.app.isReady()
? initUpdater(opts)
: app.on('ready', () => initUpdater(opts))
: opts.electron.app.on('ready', () => initUpdater(opts))
}

function initUpdater (opts) {
const {host, repo, updateInterval, debug} = opts
const {host, repo, updateInterval, debug, electron} = opts
const {app, autoUpdater, dialog} = electron
const feedURL = `${host}/${repo}/${process.platform}/${app.getVersion()}`

function log () {
Expand Down Expand Up @@ -76,6 +76,9 @@ function validateInput (opts) {
}
const {host, repo, updateInterval, debug} = Object.assign({}, defaults, opts)

// allows electron to be mocked in tests
const electron = opts.electron || require('electron')

assert(
repo && repo.length && repo.includes('/'),
'repo is required and should be in the format `owner/repo`'
Expand All @@ -101,5 +104,5 @@ function validateInput (opts) {
'debug must be a boolean'
)

return {host, repo, updateInterval, debug}
return {host, repo, updateInterval, debug, electron}
}
69 changes: 57 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
// const proxyquire = require('proxyquire')
// const mock = require('mock-require')
const updater = require('.')
const repo = 'some-owner/some-repo'
const electron = {
app: {
getVersion: () => { return '1.2.3' },
isReady: () => { return true },
on: (eventName) => { /* no-op */ }
},
autoUpdater: {
checkForUpdates: () => { /* no-op */ },
on: (eventName) => { /* no-op */ },
setFeedURL: () => { /* no-op */ }
},
dialog: {
showMessageBox: () => { /* no-op */ }
}
}

// const updater = mock('./index.js', {
// 'electron': {
// app: {},
// autoUpdater: {},
// dialog: {}
// }
// })
test('exports a function', () => {
expect(typeof updater).toBe('function')
})

// TODO: Figure out how to mock require('electron')
describe('repository', () => {
test('is required', () => {
expect(() => {
updater({electron})
}).toThrowError('repo is required and should be in the format `owner/repo`')
})
})

xtest('exports a function', () => {
expect(typeof updater).toBe('function')
describe('host', () => {
test('must a valid HTTPS URL', () => {
expect(() => {
updater({repo, electron, host: 'http://example.com'})
}).toThrowError('host must be a valid HTTPS URL')
})
})

describe('debug', () => {
test('must be a boolean', () => {
expect(() => {
updater({repo, electron, debug: 'yep'})
}).toThrowError('debug must be a boolean')
})
})

describe('updateInterval', () => {
test('must be 30 seconds or more', () => {
expect(() => {
updater({repo, electron, updateInterval: '20 seconds'})
}).toThrowError('updateInterval must be `30 seconds` or more')
})

test('must be a string', () => {
expect(() => {
updater({repo, electron, updateInterval: 3000})
}).toThrowError('updateInterval must be a human-friendly string interval like `90 seconds`')
})
})


0 comments on commit 074f751

Please sign in to comment.