Merge pull request #4862 from FreeCodeCamp/fix/incorrect-editor-value

Fix javascript challenges
This commit is contained in:
Arsen Melikyan
2015-12-01 20:31:28 +04:00
4 changed files with 27 additions and 7 deletions

View File

@ -15,6 +15,7 @@ window.common = (function(global) {
common.executeChallenge$ = function executeChallenge$() { common.executeChallenge$ = function executeChallenge$() {
const code = common.editor.getValue(); const code = common.editor.getValue();
const originalCode = code;
const head = common.arrayToNewLineString(common.head); const head = common.arrayToNewLineString(common.head);
const tail = common.arrayToNewLineString(common.tail); const tail = common.arrayToNewLineString(common.tail);
@ -75,6 +76,7 @@ window.common = (function(global) {
data, data,
code, code,
userTests, userTests,
originalCode,
output: data.output.replace(/\\\"/gi, '') output: data.output.replace(/\\\"/gi, '')
}); });
}); });

View File

@ -5,14 +5,19 @@ window.common = (function(global) {
common = { init: [] } common = { init: [] }
} = global; } = global;
common.runTests$ = function runTests$({ code, userTests, ...rest }) { common.runTests$ = function runTests$({
code,
originalCode,
userTests,
...rest
}) {
return Observable.from(userTests) return Observable.from(userTests)
.map(function(test) { .map(function(test) {
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
const assert = chai.assert; const assert = chai.assert;
const editor = { getValue() { return code; }}; const editor = { getValue() { return originalCode; }};
/* eslint-enable no-unused-vars */ /* eslint-enable no-unused-vars */
try { try {

View File

@ -1,6 +1,6 @@
window.common = (function(global) { window.common = (function(global) {
const { const {
Rx: { Observable }, Rx: { BehaviorSubject, Observable },
common = { init: [] } common = { init: [] }
} = global; } = global;
@ -27,6 +27,11 @@ window.common = (function(global) {
const iFrameScript$ = const iFrameScript$ =
common.getScriptContent$('/js/iFrameScripts.js').shareReplay(); common.getScriptContent$('/js/iFrameScripts.js').shareReplay();
// behavior subject allways remembers the last value
// we use this to determine if runPreviewTest$ is defined
// and prime it with false
common.previewReady$ = new BehaviorSubject(false);
// runPreviewTests$ should be set up in the preview window // runPreviewTests$ should be set up in the preview window
common.runPreviewTests$ = common.runPreviewTests$ =
() => Observable.throw({ err: new Error('run preview not enabled') }); () => Observable.throw({ err: new Error('run preview not enabled') });
@ -42,13 +47,16 @@ window.common = (function(global) {
return iFrameScript$ return iFrameScript$
.map(script => `<script>${script}</script>`) .map(script => `<script>${script}</script>`)
.flatMap(script => { .flatMap(script => {
// we make sure to override the last value in the
// subject to false here.
common.previewReady$.onNext(false);
preview.open(); preview.open();
preview.write(libraryIncludes + code + '<!-- -->' + script); preview.write(libraryIncludes + code + '<!-- -->' + script);
preview.close(); preview.close();
return Observable.fromCallback($(preview).ready, $(preview))() // now we filter false values and wait for the first true
.first() return common.previewReady$
// delay is need here for first initial run .filter(ready => ready)
.delay(100); .first();
}) })
.map(() => code); .map(() => code);
}; };

View File

@ -35,4 +35,9 @@ window.__$(function() {
.map(tests => ({ ...rest, tests })); .map(tests => ({ ...rest, tests }));
}; };
// now that the runPreviewTest$ is defined
// we set the subject to true
// this will let the updatePreview
// script now that we are ready.
common.previewReady$.onNext(true);
}); });