Add basic tests on Mocha/Chai/Sinon

This commit is contained in:
Ilya Ig. Petrov 2017-05-02 10:06:53 -07:00
parent da9e292cad
commit 3d68ae2529
5 changed files with 149 additions and 5 deletions

View File

@ -4,13 +4,17 @@
"description": "Development tools for chromium extension", "description": "Development tools for chromium extension",
"scripts": { "scripts": {
"lint": "eslint ./src/**/*.js --ignore-pattern vendor", "lint": "eslint ./src/**/*.js --ignore-pattern vendor",
"gulp": "gulp" "gulp": "gulp",
"test": "mocha --recursive ./src/**/test/*"
}, },
"author": "Ilya Ig. Petrov", "author": "Ilya Ig. Petrov",
"license": "GPLv3", "license": "GPLv3",
"devDependencies": { "devDependencies": {
"chai": "^3.5.0",
"eslint": "^3.15.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": { "dependencies": {
"del": "^2.2.2", "del": "^2.2.2",

View File

@ -125,16 +125,16 @@
key = prefix + key; key = prefix + key;
if (value === null) { if (value === null) {
return localStorage.removeItem(key); return window.localStorage.removeItem(key);
} }
if (value === undefined) { if (value === undefined) {
const item = localStorage.getItem(key); const item = window.localStorage.getItem(key);
return item && JSON.parse(item); return item && JSON.parse(item);
} }
if (value instanceof Date) { if (value instanceof Date) {
throw new TypeError('Converting Date format to JSON is not supported.'); throw new TypeError('Converting Date format to JSON is not supported.');
} }
localStorage.setItem(key, JSON.stringify(value)); window.localStorage.setItem(key, JSON.stringify(value));
}; };

View File

@ -104,6 +104,9 @@
const getCurrentConfigs = function getCurrentConfigs() { const getCurrentConfigs = function getCurrentConfigs() {
return kitchenState(modsKey);
// In case of migration.
const [err, mods, ...warns] = createPacModifiers( kitchenState(modsKey) ); const [err, mods, ...warns] = createPacModifiers( kitchenState(modsKey) );
if (err) { if (err) {
throw err; throw err;

View File

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

View File

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