Skip to content

Commit

Permalink
Classes. Who doesn't like classes?
Browse files Browse the repository at this point in the history
Also include a test.

Electron-mocha sets up an Electron instance and runs Mocha within
it. One oddity is that it consumes the ready event before running the
Mocha scripts, so that has to be faked (or assumed) within the test.
  • Loading branch information
dougluce committed Jul 8, 2016
1 parent 4c5e995 commit fd6ddbd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 38 deletions.
85 changes: 49 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
'use strict'
const {app, BrowserWindow} = require('electron')
const yargs = require('yargs')
const temp = require('temp')
const path = require('path')
const {app, BrowserWindow} = require('electron')

process.on('uncaughtException', function (err) {
console.error(err)
app.quit()
})

var win, filename, url
class Scanner {
run (url, cb) {
this.url = url
this.cb = cb
process.on('uncaughtException', function (err) {
console.error(err)
app.exit(1)
})
app.once('ready', () => this.onReady())
}

function onReady () {
const options = { frame: false, height: 768, width: 1024, x: 0, y: 0, show: false }
win = new BrowserWindow(options)
win.webContents.once('did-stop-loading', grabPage)
win.loadURL(url)
}
onReady () {
const options = { frame: true, height: 768, width: 1024, x: 0, y: 0, show: false }
this.win = new BrowserWindow(options)
this.win.webContents.once('did-stop-loading', () => this.grabPage())
this.win.loadURL(this.url)
}

function grabPage () {
temp.track()
const dir = temp.mkdirSync('scanner')
filename = path.join(dir, 'page.html')
console.log(`Saving to ${filename}`)
win.webContents.savePage(filename, 'HTMLOnly', scanPage)
}
grabPage () {
temp.track()
const dir = temp.mkdirSync('scanner')
this.filename = path.join(dir, 'page.html')
console.log(`Saving to ${this.filename}`)
this.win.webContents.savePage(this.filename, 'HTMLOnly', () => this.scanPage())
}

function scanPage (error) {
// Do the scan here, HTML source is in filename
app.quit()
if (error) {
console.error(error)
process.exit(-1)
scanPage (error) {
// Do the scan here, HTML source is in this.filename
if (this.cb) {
this.cb(error) // Assume caller will quit the app.
} else {
if (error) {
console.error(error)
app.exit(1)
} else {
app.quit()
}
}
}
}

const argv = yargs
.usage('Usage: $0 URL')
.demand(1)
.strict()
.version()
.help('h')
.argv

url = argv._[0]
app.once('ready', onReady)
if (!module.parent || module.parent.id === '.') {
const argv = yargs
.usage('Usage: $0 URL')
.demand(1)
.strict()
.version()
.help('h')
.argv
const s = new Scanner()
s.run(argv._[0])
} else {
module.exports = Scanner
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
"description": "Advise on what's adblocking you.",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "electron-mocha",
"testmon": "nodemon --exec '(electron-mocha && growlnotify -n nodemon --image ~/green.png -m good) || (growlnotify -n nodemon --image ~/red.png -m crash && exit 1)'"
},
"bin": {
"scan": "./bin/scan"
},
"author": "",
"license": "ISC",
"devDependencies": {
"mocha": "^2.5.3"
"chai": "^3.5.0",
"electron-mocha": "^2.3.0",
"express": "^4.14.0",
"mocha": "^2.5.3",
"nodemon": "^1.9.2"
},
"dependencies": {
"electron-prebuilt": "^1.2.6",
Expand Down
43 changes: 43 additions & 0 deletions test/test-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* global describe, before, after, it */
const expect = require('chai').expect
const express = require('express')
const temp = require('temp')
const path = require('path')
const sinon = require('sinon')
const Scanner = require('..')
const fs = require('fs')
const {app} = require('electron')

describe('Scanner', function () {
before(function (done) {
const app = express()
app.route('/').get((req, res) => {
res.send('Yeah')
})
this.server = app.listen(() => {
this.url = `http://localhost:${this.server.address().port}`
done()
})
})

after(function () {
this.server.close()
})

it('can render and obtain HTML', sinon.test(function (done) {
temp.track()
const test_dir = temp.mkdirSync('scanner-test')
this.stub(temp, 'mkdirSync', () => test_dir) // Sub in our own dir

const s = new Scanner()
const finish = function (error) {
expect(error).to.be.nil
const page_file = path.join(test_dir, 'page.html')
const file_contents = fs.readFileSync(page_file)
expect(file_contents.toString()).to.equal('Yeah')
done()
}
s.run(this.url, finish)
s.onReady() // Because electron-mocha already consumed the ready event
}))
})

0 comments on commit fd6ddbd

Please sign in to comment.