Skip to content

Commit

Permalink
Merge pull request #12 from electron/add-tests
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
zeke authored Apr 19, 2018
2 parents a076403 + 086bc27 commit 2578f98
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
cache:
directories:
- ~/.npm
notifications:
email: false
node_js:
- '8'
after_success:
- npm run travis-deploy-once "npm run semantic-release"
branches:
except:
- /^v\d+\.\d+\.\d+$/
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}
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-electron-app",
"version": "1.0.1",
"version": "0.0.0-development",
"description": "A drop-in module that adds autoUpdating capabilities to Electron apps",
"repository": "https://github.com/electron/update-electron-app",
"main": "index.js",
Expand All @@ -13,11 +13,15 @@
"devDependencies": {
"jest": "^22.4.3",
"standard": "^11.0.1",
"standard-markdown": "^4.0.2"
"standard-markdown": "^4.0.2",
"travis-deploy-once": "^4.4.1",
"semantic-release": "^15.1.7"
},
"scripts": {
"test": "jest && standard --fix && standard-markdown",
"watch": "jest --watch --notify --notifyMode=change --coverage"
"watch": "jest --watch --notify --notifyMode=change --coverage",
"travis-deploy-once": "travis-deploy-once",
"semantic-release": "semantic-release"
},
"standard": {
"env": {
Expand Down
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 2578f98

Please sign in to comment.