Move common test routines into modules

This commit is contained in:
Ilya Ig. Petrov 2017-05-02 11:55:01 -07:00
parent 3d68ae2529
commit 9030f9122c
5 changed files with 67 additions and 70 deletions

View File

@ -3,6 +3,7 @@
"version": "0.0.19", "version": "0.0.19",
"description": "Development tools for chromium extension", "description": "Development tools for chromium extension",
"scripts": { "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", "lint": "eslint ./src/**/*.js --ignore-pattern vendor",
"gulp": "gulp", "gulp": "gulp",
"test": "mocha --recursive ./src/**/test/*" "test": "mocha --recursive ./src/**/test/*"

View File

@ -1,72 +1,11 @@
'use strict'; 'use strict';
const Chrome = require('sinon-chrome'); const Chrome = require('sinon-chrome');
const Storage = require('_project-root/tools/sinon-storage');
const Chai = require('chai'); const Chai = require('chai');
const Mocha = require('mocha'); const Mocha = require('mocha');
const storageMock = function storageMock() { const MyRequire = require('_project-root/tools/cacheless-require')(module);
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.describe('window.apis.pacKitchen', function () {
@ -76,15 +15,15 @@ Mocha.describe('window.apis.pacKitchen', function () {
global.chrome = Chrome; global.chrome = Chrome;
global.window = { global.window = {
chrome: Chrome, 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 () { 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'); Chai.assert.ok(window.apis.pacKitchen, 'exports to globals');
}); });

View File

@ -3,22 +3,21 @@
const Chai = require('chai'); const Chai = require('chai');
const Mocha = require('mocha'); const Mocha = require('mocha');
const MyRequire = require('_project-root/tools/cacheless-require')(module);
Mocha.describe('window.utils', function () { Mocha.describe('window.utils', function () {
const initApis = '../00-init-apis.js'; const initApis = '../00-init-apis.js';
Mocha.beforeEach(function() { Mocha.beforeEach(function() {
delete require.cache[require.resolve(initApis)];
global.window = {}; global.window = {};
}); });
Mocha.it('exports as global', function () { Mocha.it('exports as global', function () {
console.log('1',window); MyRequire(initApis);
require(initApis);
console.log('2',window);
Chai.assert.ok(window.utils, 'exported to globals'); Chai.assert.ok(window.utils, 'exported to globals');
Chai.assert.isNotOk(window.apis.version.ifMini, 'is not MINI version'); Chai.assert.isNotOk(window.apis.version.ifMini, 'is not MINI version');

View File

@ -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);
};

View File

@ -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);
},
});
};