diff --git a/package.json b/package.json index 156c263..083a262 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "scripts": { "test": "jest --verbose --coverage && mocha --compilers js:mocha-traceur tests/**/*.spec.js", "test-react": "jest --verbose --coverage", - "test-js": "mocha --compilers js:mocha-traceur tests/**/*.spec.js", + "test-js": "node_modules/.bin/phantomjs tests/run.js", "start": "npm install && bower install && gulp", "lint": "jsxhint --jsx-only .", "postinstall": "gulp postinstall" @@ -38,7 +38,7 @@ "react-tools": "^0.13.3", "sinon": "^1.15.4", "sinon-chai": "^2.8.0", - "sinon-chrome": "^0.1.2" + "sinon-chrome": "^0.2.1" }, "dependencies": { "bootstrap": "^3.3.4", diff --git a/tests/beforeEach.js b/tests/beforeEach.js new file mode 100644 index 0000000..607a9df --- /dev/null +++ b/tests/beforeEach.js @@ -0,0 +1,40 @@ +var fs = require('fs'); +var page; +var beforeLoadFn; + +beforeEach(function() { + page = require('webpage').create(); + + page.onConsoleMessage = function(msg) { console.log(msg); }; + + page.onError = function(msg, trace) { + var msgStack = [msg]; + if (trace && trace.length) { + msgStack.push('TRACE:'); + trace.forEach(function(t) { + msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); + }); + } + // we need try..catch here as mocha throws error that catched by phantom.onError + try { + mocha.throwError(msgStack.join('\n')); + } catch(e) { } + }; + + page.onInitialized = function() { + page.injectJs(node_modules + 'chai/chai.js'); + page.injectJs(node_modules + 'sinon/pkg/sinon.js'); + page.injectJs(node_modules + 'sinon-chrome/chrome.js'); + page.injectJs(node_modules + 'sinon-chrome/src/phantom-tweaks.js'); + page.injectJs(node_modules + 'require-stub/index.js'); + // call additional function defined in tests + if (beforeLoadFn) { + beforeLoadFn(); + } + }; +}); + +afterEach(function() { + page.close(); + beforeLoadFn = null; +}); \ No newline at end of file diff --git a/tests/empty.html b/tests/empty.html index 7b7b378..ed12e53 100644 --- a/tests/empty.html +++ b/tests/empty.html @@ -2,7 +2,7 @@ - Empty HTML Document + diff --git a/tests/helpers/changeExtensionTooltip.spec.js b/tests/helpers/changeExtensionTooltip.spec.js index 52f5657..a8e8493 100644 --- a/tests/helpers/changeExtensionTooltip.spec.js +++ b/tests/helpers/changeExtensionTooltip.spec.js @@ -1,18 +1,38 @@ -var chai = require('chai'); -var chrome = require('sinon-chrome'); -var sinon = require('sinon-chai'); -var expect = chai.expect; +// var chai = require('chai'); +// var chrome = require('sinon-chrome'); +// var sinon = require('sinon-chai'); +// var expect = chai.expect; -import changeExtensionTooltip from '../../assets/js/helpers/changeExtensionTooltip'; +// import changeExtensionTooltip from '../../assets/js/helpers/changeExtensionTooltip'; + +// describe('changeExtensionTooltip', function() { +// it('should be a function', function() { +// expect(changeExtensionTooltip).to.be.a('function'); +// }); + +// it('should change the extension tooltip', function() { +// changeExtensionTooltip('WakaTime'); +// expect(chrome.browserAction.setTitle).toHaveBeenCalledWith({title: 'Wakatime'}); +// sinon.assert.calledWithMatch(chrome.browserAction.setTitle, {title: 'WakaTime'}); +// }); +// }); + +var babel describe('changeExtensionTooltip', function() { - it('should be a function', function() { - expect(changeExtensionTooltip).to.be.a('function'); - }); + // sometimes it takes time to start phantomjs + this.timeout(4000); - it('should change the extension tooltip', function() { - changeExtensionTooltip('WakaTime'); - expect(chrome.browserAction.setTitle).toHaveBeenCalledWith({title: 'Wakatime'}); - sinon.assert.calledWithMatch(chrome.browserAction.setTitle, {title: 'WakaTime'}); - }); + var FILENAME = 'tests/empty.html'; + + it('should exist', function(done) { + page.open(FILENAME, function() { + page.injectJs('assets/js/helpers/changeExtensionTooltip.js'); + + page.evaluate(function() { + console.log(typeof changeExtensionIcon); + }); + done(); + }); + }); }); \ No newline at end of file diff --git a/tests/run.js b/tests/run.js new file mode 100644 index 0000000..df69fb1 --- /dev/null +++ b/tests/run.js @@ -0,0 +1,24 @@ +/** + * Test Runner for Mocha tests + * Using phantomjs to render page and execute scripts + */ + +var node_modules = '../node_modules/'; +phantom.injectJs(node_modules + 'mocha/mocha.js'); +phantom.injectJs(node_modules + 'sinon-chrome/src/phantom-tweaks.js'); +mocha.setup({ui: 'bdd', reporter: 'spec'}); + +// Setup +phantom.injectJs('beforeeach.js'); + +// Tests +phantom.injectJs('helpers/changeExtensionTooltip.spec.js'); + +// Execute +mocha.run(function(failures) { + // setTimeout is needed to supress "Unsafe JavaScript attempt to access..." + // see https://github.com/ariya/phantomjs/issues/12697 + setTimeout(function() { + phantom.exit(failures); + }, 0); +}); \ No newline at end of file