From a933658eb75a64c048a664be082d580c091761ab Mon Sep 17 00:00:00 2001 From: Marcos Caceres Date: Sat, 5 Mar 2016 01:23:40 +1100 Subject: [PATCH] Feat (biblio.js): don't go to network for local refs --- js/core/biblio.js | 24 +++++++++++++++---- karma.conf.js | 4 ++++ tests/spec/core/biblio-spec.js | 35 ++++++++++++++++++++++++---- tests/spec/core/inlines-spec.js | 12 +++++++++- tests/spec/w3c/local-aliases-spec.js | 7 ++++-- tests/test-main.js | 8 +++++-- 6 files changed, 75 insertions(+), 15 deletions(-) diff --git a/js/core/biblio.js b/js/core/biblio.js index 195e997a93..edac80dd0f 100644 --- a/js/core/biblio.js +++ b/js/core/biblio.js @@ -3,7 +3,7 @@ // Handles bibliographic references // Configuration: // - localBiblio: override or supplement the official biblio with your own. - +"use strict"; define( [], function () { @@ -146,6 +146,7 @@ define( msg.pub("end", "core/biblio"); cb(); } + , localKeys = []; ; if (conf.localBiblio) { for (var k in conf.localBiblio) { @@ -155,16 +156,21 @@ define( } } refs = refs.normativeReferences - .concat(refs.informativeReferences) - .concat(localAliases); + .concat(refs.informativeReferences) + .concat(localAliases) + //don't go to network for local refs + .filter(function(key){ + return !(conf.localBiblio && conf.localBiblio.hasOwnProperty(key)); + }); + conf.biblio = {}; if (refs.length) { var url = "https://labs.w3.org/specrefs/bibrefs?refs=" + refs.join(","); $.ajax({ dataType: "json" , url: url , success: function (data) { - conf.biblio = data || {}; // override biblio data + conf.biblio = data || {}; if (conf.localBiblio) { for (var k in conf.localBiblio) conf.biblio[k] = conf.localBiblio[k]; } @@ -176,8 +182,16 @@ define( finish(); } }); + } else if(conf.localBiblio){ + //Populate biblio with local data + Object.getOwnPropertyNames(conf.localBiblio) + .reduce(function (biblio, next){ + conf.biblio[next] = conf.localBiblio[next]; + return biblio; + }, conf.biblio); + bibref(conf, msg); + finish(); } - else finish(); } }; } diff --git a/karma.conf.js b/karma.conf.js index dca18aece9..5f874347a1 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -68,6 +68,10 @@ module.exports = function(config) { pattern: "./node_modules/webidl2/lib/*.js", included: false, served: true, + }, { + pattern: "./node_modules/whatwg-fetch/fetch.js", + included: false, + served: true, }, "tests/spec/SpecHelper.js", "tests/test-main.js", diff --git a/tests/spec/core/biblio-spec.js b/tests/spec/core/biblio-spec.js index c3eb580016..8dd9987cd1 100644 --- a/tests/spec/core/biblio-spec.js +++ b/tests/spec/core/biblio-spec.js @@ -1,13 +1,33 @@ "use strict"; describe("W3C — Bibliographic References", function() { + var isSpecRefAvailable = true; + afterAll(function(done) { flushIframes(); done(); }); + + // Ping biblio service to see if it's running + it("should reach biblio service", function(done) { + var fetchOps = { + method: "HEAD" + }; + fetch("https://labs.w3.org/specrefs/bibrefs", fetchOps) + .then(function(res) { + isSpecRefAvailable = res.ok; + expect(res.ok).toBeTruthy(); + done(); + }) + .catch(function(err) { + fail("Error", err); + done(); + }); + }); + var customConfig = { - editors: [ - {name: "Robin Berjon"} - ], + editors: [{ + name: "Robin Berjon" + }], shortName: "Foo", specStatus: "WD", prevVersion: "FPWD", @@ -43,6 +63,11 @@ describe("W3C — Bibliographic References", function() { // Make sure the reference is added. var ref = doc.querySelector("#bib-TestRef1 + dd"); expect(ref).toBeTruthy(); + // This prevents Jasmine from taking down the whole test suite if SpecRef is down. + if (!isSpecRefAvailable) { + var err = new Error("SpecRef seems to be down. Can't proceed with this spec."); + return Promise.reject(err); + } expect(ref.textContent).toMatch(/Publishers Inc\.\s/); ref = null; // Make sure the ". " is automatically added to publisher. @@ -55,6 +80,6 @@ describe("W3C — Bibliographic References", function() { ref = doc.querySelector("#bib-TestRef3 + dd"); expect(ref).toBeTruthy(); expect(ref.textContent).toMatch(/^Publisher Here\.\s/); - }).then(done); + }).then(done).catch(done); }); -}); +}); \ No newline at end of file diff --git a/tests/spec/core/inlines-spec.js b/tests/spec/core/inlines-spec.js index 4ae98367bb..26dfd9b234 100644 --- a/tests/spec/core/inlines-spec.js +++ b/tests/spec/core/inlines-spec.js @@ -4,7 +4,7 @@ describe("Core - Inlines", function() { flushIframes(); done(); }); - it("should process all inline content", function(done) { + it("should process all in-line content", function(done) { var ops = { config: makeBasicConfig(), body: makeDefaultBody() + @@ -15,6 +15,16 @@ describe("Core - Inlines", function() { "

[[!DAHU]] [[REX]]

" + "", }; + ops.config.localBiblio = { + "DAHU": { + title: "One short leg. How I learned to overcome.", + publisher: "Publishers Inc." + }, + "REX": { + title: "Am I a dinosaur or a failed technology?", + publisher: "Publishers Inc." + }, + }; makeRSDoc(ops, function(doc) { var $inl = $("#inlines", doc); var $nr = $("#normative-references", doc); diff --git a/tests/spec/w3c/local-aliases-spec.js b/tests/spec/w3c/local-aliases-spec.js index ea99fd206a..5632f5a66f 100644 --- a/tests/spec/w3c/local-aliases-spec.js +++ b/tests/spec/w3c/local-aliases-spec.js @@ -12,13 +12,16 @@ describe("W3C — Aliased References", function() { }; ops.config.localBiblio = { "FOOBARGLOP": { - "aliasOf": "RFC2119" + "aliasOf": "BARBAR", + }, + "BARBAR": { + title: "The BARBAR Spec", } }; makeRSDoc(ops, function(doc) { var $r = $("#bib-FOOBARGLOP + dd", doc); expect($r.length).toBeTruthy(); - expect($r.text()).toMatch(/2119/); + expect($r.text()).toMatch(/BARBAR/); }).then(done); }); }); diff --git a/tests/test-main.js b/tests/test-main.js index 6959880399..5992ccda02 100644 --- a/tests/test-main.js +++ b/tests/test-main.js @@ -17,7 +17,10 @@ var testFiles = Object.keys(window.__karma__.files) return collector; }, []); -var allDeps = ["js/core/jquery-enhanced"].concat(testFiles); +var allDeps = [ + "js/core/jquery-enhanced", + "fetch", +].concat(testFiles); require.config({ // Karma serves files under /base, which is the basePath from your config file @@ -26,8 +29,9 @@ require.config({ "jquery": "/base/node_modules/jquery/dist/jquery", "core/utils": "js/core/utils", "core/jquery-enhanced": "js/core/jquery-enhanced", + "fetch": "/base/node_modules/whatwg-fetch/fetch" }, - // dynamically load all test files + // dynamically load all test files and other deps deps: allDeps, // we have to kickoff jasmine, as it is asynchronous callback: window.__karma__.start,