diff --git a/extensions/chromium/runet-censorship-bypass/package.json b/extensions/chromium/runet-censorship-bypass/package.json index e3de158..3b69739 100644 --- a/extensions/chromium/runet-censorship-bypass/package.json +++ b/extensions/chromium/runet-censorship-bypass/package.json @@ -3,6 +3,7 @@ "version": "0.0.19", "description": "Development tools for chromium extension", "scripts": { + "postinstall": "node --use_strict -e \"const fs = require('fs'), path = 'node_modules/_project-root'; fs.unlink(path, ()=> fs.symlinkSync('..', path, 'dir'));\"", "lint": "eslint ./src/**/*.js --ignore-pattern vendor", "gulp": "gulp", "test": "mocha --recursive ./src/**/test/*" 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 index 11c12fd..4a8e695 100644 --- 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 @@ -1,72 +1,11 @@ 'use strict'; const Chrome = require('sinon-chrome'); +const Storage = require('_project-root/tools/sinon-storage'); 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); - -}; +const MyRequire = require('_project-root/tools/cacheless-require')(module); Mocha.describe('window.apis.pacKitchen', function () { @@ -76,15 +15,15 @@ Mocha.describe('window.apis.pacKitchen', function () { global.chrome = Chrome; global.window = { chrome: Chrome, - localStorage: new storageMock(), + localStorage: new Storage(), }; - myRequire('../00-init-apis.js'); + MyRequire('../00-init-apis.js'); }); Mocha.it('is evaluated and defined', function () { - myRequire('../35-pac-kitchen-api.js'); + MyRequire('../35-pac-kitchen-api.js'); Chai.assert.ok(window.apis.pacKitchen, 'exports to globals'); }); 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 index b5fbcb3..52d3e06 100644 --- a/extensions/chromium/runet-censorship-bypass/src/extension-common/test/utils.js +++ b/extensions/chromium/runet-censorship-bypass/src/extension-common/test/utils.js @@ -3,22 +3,21 @@ const Chai = require('chai'); const Mocha = require('mocha'); +const MyRequire = require('_project-root/tools/cacheless-require')(module); + 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); + MyRequire(initApis); Chai.assert.ok(window.utils, 'exported to globals'); Chai.assert.isNotOk(window.apis.version.ifMini, 'is not MINI version'); diff --git a/extensions/chromium/runet-censorship-bypass/tools/cacheless-require.js b/extensions/chromium/runet-censorship-bypass/tools/cacheless-require.js new file mode 100644 index 0000000..abdbe13 --- /dev/null +++ b/extensions/chromium/runet-censorship-bypass/tools/cacheless-require.js @@ -0,0 +1,10 @@ +'ust strict'; + +module.exports = (parentModule) => function cachelessRequire(path) { + + for(let key of Object.keys(require.cache)) { + delete require.cache[key]; + } + return parentModule.require(path); + +}; diff --git a/extensions/chromium/runet-censorship-bypass/tools/sinon-storage.js b/extensions/chromium/runet-censorship-bypass/tools/sinon-storage.js new file mode 100644 index 0000000..7a2d356 --- /dev/null +++ b/extensions/chromium/runet-censorship-bypass/tools/sinon-storage.js @@ -0,0 +1,48 @@ +'use strict'; + +const Sinon = require('sinon'); + +module.exports = function storageMock() { + + let storage = {}; + + return new Proxy({ + setItem: Sinon.spy(function(key, value) { + storage[key] = value || ''; + }), + getItem: Sinon.spy(function(key) { + return key in storage ? storage[key] : null; + }), + removeItem: Sinon.spy(function(key) { + delete storage[key]; + }), + get length() { + return Object.keys(storage).length; + }, + key: Sinon.spy(function(i) { + throw new Error('Not implemented!'); + }), + clear: Sinon.spy(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); + + }, + }); + +};