Skip to content

Commit

Permalink
electron-window: refactored, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jprichardson committed Jul 9, 2015
1 parent 0b43690 commit dea581a
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,6 +1,7 @@
0.4.0 / 2015-07-09
------------------
- Removed passing defaults of `resizable: false` and `frame: true`.
- refactored, added tests

0.3.0 / 2015-05-27
------------------
Expand Down
39 changes: 39 additions & 0 deletions lib/__tests__/args.test.js
@@ -0,0 +1,39 @@
var assert = require('assert')
var args = require('../args')

/* global describe it */

describe('args', function () {
describe('encode()', function () {
it('should hash encode object', function () {
var obj = {name: 'jp'}
var str = args.encode(obj)
assert.strictEqual(str, '%7B%22name%22%3A%22jp%22%7D')

str = args.encode(null)
assert.strictEqual(str, '')
})
})

describe('urlWithArgs()', function () {
describe('> when url', function () {
it('should create a url with encoded args in hash', function () {
var url = args.urlWithArgs('http://google.com', {doThat: 'ok'})
assert.strictEqual(url, 'http://google.com/#%7B%22doThat%22%3A%22ok%22%7D')

url = args.urlWithArgs('http://google.com')
assert.strictEqual(url, 'http://google.com/')
})
})

describe('> when file', function () {
it('should create a url with encoded args in hash', function () {
var url = args.urlWithArgs('/tmp/index.html', {doThat: 'ok'})
assert.strictEqual(url, 'file:///tmp/index.html#%7B%22doThat%22%3A%22ok%22%7D')

url = args.urlWithArgs('/tmp/index.html')
assert.strictEqual(url, 'file:///tmp/index.html')
})
})
})
})
40 changes: 40 additions & 0 deletions lib/__tests__/main.test.js
@@ -0,0 +1,40 @@
var assert = require('assert')
var proxyquire = require('proxyquire')
// var stubo = require('stubo')

/* global describe it */

describe('main', function () {
describe('_createWindow()', function () {
it('should should create the window with options and set global window references', function () {
function BrowserWindow (options) { this.id = 5; this._captureOptions = options }
var stubs = {
'browser-window': BrowserWindow
}
stubs['browser-window']['@noCallThru'] = true
var main = proxyquire('../main', stubs)

var win = main._createWindow({frame: 'something'})
assert(win instanceof BrowserWindow)
assert.strictEqual(main.windows['5'], win)
assert.strictEqual(win._captureOptions.frame, 'something')
})
})

describe('_unref()', function () {
it('should delete global reference', function () {
var stubs = {'browser-window': {}}
stubs['browser-window']['@noCallThru'] = true
var main = proxyquire('../main', stubs)

var win = {id: 3}
main.windows[win.id] = win
assert.strictEqual(main.windows[3], win)
assert.strictEqual(Object.keys(main.windows).length, 1)

main._unref.call(win)
assert.strictEqual(main.windows[3], undefined)
assert.strictEqual(Object.keys(main.windows).length, 0)
})
})
})
37 changes: 37 additions & 0 deletions lib/args.js
@@ -0,0 +1,37 @@
var assert = require('assert')
var assign = require('object-assign')
var path = require('path')
var url = require('url')

function encode (args) {
args = args || null
assert.strictEqual(typeof args, 'object', 'args must be an object')
// stringify the args
args = args ? encodeURIComponent(JSON.stringify(args)) : ''
return args
}

function urlWithArgs (urlOrFile, args) {
args = encode(args)

var u
if (urlOrFile.indexOf('http') === 0) {
var urlData = url.parse(urlOrFile)
var hash = urlData.hash || args ? args : undefined
u = url.format(assign(urlData, { hash: hash }))
} else { // presumably a file url
u = url.format({
protocol: 'file',
pathname: path.resolve(urlOrFile),
slashes: true,
hash: args ? args : undefined
})
}

return u
}

module.exports = {
encode: encode,
urlWithArgs: urlWithArgs
}
63 changes: 23 additions & 40 deletions lib/main.js
@@ -1,73 +1,56 @@
var assert = require('assert')
var assign = require('object-assign')
var path = require('path')
var url = require('url')
var BrowserWindow = require('browser-window')
var wargs = require('./args')

// retain global references, if not, window will be closed automatically when
// garbage collected
var _windows = {}

function createWindow (options) {
var window = new BrowserWindow(extend({
function _createWindow (options) {
var opts = assign({
show: false,
preload: path.join(__dirname, 'renderer-preload')
}, options))
}, options)

var window = new BrowserWindow(opts)
_windows[window.id] = window

// should not need to be called, but just in case
// window.destroy() is ever called
window.unref = function () {
delete _windows[window.id]
}
return window
}

window.once('closed', function () {
window.unref()
})
// should not need to be called directly, but just in case
// window.destroy() is ever called
function _unref () {
delete _windows[this.id]
}

function createWindow (options) {
var window = _createWindow(options)
window.unref = _unref.bind(window)
window.once('closed', window.unref)

window.showUrl = function (httpOrFileUrl, args, callback) {
if (typeof args === 'function') {
callback = args
args = null
}
assert.strictEqual(typeof args, 'object', 'args must be an object')
// stringify the args
args = args ? encodeURIComponent(JSON.stringify(args)) : ''

window.webContents.once('did-finish-load', function () {
window.show()
callback.apply(this, arguments)
})

if (httpOrFileUrl.indexOf('http') === 0) {
var urlData = url.parse(httpOrFileUrl)
var httpUrl = url.format(extend(urlData, {
hash: urlData.hash || args ? args : undefined
}))
window.loadUrl(httpUrl)
} else { // presumably a file url
var fileUrl = url.format({
protocol: 'file',
pathname: path.resolve(httpOrFileUrl),
slashes: true,
hash: args ? args : undefined
})

window.loadUrl(fileUrl)
}
var url = wargs.urlWithArgs(httpOrFileUrl, args)
window.loadUrl(url)
}

return window
}

function extend (target, source) {
Object.keys(source).forEach(function (key) {
target[key] = source[key]
})
return target
}

module.exports = {
createWindow: createWindow,
windows: _windows
windows: _windows,
_createWindow: _createWindow,
_unref: _unref
}
10 changes: 7 additions & 3 deletions package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Convenience methods for Electron windows.",
"main": "./lib/index.js",
"scripts": {
"test": "standard"
"test": "standard && find ./lib -name *.test.js | xargs mocha"
},
"repository": {
"type": "git",
Expand All @@ -25,9 +25,13 @@
},
"homepage": "https://github.com/jprichardson/electron-window",
"devDependencies": {
"standard": "^3.11.0"
"mocha": "2.x",
"proxyquire": "^1.6.0",
"standard": "4.x",
"stubo": "^0.1.0"
},
"dependencies": {
"is-electron-renderer": "^1.0.0"
"is-electron-renderer": "^1.0.0",
"object-assign": "^3.0.0"
}
}

0 comments on commit dea581a

Please sign in to comment.