Add workaround for Array.from when running in PhantomJS

https://github.com/openstreetmap/iD/issues/6087#issuecomment-476219308

Also added `npm run phantom` to test workarounds like this quickly
This commit is contained in:
Bryan Housel
2019-03-25 12:47:55 -04:00
parent 776bf103d8
commit daaab30d13
5 changed files with 63 additions and 10 deletions
+1 -9
View File
@@ -321,15 +321,7 @@ export function coreHistory(context) {
}
});
});
if (window.mocha) {
var arr = [];
s.forEach(function(v) { arr.push(v); });
return arr;
} else {
return Array.from(s);
}
// var arr = _map(_stack.slice(1, _index + 1), 'imageryUsed');
// return _without(_uniq(_flatten(arr)), 'Custom');
return Array.from(s);
}
},
+1
View File
@@ -26,6 +26,7 @@
"imagery": "node data/update_imagery",
"lint": "eslint *.js test/spec modules",
"start": "node development_server.js develop",
"phantom": "phantomjs --web-security=no node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js test/phantom.html spec",
"test": "npm-run-all -s lint build test:**",
"test:phantom": "phantomjs --web-security=no node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js test/index.html spec",
"translations": "node data/update_locales"
+39
View File
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Mocha Tests</title>
<link rel='stylesheet' href='../node_modules/mocha/mocha.css'>
<link rel='stylesheet' href='../dist/iD.css'>
<!-- <script src='../node_modules/d3/build/d3.js'></script> -->
</head>
<body style="overflow:scroll">
<div id='mocha'></div>
<script src='../node_modules/mocha/mocha.js'></script>
<script src='../node_modules/chai/chai.js'></script>
<script src='../node_modules/sinon/pkg/sinon.js'></script>
<script src='../node_modules/sinon-chai/lib/sinon-chai.js'></script>
<script src='../node_modules/happen/happen.js'></script>
<script>
if (typeof initMochaPhantomJS === 'function') {
initMochaPhantomJS()
}
</script>
<!-- include source files here... -->
<script src='../dist/iD.js'></script>
<script src='spec/spec_helpers.js'></script>
<!-- include spec files below... -->
<script src='spec/phantom.js'></script>
<script>
window.mocha.run();
</script>
</body>
</html>
+7
View File
@@ -0,0 +1,7 @@
describe('test some capabilities of PhantomJS', function () {
it('Array.from(Set)', function () {
var s = new Set([1,1]);
var result = Array.from(s);
expect(result).to.eql([1]);
});
});
+15 -1
View File
@@ -36,4 +36,18 @@ mocha.setup({
expect = chai.expect;
window.d3 = iD.d3; // TODO: remove
window.d3 = iD.d3; // TODO: remove if we can avoid exporting all of d3.js
// workaround for `Array.from` polyfill in PhantomJS
// https://github.com/openstreetmap/iD/issues/6087#issuecomment-476219308
var __arrayfrom = Array.from;
Array.from = function(what) {
if (what instanceof Set) {
var arr = [];
what.forEach(function(v) { arr.push(v); });
return arr;
} else {
return __arrayfrom.apply(null, arguments);
}
};