From 3d68ae252977021fcbf1d24edc4cb16dd89bf79f Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Tue, 2 May 2017 10:06:53 -0700 Subject: [PATCH] Add basic tests on Mocha/Chai/Sinon --- .../runet-censorship-bypass/package.json | 8 +- .../src/extension-common/00-init-apis.js | 6 +- .../extension-common/35-pac-kitchen-api.js | 3 + .../src/extension-common/test/pac-kitchen.js | 101 ++++++++++++++++++ .../src/extension-common/test/utils.js | 36 +++++++ 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 extensions/chromium/runet-censorship-bypass/src/extension-common/test/pac-kitchen.js create mode 100644 extensions/chromium/runet-censorship-bypass/src/extension-common/test/utils.js diff --git a/extensions/chromium/runet-censorship-bypass/package.json b/extensions/chromium/runet-censorship-bypass/package.json index e161dd3..e3de158 100644 --- a/extensions/chromium/runet-censorship-bypass/package.json +++ b/extensions/chromium/runet-censorship-bypass/package.json @@ -4,13 +4,17 @@ "description": "Development tools for chromium extension", "scripts": { "lint": "eslint ./src/**/*.js --ignore-pattern vendor", - "gulp": "gulp" + "gulp": "gulp", + "test": "mocha --recursive ./src/**/test/*" }, "author": "Ilya Ig. Petrov", "license": "GPLv3", "devDependencies": { + "chai": "^3.5.0", "eslint": "^3.15.0", - "eslint-config-google": "^0.7.1" + "eslint-config-google": "^0.7.1", + "mocha": "^3.3.0", + "sinon-chrome": "^2.2.1" }, "dependencies": { "del": "^2.2.2", diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js index 1cb6214..d359622 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/00-init-apis.js @@ -125,16 +125,16 @@ key = prefix + key; if (value === null) { - return localStorage.removeItem(key); + return window.localStorage.removeItem(key); } if (value === undefined) { - const item = localStorage.getItem(key); + const item = window.localStorage.getItem(key); return item && JSON.parse(item); } if (value instanceof Date) { throw new TypeError('Converting Date format to JSON is not supported.'); } - localStorage.setItem(key, JSON.stringify(value)); + window.localStorage.setItem(key, JSON.stringify(value)); }; diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js index 33fe42c..1500c33 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/35-pac-kitchen-api.js @@ -104,6 +104,9 @@ const getCurrentConfigs = function getCurrentConfigs() { + return kitchenState(modsKey); + + // In case of migration. const [err, mods, ...warns] = createPacModifiers( kitchenState(modsKey) ); if (err) { throw err; diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/test/pac-kitchen.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/test/pac-kitchen.js new file mode 100644 index 0000000..11c12fd --- /dev/null +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/test/pac-kitchen.js @@ -0,0 +1,101 @@ +'use strict'; + +const Chrome = require('sinon-chrome'); +const Chai = require('chai'); +const Mocha = require('mocha'); + +const storageMock = function storageMock() { + + let storage = {}; + + return new Proxy({ + setItem: function(key, value) { + + storage[key] = value || ''; + + }, + getItem: function(key) { + + return key in storage ? storage[key] : null; + + }, + removeItem: function(key) { + + delete storage[key]; + + }, + get length() { + + return Object.keys(storage).length; + + }, + key: function(i) { + + throw new Error('Not implemented!'); + + }, + clear: function() { + + storage = {}; + + }, + }, { + get: function(target, name) { + + if (name in target) { + return target[name]; + } + return target.getItem(name); + + }, + set: function(target, prop, value) { + + if (prop in target) { + target[prop] = value; + return; + } + return target.setItem(prop, value); + + }, + }); + +}; + +const myRequire = (path) => { + + delete require.cache[require.resolve(path)]; + return require(path); + +}; + +Mocha.describe('window.apis.pacKitchen', function () { + + + Mocha.beforeEach(function() { + + global.chrome = Chrome; + global.window = { + chrome: Chrome, + localStorage: new storageMock(), + }; + myRequire('../00-init-apis.js'); + + }); + + Mocha.it('is evaluated and defined', function () { + + myRequire('../35-pac-kitchen-api.js'); + Chai.assert.ok(window.apis.pacKitchen, 'exports to globals'); + + }); + + Mocha.afterEach(function() { + + delete global.window; + + }); + +}); + + + diff --git a/extensions/chromium/runet-censorship-bypass/src/extension-common/test/utils.js b/extensions/chromium/runet-censorship-bypass/src/extension-common/test/utils.js new file mode 100644 index 0000000..b5fbcb3 --- /dev/null +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/test/utils.js @@ -0,0 +1,36 @@ +'use strict'; + +const Chai = require('chai'); +const Mocha = require('mocha'); + +Mocha.describe('window.utils', function () { + + const initApis = '../00-init-apis.js'; + + Mocha.beforeEach(function() { + + delete require.cache[require.resolve(initApis)]; + global.window = {}; + + }); + + Mocha.it('exports as global', function () { + + console.log('1',window); + require(initApis); + console.log('2',window); + Chai.assert.ok(window.utils, 'exported to globals'); + Chai.assert.isNotOk(window.apis.version.ifMini, 'is not MINI version'); + + }); + + Mocha.afterEach(function() { + + delete global.window; + + }); + +}); + + +