Run tests in iframe displays results to user

This commit is contained in:
Berkeley Martinez
2016-05-25 18:28:20 -07:00
parent e3e9f67e40
commit 256182836a
9 changed files with 131 additions and 38 deletions

View File

@@ -1,8 +1,12 @@
document.addEventListener('DOMContentLoaded', function() {
var common = parent.__common;
var Rx = parent.Rx;
var frameId = window.__frameId;
var frameReady = common[frameId + 'Ready$'] || { onNext() {} };
var Rx = document.Rx;
var chai = parent.chai;
var source = document.__source;
common.getJsOutput = function evalJs(source = '') {
document.__getJsOutput = function getJsOutput() {
if (window.__err || !common.shouldRun()) {
return window.__err || 'source disabled';
}
@@ -12,13 +16,17 @@ document.addEventListener('DOMContentLoaded', function() {
output = eval(source);
/* eslint-enable no-eval */
} catch (e) {
output = e.message;
window.__err = e;
}
return output;
};
common.runTests$ = function runTests$({ tests = [], source }) {
document.__runTests$ = function runTests$(tests = []) {
/* eslint-disable no-unused-vars */
const editor = { getValue() { return source; } };
const code = source;
/* eslint-enable no-unused-vars */
if (window.__err) {
return Rx.Observable.throw(window.__err);
}
@@ -27,8 +35,11 @@ document.addEventListener('DOMContentLoaded', function() {
// on new stacks
return Rx.Observable.from(tests, null, null, Rx.Scheduler.default)
// add delay here for firefox to catch up
.delay(100)
.delay(200)
/* eslint-disable no-unused-vars */
.map(({ text, testString }) => {
const assert = chai.assert;
/* eslint-enable no-unused-vars */
const newTest = { text, testString };
let test;
try {
@@ -46,7 +57,10 @@ document.addEventListener('DOMContentLoaded', function() {
}
}
} catch (e) {
newTest.err = e.message.split(':').shift();
newTest.err = e.message + '\n' + e.stack;
}
if (!newTest.err) {
newTest.pass = true;
}
return newTest;
})
@@ -55,7 +69,7 @@ document.addEventListener('DOMContentLoaded', function() {
};
// used when updating preview without running tests
common.checkPreview$ = function checkPreview$(args) {
document.__checkPreview$ = function checkPreview$(args) {
if (window.__err) {
return Rx.Observable.throw(window.__err);
}
@@ -66,5 +80,5 @@ document.addEventListener('DOMContentLoaded', function() {
// we set the subject to true
// this will let the updatePreview
// script now that we are ready.
common.testFrameReady$.onNext(true);
frameReady.onNext(null);
});