derby
Version:
MVC framework making it easy to write realtime, collaborative applications that run in both Node.js and browsers.
88 lines (87 loc) • 3.29 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.install = exports.DomTestRunner = void 0;
var racer_1 = require("racer");
var assertions_1 = require("./assertions");
var ComponentHarness_1 = require("./ComponentHarness");
var DomTestRunner = /** @class */ (function () {
function DomTestRunner() {
this.window = null;
this.document = null;
this.harnesses = [];
}
DomTestRunner.prototype.installMochaHooks = function (options) {
options = options || {};
var jsdomOptions = options.jsdomOptions;
// Set up runner's `window` and `document`.
if (racer_1.util.isServer) {
mochaHooksForNode(this, {
jsdomOptions: jsdomOptions
});
}
else {
mochaHooksForBrowser(this);
}
};
DomTestRunner.prototype.createHarness = function () {
var harness = new ComponentHarness_1.ComponentHarness();
if (arguments.length > 0) {
// eslint-disable-next-line prefer-spread, prefer-rest-params
harness.setup.apply(harness, arguments);
}
this.harnesses.push(harness);
return harness;
};
return DomTestRunner;
}());
exports.DomTestRunner = DomTestRunner;
function mochaHooksForNode(runner, options) {
var jsdomOptions = options.jsdomOptions;
// Use an indirect require so that Browserify doesn't try to bundle JSDOM.
var JSDOM = racer_1.util.serverRequire(module, 'jsdom').JSDOM;
var nodeGlobal = global;
// Keep a direct reference so that we're absolutely sure we clean up our own JSDOM.
var jsdom;
global.beforeEach(function () {
jsdom = new JSDOM('', jsdomOptions);
runner.window = jsdom.window;
runner.document = jsdom.window.document;
// Set `window` and `document` globals for Derby code that doesn't allow injecting them.
nodeGlobal.window = runner.window;
nodeGlobal.document = runner.document;
// Initialize "input" and "change" listeners on the document.
module.require('../documentListeners').add(runner.document);
});
global.afterEach(function () {
// Destroy the pages created by the harness, so that if a test cleans up its model itself,
// bindings won't throw errors due to `document` not being present.
runner.harnesses.forEach(function (harness) {
harness.app._pages.forEach(function (page) {
page.destroy();
});
});
runner.harnesses = [];
jsdom.window.close();
runner.window = null;
runner.document = null;
delete nodeGlobal.window;
delete nodeGlobal.document;
});
}
function mochaHooksForBrowser(runner) {
global.beforeEach(function () {
runner.window = global.window;
runner.document = global.window.document;
});
global.afterEach(function () {
runner.window = null;
runner.document = null;
});
}
var runner = new DomTestRunner();
// Set up Chai assertion chain methods: `#html` and `#render`
(0, assertions_1.assertions)(runner, module.require('chai').Assertion);
function install(options) {
runner.installMochaHooks(options);
return runner;
}
exports.install = install;