Commit ff438cb7 authored by Professor Chaos's avatar Professor Chaos

chore(test): refactoring tests in this project

parent b01484f2
{"env":{"browser":true,"es6":true,"node":true},"globals":{"document":false,"escape":false,"navigator":false,"unescape":false,"window":false,"describe":true,"before":true,"it":true,"expect":true,"sinon":true},"parser":"babel-eslint","plugins":[],"rules":{"block-scoped-var":2,"brace-style":[2,"1tbs",{"allowSingleLine":true}],"camelcase":[0,{"properties":"always"}],"comma-dangle":[2,"never"],"comma-spacing":[2,{"before":false,"after":true}],"comma-style":[2,"last"],"complexity":0,"consistent-return":2,"consistent-this":0,"curly":[2,"multi-line"],"default-case":0,"dot-location":[2,"property"],"dot-notation":0,"eol-last":2,"eqeqeq":[2,"allow-null"],"func-names":0,"func-style":0,"generator-star-spacing":[2,"both"],"guard-for-in":0,"handle-callback-err":[2,"^(err|error|anySpecificError)$"],"key-spacing":[2,{"beforeColon":false,"afterColon":true}],"keyword-spacing":[2,{"before":true,"after":true}],"linebreak-style":0,"max-depth":0,"max-len":[2,120,4],"max-nested-callbacks":0,"max-params":0,"max-statements":0,"new-cap":[2,{"newIsCap":true,"capIsNew":false}],"newline-after-var":[2,"always"],"new-parens":2,"no-alert":0,"no-array-constructor":2,"no-bitwise":0,"no-caller":2,"no-catch-shadow":0,"no-cond-assign":2,"no-console":0,"no-constant-condition":0,"no-continue":0,"no-control-regex":2,"no-debugger":2,"no-delete-var":2,"no-div-regex":0,"no-dupe-args":2,"no-dupe-keys":2,"no-duplicate-case":2,"no-else-return":2,"no-empty":0,"no-empty-character-class":2,"no-eq-null":0,"no-eval":2,"no-ex-assign":2,"no-extend-native":2,"no-extra-bind":2,"no-extra-boolean-cast":2,"no-extra-parens":0,"no-extra-semi":0,"no-extra-strict":0,"no-fallthrough":2,"no-floating-decimal":2,"no-func-assign":2,"no-implied-eval":2,"no-inline-comments":0,"no-inner-declarations":[2,"functions"],"no-invalid-regexp":2,"no-irregular-whitespace":2,"no-iterator":2,"no-label-var":2,"no-labels":2,"no-lone-blocks":0,"no-lonely-if":0,"no-loop-func":0,"no-mixed-requires":0,"no-mixed-spaces-and-tabs":[2,false],"no-multi-spaces":2,"no-multi-str":2,"no-multiple-empty-lines":[2,{"max":1}],"no-native-reassign":2,"no-negated-in-lhs":2,"no-nested-ternary":0,"no-new":2,"no-new-func":2,"no-new-object":2,"no-new-require":2,"no-new-wrappers":2,"no-obj-calls":2,"no-octal":2,"no-octal-escape":2,"no-path-concat":0,"no-plusplus":0,"no-process-env":0,"no-process-exit":0,"no-proto":2,"no-redeclare":2,"no-regex-spaces":2,"no-reserved-keys":0,"no-restricted-modules":0,"no-return-assign":2,"no-script-url":0,"no-self-compare":2,"no-sequences":2,"no-shadow":0,"no-shadow-restricted-names":2,"no-spaced-func":2,"no-sparse-arrays":2,"no-sync":0,"no-ternary":0,"no-throw-literal":2,"no-trailing-spaces":1,"no-undef":2,"no-undef-init":2,"no-undefined":0,"no-underscore-dangle":0,"no-unneeded-ternary":2,"no-unreachable":2,"no-unused-expressions":0,"no-unused-vars":[2,{"vars":"all","args":"none"}],"no-use-before-define":2,"no-var":0,"no-void":0,"no-warning-comments":0,"no-with":2,"one-var":0,"operator-assignment":0,"operator-linebreak":[2,"after"],"padded-blocks":0,"quote-props":0,"quotes":[2,"single","avoid-escape"],"radix":2,"semi":[2,"always"],"semi-spacing":0,"sort-vars":0,"space-before-blocks":[2,"always"],"space-before-function-paren":[2,{"anonymous":"always","named":"never"}],"space-in-brackets":0,"space-in-parens":[2,"never"],"space-infix-ops":2,"space-unary-ops":[2,{"words":true,"nonwords":false}],"spaced-comment":[2,"always"],"strict":0,"use-isnan":2,"valid-jsdoc":0,"valid-typeof":2,"vars-on-top":2,"wrap-iife":[2,"any"],"wrap-regex":0,"yoda":[2,"never"]}}
\ No newline at end of file
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"globals": {
"document": false,
"escape": false,
"navigator": false,
"unescape": false,
"window": false,
"describe": true,
"before": true,
"it": true,
"expect": true,
"sinon": true
},
"parser": "babel-eslint",
"plugins": [],
"rules": {
"block-scoped-var": 2,
"brace-style": [
2,
"1tbs",
{
"allowSingleLine": true
}
],
"camelcase": [
0,
{
"properties": "always"
}
],
"comma-dangle": [
2,
"never"
],
"comma-spacing": [
2,
{
"before": false,
"after": true
}
],
"comma-style": [
2,
"last"
],
"complexity": 0,
"consistent-return": 2,
"consistent-this": 0,
"curly": [
2,
"multi-line"
],
"default-case": 0,
"dot-location": [
2,
"property"
],
"dot-notation": 0,
"eol-last": 2,
"eqeqeq": [
2,
"allow-null"
],
"func-names": 0,
"func-style": 0,
"generator-star-spacing": [
2,
"both"
],
"guard-for-in": 0,
"handle-callback-err": [
2,
"^(err|error|anySpecificError)$"
],
"key-spacing": [
2,
{
"beforeColon": false,
"afterColon": true
}
],
"keyword-spacing": [
2,
{
"before": true,
"after": true
}
],
"linebreak-style": 0,
"max-depth": 0,
"max-len": [
2,
120,
4
],
"max-nested-callbacks": 0,
"max-params": 0,
"max-statements": 0,
"new-cap": [
2,
{
"newIsCap": true,
"capIsNew": false
}
],
"newline-after-var": [
2,
"always"
],
"new-parens": 2,
"no-alert": 0,
"no-array-constructor": 2,
"no-bitwise": 0,
"no-caller": 2,
"no-catch-shadow": 0,
"no-cond-assign": 2,
"no-console": 0,
"no-constant-condition": 0,
"no-continue": 0,
"no-control-regex": 2,
"no-debugger": 2,
"no-delete-var": 2,
"no-div-regex": 0,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-else-return": 2,
"no-empty": 0,
"no-empty-character-class": 2,
"no-eq-null": 0,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": 0,
"no-extra-semi": 0,
"no-extra-strict": 0,
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-inline-comments": 0,
"no-inner-declarations": [
2,
"functions"
],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 0,
"no-lonely-if": 0,
"no-loop-func": 0,
"no-mixed-requires": 0,
"no-mixed-spaces-and-tabs": [
2,
false
],
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": [
2,
{
"max": 1
}
],
"no-native-reassign": 2,
"no-negated-in-lhs": 2,
"no-nested-ternary": 0,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-require": 2,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-path-concat": 0,
"no-plusplus": 0,
"no-process-env": 0,
"no-process-exit": 0,
"no-proto": 2,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-reserved-keys": 0,
"no-restricted-modules": 0,
"no-return-assign": 2,
"no-script-url": 0,
"no-self-compare": 2,
"no-sequences": 2,
"no-shadow": 0,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-sparse-arrays": 2,
"no-sync": 0,
"no-ternary": 0,
"no-throw-literal": 2,
"no-trailing-spaces": 1,
"no-undef": 2,
"no-undef-init": 2,
"no-undefined": 0,
"no-underscore-dangle": 0,
"no-unneeded-ternary": 2,
"no-unreachable": 2,
"no-unused-expressions": 0,
"no-unused-vars": [
2,
{
"vars": "all",
"args": "none"
}
],
"no-use-before-define": 2,
"no-var": 0,
"no-void": 0,
"no-warning-comments": 0,
"no-with": 2,
"one-var": 0,
"operator-assignment": 0,
"operator-linebreak": [
2,
"after"
],
"padded-blocks": 0,
"quote-props": 0,
"quotes": [
2,
"single",
"avoid-escape"
],
"radix": 2,
"semi": [
2,
"always"
],
"semi-spacing": 0,
"sort-vars": 0,
"space-before-blocks": [
2,
"always"
],
"space-before-function-paren": [
2,
{
"anonymous": "always",
"named": "never"
}
],
"space-in-brackets": 0,
"space-in-parens": [
2,
"never"
],
"space-infix-ops": 2,
"space-unary-ops": [
2,
{
"words": true,
"nonwords": false
}
],
"spaced-comment": [
2,
"always"
],
"strict": 0,
"use-isnan": 2,
"valid-jsdoc": 0,
"valid-typeof": 2,
"vars-on-top": 2,
"wrap-iife": [
2,
"any"
],
"wrap-regex": 0,
"yoda": [
2,
"never"
]
}
}
\ No newline at end of file
.idea/
releases/
.idea/
node_modules/
dist/
.nyc_output
coverage/
coverage/
dist/
docs/
release.zip
\ No newline at end of file
release.zip
yarn-error.log
\ No newline at end of file
......@@ -7,7 +7,7 @@ cache:
script:
- npm run docs
before_deploy:
# - npm run test:coveralls || travis_terminate 1
- npm run test --coveralls || travis_terminate 1
- npm run build
deploy:
- provider: pages
......
......@@ -10,5 +10,8 @@
},
"permissionNotGranted": {
"message": "You did not authorize access to website domain"
},
"activeSession": {
"message": "Instance of sitemap generator is already active. Wait for the previous instance to complete then try again."
}
}
\ No newline at end of file
......@@ -9,33 +9,24 @@
"type": "git",
"url": "https://github.com/pikkumyy/sitemap-generator.git"
},
"babel": {
"presets": [
"env"
]
},
"scripts": {
"start": "xt-build -e dev",
"build": "xt-build -e prod",
"clean": "xt-clean",
"docs": "xt-docs",
"test": "nyc mocha ./test/*.spec.js --require babel-core/register",
"test:coveralls": "npm run test && nyc report --reporter=text-lcov | coveralls",
"release": "git add -A && git commit -m \"release\" && standard-version"
"test": "xt-test",
"release": "git add . && git commit && standard-version"
},
"devDependencies": {
"cws-publish": "^1.0.8",
"pm-components": "^0.0.1",
"pm-extension-cli": "^0.4.0",
"standard-version": "^4.3.0",
"babel-preset-env": "^1.7.0",
"babel-core": "^6.26.3",
"coveralls": "^3.0.1",
"mocha": "^5.2.0",
"nyc": "^11.8.0",
"chai": "^4.1.2",
"jsdom": "11.10.0",
"jsdom-global": "3.0.2",
"sinon-chrome": "^2.3.2"
"pm-extension-cli": "0.7.0",
"standard-version": "^4.3.0"
},
"babel": {
"presets": [
"env"
]
},
"xtdocs": {
"templates": {
......
......@@ -20,11 +20,7 @@ class BackgroundApi {
*/
static resolveSetupPageUrl(url) {
let appPath = '';
if (url && url.indexOf('http') === 0) {
appPath = url;
}
let appPath = (url && url.indexOf('http') === 0) ? url : '';
return setupPageURI + '?u=' + appPath;
}
......@@ -36,11 +32,7 @@ class BackgroundApi {
* @see {@link https://developer.chrome.com/extensions/browserAction#event-onClicked|onClicked}
*/
static openSetupPage(tab) {
if (generator) {
return false;
}
let windowUrl = BackgroundApi.resolveSetupPageUrl(tab.url);
let windowUrl = BackgroundApi.resolveSetupPageUrl((tab || {}).url);
return CenteredPopup.open(600, 600, windowUrl, 'popup')
.then(BackgroundApi.setupWindowId);
......@@ -58,19 +50,16 @@ class BackgroundApi {
* @see {@link https://developer.chrome.com/extensions/runtime#type-MessageSender|MessageSender}
*/
static launchRequest(request, sender) {
if (!request.start) {
return false;
if (request.start) {
let config = request.start,
callback = (granted) => BackgroundApi
.handleGrantResponse(granted, config, sender);
window.chrome.permissions.request({
permissions: ['tabs', 'downloads'],
origins: [config.requestDomain]
}, callback);
}
let config = request.start,
callback = (granted) => BackgroundApi
.handleGrantResponse(granted, config, sender);
window.chrome.permissions.request({
permissions: ['tabs', 'downloads'],
origins: [config.requestDomain]
}, callback);
return true;
}
/**
......@@ -83,12 +72,13 @@ class BackgroundApi {
if (sender && sender.tab) {
window.chrome.tabs.remove(sender.tab.id);
}
if (granted) {
if (granted && !generator) {
BackgroundApi.onStartGenerator(config);
} else {
window.alert(window.chrome.i18n.getMessage('permissionNotGranted'));
let msg = generator ? 'activeSession' : 'permissionNotGranted';
window.alert(window.chrome.i18n.getMessage(msg));
}
return granted;
}
/**
......@@ -105,13 +95,9 @@ class BackgroundApi {
* @param {Object} config - generator configuration
*/
static onStartGenerator(config) {
if (generator) {
return false;
}
config.callback = BackgroundApi.onCrawlComplete;
generator = new Generator(config);
generator.start();
return generator;
}
}
......
......@@ -120,6 +120,4 @@ class Crawler {
}
}
(() => new Crawler())();
export default Crawler;
import Crawler from './crawler';
(() => new Crawler())();
\ No newline at end of file
......@@ -9,26 +9,14 @@
"incognito": "not_allowed",
"minimum_chrome_version": "46",
"homepage_url": "https://chrome.google.com/webstore/detail/hcnjemngcihnhncobgdgkkfkhmleapah",
"permissions": [
"activeTab",
"webRequest",
"webRequestBlocking",
"system.display"
],
"permissions": ["activeTab", "webRequest", "webRequestBlocking", "system.display"],
"content_security_policy": "script-src 'self' 'sha256-rwQFR+Lqw7LzDK8rVQATBE+3CIpTqJlKlhIbusXISKE=' https://www.google-analytics.com; object-src 'self'",
"optional_permissions": [
"downloads",
"tabs",
"http://*/*",
"https://*/*"
],
"optional_permissions": ["downloads", "tabs", "http://*/*", "https://*/*"],
"icons": {
"128": "icons/128x128.png"
},
"background": {
"scripts": [
"background.js"
],
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
......
import Process from './process';
(() => new Process())();
\ No newline at end of file
......@@ -43,6 +43,4 @@ class Process {
}
}
(() => new Process())();
export default Process;
import Setup from './setup';
(() => new Setup())();
\ No newline at end of file
......@@ -110,9 +110,6 @@ class Setup {
}
return result;
}
}
(() => new Setup())();
export default Setup;
body {
font-family: "Roboto", sans-serif;
font-size: 100%;
......
// import chrome from 'sinon-chrome';
// import chai from 'chai';
// import BackgroundApi from '../src/background/backgroundApi';
//
// require('jsdom-global')();
// const expect = chai.expect;
//
// let url = "https://www.test.com/",
// requestDomain = url + "/*",
// testPages = {
// a: "https://www.test.com/index.html",
// b: "https://www.test.com/about.html",
// c: "https://www.test.com/home.html",
// d: "https://www.nottest.com/index.html"
// },
// defaultConfig = {url: url, requestDomain: requestDomain},
// defaultSender = {tab: {id: 1}};
//
// describe('Background Api', () => {
// before(() => {
// window.chrome = chrome;
// window.alert = () => {
// };
// });
// beforeEach(() => {
// chrome.flush();
// });
// it('constructor should register onmessage listener', () => {
// expect(window.chrome.runtime.onMessage.addListener.notCalled).to.be.true;
// new BackgroundApi();
// expect(window.chrome.runtime.onMessage.addListener.notCalled).to.not.be.true;
// });
// it('constructor should register browseraction listener', () => {
// expect(window.chrome.browserAction.onClicked.addListener.notCalled).to.be.true;
// new BackgroundApi();
// expect(window.chrome.browserAction.onClicked.addListener.notCalled).to.not.be.true;
// });
// it('resolveSetupPageUrl should include active tab url if it starts with http', () => {
// let result = BackgroundApi.resolveSetupPageUrl(testPages.a);
// expect(result).to.contain(testPages.a);
// });
// it('resolveSetupPageUrl should not include active tab url if it does not start with http', () => {
// let result = BackgroundApi.resolveSetupPageUrl("chrome://about");
// expect(result).to.not.contain("chrome://about");
// });
// it('openSetupPage launches only if generator does not exist', () => {
// BackgroundApi.onStartGenerator(defaultConfig);
// expect(BackgroundApi.openSetupPage({url: testPages.a})).to.be.false;
// BackgroundApi.onCrawlComplete();
// expect(BackgroundApi.openSetupPage({url: ''})).to.not.be.false;
// });
// it('launchRequest starts generator without error', () => {
// expect(BackgroundApi.launchRequest({start: defaultConfig}, defaultSender)).to.be.true;
// });
// it('launchRequest does not try to start when config not provided', () => {
// expect(BackgroundApi.launchRequest({incorrect: defaultConfig}, defaultSender)).to.be.false;
// });
// it('handleGrantResponse does starts when permission granted', () => {
// BackgroundApi.onCrawlComplete(); // kill any existing intanse
// expect(BackgroundApi.handleGrantResponse(true, defaultConfig, defaultSender)).to.be.true;
// });
// it('handleGrantResponse does NOT start when permission not granted', () => {
// expect(BackgroundApi.handleGrantResponse(false, defaultConfig)).to.be.false;
// });
// it('generator does not try to start when already exists', () => {
// BackgroundApi.onStartGenerator(defaultConfig);
// expect(BackgroundApi.onStartGenerator(defaultConfig)).to.be.false;
// });
// });
import BackgroundApi from '../src/background/backgroundApi';
import { Generator } from '../src/background/generator/generator';
import { CenteredPopup } from 'pm-components';
let launchSpy;
let launchRequest;
let alertValue;
let alertCalled;
describe('Background Api', () => {
before(() => {
launchRequest = (sender) => {
window.chrome.runtime.onMessage.dispatch(
{ start: { requestDomain: 'http://www.google.com' } }, sender);
};
alertCalled = 'called';
window.alert = () => { alertValue = alertCalled }
});
beforeEach(() => {
new BackgroundApi();
alertValue = null;
launchSpy = sinon.spy(BackgroundApi, "onStartGenerator");
})
afterEach(() => {
if (launchSpy) launchSpy.restore();
BackgroundApi.onCrawlComplete();
})
it('clicking browser action opens setup page', () => {
sinon.stub(CenteredPopup, 'open');
CenteredPopup.open.resolves(1);
expect(CenteredPopup.open.notCalled, 'window not opened').to.be.true;
window.chrome.browserAction.onClicked.dispatch({ url: "https://www.google.com" });
expect(CenteredPopup.open.calledOnce, 'window opened').to.be.true;
window.chrome.browserAction.onClicked.dispatch(null)
expect(CenteredPopup.open.calledTwice, '2nd window opened').to.be.true;
});
it('launch request starts generator', () => {
window.chrome.permissions.request.yields(true);
expect(BackgroundApi.onStartGenerator.notCalled, 'launch method not called').to.be.true;
window.chrome.runtime.onMessage.dispatch({ wrongLaunchRequest: true });
expect(BackgroundApi.onStartGenerator.notCalled, 'launch only on start request').to.be.true;
launchRequest({ tab: { id: 1 } });
expect(window.chrome.permissions.request.calledOnce, 'permissions').to.be.true;
expect(BackgroundApi.onStartGenerator.calledOnce, 'launch method').to.be.true;
});
it('launch does not occur when permission not granted', () => {
window.chrome.permissions.request.yields(false);
expect(BackgroundApi.onStartGenerator.notCalled, 'launch method not called').to.be.true;
launchRequest();
expect(BackgroundApi.onStartGenerator.notCalled, 'launch method not called').to.be.true;
expect(alertValue, 'window alert shows').to.equal(alertCalled);
});
it('only 1 generator can run at a time', () => {
window.chrome.permissions.request.yields(true);
expect(BackgroundApi.onStartGenerator.notCalled, 'launch method not called').to.be.true;
launchRequest();
expect(BackgroundApi.onStartGenerator.calledOnce, 'launch occurred').to.be.true;
launchRequest();
expect(BackgroundApi.onStartGenerator.calledOnce, '2nd launch does not occur').to.be.true;
expect(alertValue, 'window alert shows').to.equal(alertCalled);
});
});
\ No newline at end of file
// import chrome from 'sinon-chrome';
// import chai from 'chai';
// import 'jsdom-global';
//
// const expect = chai.expect;
//
// let Crawler;
//
// describe('Page Crawler', () => {
// before(() => {
// window.chrome = chrome;
// global.setTimeout = () => {
// };
// global.setInterval = () => {
// };
// Crawler = require('../src/crawler/crawler.js');
// });
// beforeEach(function () {
// chrome.flush();
// document.documentElement.innerHTML =
// "<html><head></head><body><a href='home.html'>Home</a></body>";
// });
// });
import Crawler from '../src/crawler/crawler';
const fs = require('fs');
let pageHTML;
describe('Page Crawler', () => {
before(() => {
pageHTML = "<html><head></head><body><a href='home.html'>Home</a></body>";
window.setTimeout = () => {
};
window.setInterval = () => {
};
});
beforeEach(function () {
document.documentElement.innerHTML = pageHTML;
});
it('Page crawler initializes without error', () => {
expect(() => new Crawler()).to.not.throw();
});
});
// import chrome from 'sinon-chrome';
// import chai from 'chai';
// import 'jsdom-global';
//
// const expect = chai.expect;
//
// let Process;
//
// describe('Process Page', () => {
// before(() => {
// window.chrome = chrome;
// document.documentElement.innerHTML = require('fs')
// .readFileSync('./src/process/process.html', 'utf8');
// global.setTimeout = () => {
// };
// global.setInterval = () => {
// };
// window.ga = () => {
// };
// Process = require('../src/process/process.js');
// });
// beforeEach(function () {