Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const mockData = require('./response.mock');
const sinon = require('sinon');
const srcScript = fs.readFileSync('./src/assets/main.js', 'utf8');
const scriptRegex = /<\s*script[\s\S]*?>[\s\S]*?<\s*\/\s*script\s*>/ig;
const codeschoolRegex = /https:\/\/www\.codeschool\.com\/users\/.+\.json/i;
// HTML Page
let srcHtml = fs.readFileSync('./src/index.html', 'utf8');
srcHtml = srcHtml.replace(
scriptRegex,
(tag) => !/src\s*=['"][^'"]*assets\//i.test(tag) ? tag : ''
);
// JSDOM Setup
const virtualConsole = jsdom.createVirtualConsole();
virtualConsole.sendTo(console);
// Tests
describe('The webpage', () => {
let document;
let window;
let spy;
before((done) => {
document = jsdom.jsdom(srcHtml, {
virtualConsole: virtualConsole,
});
window = document.defaultView;
function createClient(envConfig, clientConfig, callback){
clientConfig = Object.assign({
appKey: sandbox.appkey
}, clientConfig || {});
envConfig = Object.assign({
html: '<div></div>',
scripts: 'file://' + __dirname + '/../dist/FamilySearch.min.js',
/* Enable the virtual console to pipe the virtual window console
into the console of the current node instance. Helpful for debugging tests. */
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
done: function(error, window){
if(error){
console.error(error);
callback(error);
}
else {
callback(null, new window.FamilySearch(clientConfig));
}
}
}, envConfig || {});
jsdom.env(envConfig);
}
"use strict";
const fs = require("fs");
const {createInstrumenter} = require("istanbul-lib-instrument");
const {jsdom, createVirtualConsole} = require("jsdom");
var virtualConsole = createVirtualConsole();
// Optionally print console logs from the jsdom window.
if (process.env.ENABLE_JSDOM_CONSOLE == "y") {
virtualConsole.sendTo(console);
}
// Path to the browser-polyfill script, relative to the current work dir
// where mocha is executed.
let BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.js";
if (process.env.TEST_MINIFIED_POLYFILL) {
BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.min.js";
} else if (process.env.TEST_BUNDLED_POLYFILL) {
BROWSER_POLYFILL_PATH = process.env.TEST_BUNDLED_POLYFILL;
}
import jsdom from 'jsdom';
import gl from 'gl';
// import sinon from 'sinon';
global.document = jsdom.jsdom(null, {
url: 'http://localhost/',
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
});
global.window = document.defaultView;
Object.keys(document.defaultView).forEach(property => {
if (typeof global[property] === 'undefined') {
global[property] = document.defaultView[property];
}
});
global.window.devicePixelRatio = 1;
global.window.HTMLElement.prototype.clientLeft = 0;
global.window.HTMLElement.prototype.clientTop = 0;
global.window.HTMLCanvasElement.prototype.getContext = function(type, attributes) {
if (!this._webGLContext) {
this._webGLContext = gl(this.width, this.height, attributes);
}
mkHtml("../diagram.html", target+"/diagram.html", { type: "html", prep: function(html) {
jsdom.env({
html: html,
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
scripts: ["node_modules/jquery/dist/jquery.js"],
features: {
FetchExternalResources: ["script"],
ProcessExternalResources: ["script"],
},
done: function (errors, window) {
var $=window.$;
$(function() {
var svg=$("svg");
svg.addClass("img-responsive");
$("script", $("#rr")).each(function(i) {
$(this).replaceWith($(svg[i]));
});
// remove the script that generated the page
$("script").remove();
save(syntax.replace(/%%%/, unwrapDoc(extractDoc(window))));
export const loadJSDOM = done => {
global.document = jsdom.jsdom('<div id="container"></div>', {
FetchExternalResources: false,
ProcessExternalResources: false,
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
url: `${HOST}:${PORT}`,
});
global.window = document.defaultView;
Object.keys(document.defaultView).forEach(property => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js',
};
done();
};
let assert = require('assert');
let jsdom = require('jsdom');
global.document = jsdom.jsdom(undefined, {
virtualConsole: jsdom.createVirtualConsole().sendTo(console)
});
global.window = global.document.defaultView;
global.navigator = { userAgent: "Node.JS" };
let React = require('react/addons');
let _ = require('lodash');
global.React = React;
global.assert = assert;
global._ = _;
const App = React.createClass({
displayName: 'App',
render: function () {
return (
React.createElement(Provider, {
store: store
}, function () {
return React.createElement(HelloWorldReduxContainer, null);
})
);
}
});
jsdom.env('', {
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
scripts: ['http://code.jquery.com/jquery-2.1.1.js'],
done: function (err, window) {
global.window = window;
ReactDOM.render(React.createElement(App, null), window.document.body);
process.nextTick(function () {
store.dispatch({type:'one'});
console.log(store.getState());
process.nextTick(function () {
var cacheUrl_1 = _this.cache.loadUrl(html, url);
if (cacheUrl_1.isCached()) {
debug('This URL is cached', url);
defer.resolve(cacheUrl_1.getCached());
}
else {
jsdom.debugMode = true;
var rendering_1 = false;
debug('SERVER URL = ', 'http://' + _this.config.server.domain + ':' + _this.config.server.port + url);
var document_1 = jsdom.jsdom(html, {
features: {
FetchExternalResources: ['script'],
ProcessExternalResources: ['script']
},
url: 'http://' + _this.config.server.domain + ':' + _this.config.server.port + url,
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
document: {
referrer: '',
cookie: 'key=value; expires=Wed, Sep 21 2011 12:00:00 GMT; path=/',
cookieDomain: _this.config.server.domain
}
});
var window_1 = Object.assign(document_1.defaultView, {
onServer: true,
fs: fs,
logConfig: _this.config.log
});
debug('jsdom.jsdom loaded');
var serverTimeout_1 = setTimeout(function () {
if (rendering_1)
return;
debug('SERVER TIMEOUT ! ! !');
html: '',
url: 'https://example.com/iframe.js?selectedKind=none&selectedStory=none',
src: [workerMock, localStorageMock, matchMediaMock, previewJavascriptCode],
done: (err, window) => {
if (err) return reject(err.response.body);
if (!window || !window[storiesKey]) {
const message =
'Storybook object not found on window. ' +
"Check your call to serializeStories in your Storybook's config.js.";
reject(new Error(message));
}
resolve(window[storiesKey]);
},
};
if (options.debug) {
jsDomConfig.virtualConsole = jsdom.createVirtualConsole().sendTo(console);
}
jsdom.env(jsDomConfig);
});
}