Skip to content

Commit

Permalink
Feat (biblio.js): don't go to network for local refs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Apr 1, 2016
1 parent 18a4b0f commit f10b5a7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 25 deletions.
39 changes: 25 additions & 14 deletions js/core/biblio.js
Expand Up @@ -138,25 +138,30 @@ define(

return {
stringifyRef: stringifyRef,
run: function (conf, doc, cb, msg) {
run: function (conf, doc, cb, msg) {
msg.pub("start", "core/biblio");
var refs = getRefKeys(conf)
, localAliases = []
, finish = function () {
msg.pub("end", "core/biblio");
cb();
}
;
var localAliases = [];
var finish = function () {
msg.pub("end", "core/biblio");
cb();
};
var localKeys = [];;
if (conf.localBiblio) {
for (var k in conf.localBiblio) {
if (typeof conf.localBiblio[k].aliasOf !== "undefined") {
localAliases.push(conf.localBiblio[k].aliasOf);
}
}
}
refs = refs.normativeReferences
.concat(refs.informativeReferences)
.concat(localAliases);
refs = getRefKeys(conf)
.normativeReferences
.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(",");
fetch(url)
Expand All @@ -165,17 +170,23 @@ define(
})
.then(function(data){
conf.biblio = data || {};
// override biblio data
if (conf.localBiblio) {
for (var k in conf.localBiblio) conf.biblio[k] = conf.localBiblio[k];
}
bibref(conf, msg);
finish();
}).catch(function(err){
msg.pub("error", err);
console.error(err);
finish();
});
} else {
} 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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Expand Up @@ -68,7 +68,7 @@ module.exports = function(config) {
included: false,
served: true,
},{
pattern: "node_modules/whatwg-fetch/fetch.js",
pattern: "./node_modules/whatwg-fetch/fetch.js",
included: false,
served: true,
},
Expand Down
35 changes: 30 additions & 5 deletions 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",
Expand Down Expand Up @@ -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.
Expand All @@ -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);
});
});
});
12 changes: 11 additions & 1 deletion tests/spec/core/inlines-spec.js
Expand Up @@ -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() +
Expand All @@ -15,6 +15,16 @@ describe("Core - Inlines", function() {
" <p>[[!DAHU]] [[REX]]</p>" +
"</section>",
};
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);
Expand Down
7 changes: 5 additions & 2 deletions tests/spec/w3c/local-aliases-spec.js
Expand Up @@ -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);
});
});
8 changes: 6 additions & 2 deletions tests/test-main.js
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit f10b5a7

Please sign in to comment.